Created
November 12, 2013 18:47
-
-
Save dougyoung/7436482 to your computer and use it in GitHub Desktop.
Prime number sieve
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 prime_number_sieve(n) | |
primes = [] | |
integers_up_to_n = (2..n).to_a | |
integers_up_to_n.each do |i| | |
next if i.nil? | |
primes << i | |
not_prime = i + i | |
while (not_prime <= n) do | |
integers_up_to_n[not_prime - 2] = nil | |
not_prime = not_prime + i | |
end | |
end | |
primes | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment