programming-examples/ruby/Algorithms/seive_of_erastenous.rb

8 lines
159 B
Ruby
Raw Normal View History

2019-11-15 12:59:38 +01:00
def eratosthenes(n)
nums = [nil, nil, *2..n]
(2..Math.sqrt(n)).each do |i|
(i**2..n).step(i) { |m| nums[m] = nil } if nums[i]
end
nums.compact
end