public
Created

  • Download Gist
sieve_of_euler.rb
Ruby
1 2 3 4 5 6 7 8 9 10
def sieve_of_euler(list)
if list.empty?
list
else
out = list - (list.map { |e| e * list.first })
[ out.first ] + sieve_of_euler(out[1..-1])
end
end
 
sieve_of_euler((2..30).to_a) # => [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.