class PrimeTest::PrimeCalculator

Constants

LOWEST_PRIME

Public Class Methods

first_n(n, i=LOWEST_PRIME, results=[]) click to toggle source
# File lib/prime_test/prime_calculator.rb, line 6
def self.first_n(n, i=LOWEST_PRIME, results=[])
  n = n.to_i

  if results.length < n
    results << i if prime?(i)
    first_n(n, i+1, results)
  end
  results
end
prime?(n) click to toggle source
# File lib/prime_test/prime_calculator.rb, line 28
def self.prime?(n)
  sieve_up_to(n.to_i).include?(n.to_i)
end
sieve_up_to(max) click to toggle source
# File lib/prime_test/prime_calculator.rb, line 16
def self.sieve_up_to(max)
  list = (0..max).to_a
  list[0] = list[1] = nil
  
  list.each do |p|
    next unless p
    break if p*p > max
    (p*p).step(max, p) { |m| list[m] = nil }
  end
  list.compact
end