Skip to content

Instantly share code, notes, and snippets.

@hannestyden
Created February 25, 2011 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hannestyden/844494 to your computer and use it in GitHub Desktop.
Save hannestyden/844494 to your computer and use it in GitHub Desktop.
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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment