Created
February 1, 2012 03:25
-
-
Save timoteoponce/1714906 to your computer and use it in GitHub Desktop.
sieve_of_eratosthenes_0
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 ] = false | |
end | |
init = 2 | |
while init <= limit do | |
prime = init | |
while prime <= limit do | |
if prime > init then | |
list[ prime ] = true | |
end | |
prime += init | |
end | |
found = false | |
(init..limit ).each do |i| | |
if i > init and list[i] == false then | |
init = i | |
found = true | |
break | |
end | |
end | |
unless found then | |
init = limit + 1 | |
end | |
end | |
print_primes list | |
end | |
def print_primes( list ) | |
list.each do |i , value| | |
if value == false 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