Created
May 14, 2012 20:31
-
-
Save xoebus/2696591 to your computer and use it in GitHub Desktop.
Prime Sieve
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
#!/usr/bin/env ruby | |
def print_primes(n) | |
return if (n < 2) | |
primes = [] | |
(2..n).each do |i| | |
is_prime = primes.none? { |prime| i % prime == 0} | |
primes << i if is_prime | |
end | |
puts primes.join "," | |
end | |
filename = ARGV[0] | |
file = File.open(filename, "r") | |
file.each_line do |line| | |
line.chomp! | |
print_primes(line.to_i) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment