1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/matrix.rb: Simplify and optimize EratosthenesSieve

based on patch by Ajay Kumar. [Fixes GH-921]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50800 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
marcandre 2015-06-09 02:45:31 +00:00
parent 0d3591f250
commit 3ed18fa817
2 changed files with 7 additions and 5 deletions

View file

@ -1,3 +1,8 @@
Tue Jun 9 11:45:00 2015 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
* 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@atdot.net>
* gc.c (obj_info): print method id for T_IMEMO/ment.

View file

@ -425,13 +425,10 @@ class Prime
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