Created
February 2, 2012 06:45
-
-
Save IvanTorresEdge/1722053 to your computer and use it in GitHub Desktop.
Find prime numbers using Sieve of Eratosthenes algorithm (Ruby)
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
#!/usr/bin/env ruby | |
n = 2..1000 | |
o = [] | |
p = [] | |
for i in n | |
next if o.include? i | |
ii = i * 2 | |
while ii <= n.last do | |
o << ii | |
ii = ii + i | |
end | |
p << i unless o.include?(i) | |
end | |
puts p.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes