Skip to content

Instantly share code, notes, and snippets.

@timoteoponce
Created February 1, 2012 03:25
Show Gist options
  • Save timoteoponce/1714906 to your computer and use it in GitHub Desktop.
Save timoteoponce/1714906 to your computer and use it in GitHub Desktop.
sieve_of_eratosthenes_0
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