Skip to content

Instantly share code, notes, and snippets.

@evizitei
Created January 6, 2015 14:48
Show Gist options
  • Save evizitei/6ac3a55c2baeef22ba3f to your computer and use it in GitHub Desktop.
Save evizitei/6ac3a55c2baeef22ba3f to your computer and use it in GitHub Desktop.
NthPrime
class Prime
PRIMES = [2, 3]
def self.nth(index)
raise ArgumentError if index < 1
return PRIMES[index - 1] if PRIMES.length >= index
generate_primes_up_to(index)
end
def self.generate_primes_up_to(index)
candidate = PRIMES.last + 2
counter = PRIMES.length
while counter < index do
if is_prime?(candidate)
PRIMES << candidate
counter += 1
end
candidate += 2
end
PRIMES[index - 1]
end
def self.is_prime?(num)
return false if PRIMES.any?{|p| num % p == 0 }
factor = PRIMES.last + 2
while factor <= Math.sqrt(num) do
return false if num % p == 0
factor += 2
end
return true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment