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


447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/epitools/core_ext/numbers.rb', line 447

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