ruby-changes:38721
From: marcandre <ko1@a...>
Date: Tue, 9 Jun 2015 12:32:23 +0900 (JST)
Subject: [ruby-changes:38721] marcandRe: r50802 (trunk): * lib/prime.rb: Simplify and optimize EratosthenesSieve
marcandre 2015-06-09 12:32:11 +0900 (Tue, 09 Jun 2015) New Revision: 50802 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=50802 Log: * lib/prime.rb: Simplify and optimize EratosthenesSieve Modified files: trunk/lib/prime.rb Index: lib/prime.rb =================================================================== --- lib/prime.rb (revision 50801) +++ lib/prime.rb (revision 50802) @@ -424,10 +424,11 @@ class Prime https://github.com/ruby/ruby/blob/trunk/lib/prime.rb#L424 segment_max = [segment_min + max_segment_size, max_cached_prime * 2].min root = Integer(Math.sqrt(segment_max).floor) - sieving_primes = @primes[1 .. -1].take_while { |prime| prime <= root } - segment = ((segment_min + 1) .. segment_max).step(2).to_a - sieving_primes.each do |prime| + + (1..Float::INFINITY).each do |sieving| + prime = @primes[sieving] + break if prime > root composite_index = (-(segment_min + 1 + prime) / 2) % prime while composite_index < segment.size do segment[composite_index] = nil -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/