Created
February 25, 2011 21:06
-
-
Save hannestyden/844494 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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