Skip to content

Instantly share code, notes, and snippets.

@KevinWMatthews
Created April 15, 2015 03:14
Show Gist options
  • Save KevinWMatthews/eea637827ae11fc3c6dc to your computer and use it in GitHub Desktop.
Save KevinWMatthews/eea637827ae11fc3c6dc to your computer and use it in GitHub Desktop.
prime?
def self.prime?(num)
return num > 1 if num <= 3
return false if (divisible?(num,2) || divisible?(num,3))
# loop while (6*k-1)**2 <= num, or
# k <= (1+sqrt(num)/6)
max_k = (Math.sqrt(num)+1)/6.0
return false if (1..max_k).any? {|k| divisible?(num,6*k+1) || divisible?(num,6*k-1)}
end
@sarahjschultz
Copy link

@KevinWMatthews -- will you post your updates to this, here in the gist? I'm curious to see what you come up with :D

@KevinWMatthews
Copy link
Author

Oh! Just saw this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment