ruby-changes:38719
From: marcandre <ko1@a...>
Date: Tue, 9 Jun 2015 11:45:46 +0900 (JST)
Subject: [ruby-changes:38719] marcandRe: r50800 (trunk): * lib/matrix.rb: Simplify and optimize EratosthenesSieve
marcandre 2015-06-09 11:45:31 +0900 (Tue, 09 Jun 2015) New Revision: 50800 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=50800 Log: * lib/matrix.rb: Simplify and optimize EratosthenesSieve based on patch by Ajay Kumar. [Fixes GH-921] Modified files: trunk/ChangeLog trunk/lib/prime.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 50799) +++ ChangeLog (revision 50800) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Tue Jun 9 11:45:00 2015 Marc-Andre Lafortune <ruby-core@m...> + + * lib/matrix.rb: Simplify and optimize EratosthenesSieve + based on patch by Ajay Kumar. [Fixes GH-921] + Mon Jun 8 05:09:58 2015 Koichi Sasada <ko1@a...> * gc.c (obj_info): print method id for T_IMEMO/ment. Index: lib/prime.rb =================================================================== --- lib/prime.rb (revision 50799) +++ lib/prime.rb (revision 50800) @@ -425,13 +425,10 @@ class Prime https://github.com/ruby/ruby/blob/trunk/lib/prime.rb#L425 root = Integer(Math.sqrt(segment_max).floor) sieving_primes = @primes[1 .. -1].take_while { |prime| prime <= root } - offsets = Array.new(sieving_primes.size) do |i| - (-(segment_min + 1 + sieving_primes[i]) / 2) % sieving_primes[i] - end segment = ((segment_min + 1) .. segment_max).step(2).to_a - sieving_primes.each_with_index do |prime, index| - composite_index = offsets[index] + sieving_primes.each do |prime| + composite_index = (-(segment_min + 1 + prime) / 2) % prime while composite_index < segment.size do segment[composite_index] = nil composite_index += prime -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/