Class: Range

Inherits:
Object
  • Object
show all
Defined in:
lib/include/prime.rb

Instance Method Summary collapse

Instance Method Details

#each_primeObject



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# File 'lib/include/prime.rb', line 537

def each_prime()
  return Enumerator.new(self, :each_prime) unless block_given?

  primes = Abst.primes_list

  max = last + (exclude_end? ? -1 : 0)
  if (first <= primes.last)
    a = Bisect.bisect_left(primes, first)
    a.upto(primes.size - 1) do |i|
      return if max < primes[i]
      yield primes[i]
    end
  end

  s = primes.last + 2
  s.step(max, 2) do |i|
    yield i if prime?(i)
  end
end