Sha256: 0633f85c11103f78ed6a45f1b0b1f87e98d4974b63068dc121103526a20b6e5f

Contents?: true

Size: 936 Bytes

Versions: 1

Compression:

Stored size: 936 Bytes

Contents

require 'eratostene_sieve/version'
require 'eratostene_sieve/railtie' if defined? Rails

# Utility to get:
# - nth_prime(n): the nth prime number.
# - prime_serie: an enumerator on prime number serie.
module EratosteneSieve
  Inf = 1.0 / 0.0

  def self.nth_prime(index)
    return first_primes[index] if index < first_primes.size
    counter = first_primes.size - 1
    ((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

  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

  def self.first_primes
    @first_primes ||= [1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
eratostene_sieve-0.0.2.alpha lib/eratostene_sieve.rb