Skip to content

Instantly share code, notes, and snippets.

@johncrisostomo
Created July 13, 2011 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johncrisostomo/1080248 to your computer and use it in GitHub Desktop.
Save johncrisostomo/1080248 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes in Ruby
print "Enter maximum number : "
n = gets.to_i
primes = (2..n).to_a
i = 0
while primes[i]**2 < primes.last
prime = primes[i]
primes = primes.select { |num| num == prime || num % prime != 0 }
i += 1
end
primes.each do |number|
puts number
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment