Created
October 11, 2013 18:46
-
-
Save ahimmelstoss/6939967 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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