module EratosteneSieve

Utility to get:

Defines lib version constant.

Constants

Inf
VERSION

Public Class Methods

nth_prime(index) click to toggle source
# File lib/eratostene_sieve.rb, line 10
def self.nth_prime(index)
  return first_primes[index] if index < first_primes.size
  counter = first_primes.size
  ((first_primes.last + 2)..Inf).step(2) do |candidate|
    (2..((candidate**0.5).to_i)).each do |m|
      break if candidate % m == 0
      counter += 1 if m == (candidate**0.5).to_i
    end
    return candidate.to_i if counter == index
  end
end
prime_serie() click to toggle source
# File lib/eratostene_sieve.rb, line 22
def self.prime_serie
  Enumerator.new do |y|
    a = 1
    b = 2
    index = 2
    loop do
      y << a
      a, b = b, nth_prime(index)
      index += 1
    end
  end
end

Private Class Methods

first_primes() click to toggle source
# File lib/eratostene_sieve.rb, line 37
def self.first_primes
  @first_primes ||= [1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
end