Created
January 12, 2013 19:52
-
-
Save awd/4520195 to your computer and use it in GitHub Desktop.
Determine if a number is Prime. I was recently asked to create a method which given a number would return if a number was Prime or not.
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
# determine if a number is prime | |
# not divisible by a natural number greater than 1, and less than itself | |
def is_prime?(n) | |
return false if n == 1 | |
# start the range greater than 1 | |
# do not include the number itself | |
range = (2..(n-1)).to_a | |
detection = range.detect do |n2| | |
(n % n2) == 0 | |
end | |
return false if detection | |
true | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment