Last active
April 11, 2020 22:03
-
-
Save plashchynski/90f87084bd019f5c4cb2fe4f2c9afaac to your computer and use it in GitHub Desktop.
Check if the number is prime in Ruby
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
# trial division — the simplest primality test | |
def is_prime(number) | |
return number > 1 if number <= 3 | |
return false if number % 2 == 0 || number % 3 == 0 | |
i = 5 | |
while i * i <= number do | |
return false if number % i == 0 || number % (i + 2) == 0 | |
i = i + 6 | |
end | |
true | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment