You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
8 lines
159 B
Ruby
8 lines
159 B
Ruby
5 years ago
|
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
|