Skip to content

Instantly share code, notes, and snippets.

@ahimmelstoss
Created October 11, 2013 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahimmelstoss/6939967 to your computer and use it in GitHub Desktop.
Save ahimmelstoss/6939967 to your computer and use it in GitHub Desktop.
def prime?(number)
tries = 0 #keep track for efficiency
prime = !(2...number).any? do |i|
tries += 1
number % i == 0
end
#is prime if it is NOT divisible by any number evenly from 2 through the number
prime_in_words = prime ? "prime" : "not prime" #if true, else false
prime #return prime
puts "#{number} is #{prime_in_words}."
puts "...solved in #{tries} tries."
end
prime? 119
prime? 8
prime? 6003
prime? 10007
prime? 2
prime? 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment