Skip to content

Instantly share code, notes, and snippets.

@awd
Created January 12, 2013 19:52
Show Gist options
  • Save awd/4520195 to your computer and use it in GitHub Desktop.
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.
# 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