Class: FundingPrimer::Prime::PrimorialGenerator

Inherits:
BaseGenerator show all
Defined in:
lib/funding_primer/prime.rb

Instance Method Summary collapse

Methods inherited from BaseGenerator

#first

Instance Method Details

#primorial(n) ⇒ Object

Calculates the primorial of the integer n. That is, the product of the first n prime numbers.

Raises an IndexError like Array#fetch if n primes haven’t been calculated and cached yet, since this function is intended to be used to calculate primes much bigger than n.

NOTE: Some define primorial as the product of all of the prime numbers less than or equal to n. In this case, the implementation would be

@primes.take_while {|p| p <= n }.inject :*

Raises:

  • (IndexError)


97
98
99
100
# File 'lib/funding_primer/prime.rb', line 97

def primorial(n)
  raise IndexError if n > @primes.length
  @primes.take(n).inject(:*)
end