Created
July 13, 2011 12:56
-
-
Save johncrisostomo/1080246 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes in Ruby
This file contains hidden or 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
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