Skip to content

Instantly share code, notes, and snippets.

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