Class: Prime

Inherits:
Object show all
Defined in:
lib/epitools/core_ext/numbers.rb

Instance Method Summary collapse

Instance Method Details

#[](range) ⇒ Object

Return an array of prime numbers within the specified range. (It still has to generate all the primes less than the lower bound, so, yeah… be warned.)



496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/epitools/core_ext/numbers.rb', line 496

def [](range)
  ubound    = range.end
  lbound    = range.begin
  ubound   -= 1 if range.exclude_end?
  generator = each(ubound)
  n         = nil

  loop do
    break if (n = generator.succ) >= lbound
  end

  [n, *generator.to_a]
end