Skip to content

Instantly share code, notes, and snippets.

@xoebus
Created May 14, 2012 20:31
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 xoebus/2696591 to your computer and use it in GitHub Desktop.
Save xoebus/2696591 to your computer and use it in GitHub Desktop.
Prime Sieve
#!/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