Skip to content

Instantly share code, notes, and snippets.

@plashchynski
Last active April 11, 2020 22:03
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 plashchynski/90f87084bd019f5c4cb2fe4f2c9afaac to your computer and use it in GitHub Desktop.
Save plashchynski/90f87084bd019f5c4cb2fe4f2c9afaac to your computer and use it in GitHub Desktop.
Check if the number is prime in Ruby
# 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