euler/euler010.rb

12 lines
371 B
Ruby

#an implementation of the sieve of Eratosthenes
def improved_sieve(n)
eSieve = (2..n).to_a
(2..Math.sqrt(n)).each do |i|
next unless eSieve[i]
(i*i).step(n, i) { |j| eSieve[j] = nil } #steps by increments of i up to n starting at i*i and sets those elements to nil, marking them for removal
end
eSieve.compact
end
puts improved_sieve(2000000).inject(:+)