Created
February 1, 2012 03:19
-
-
Save timoteoponce/1714886 to your computer and use it in GitHub Desktop.
sieve_of_eratosthenes
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 find_primes( limit ) | |
list = Hash.new | |
(2..limit).each do |i| | |
list[ i ] = true | |
end | |
init = 2 | |
while init <= limit/2 do | |
if list[init] then | |
index = init * 2 | |
while index <= limit do | |
list[ index ] = false | |
index += init | |
end | |
end | |
init += 1 | |
end | |
print_primes list | |
end | |
def print_primes( list ) | |
list.each do |i , value| | |
if value then | |
puts "#{i}" | |
end | |
end | |
end | |
find_primes( 30 ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment