Skip to content

Instantly share code, notes, and snippets.

@jpignata
Created December 6, 2009 22:48
Show Gist options
  • Save jpignata/250480 to your computer and use it in GitHub Desktop.
Save jpignata/250480 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
Infinity = 1.0/0
@start = Time.now
@primes = []
print "PRIMEZ! "
trap("INT") do
duration = (Time.now - @start).round
puts
puts "=" * 80
puts "Calculated #{@primes.size} primes in #{duration} secs"
throw :exit
end
catch(:exit) do
(2..Infinity).each do |number|
catch(:composite) do
2.upto(Math.sqrt(number)).each { |divisor| throw :composite if number % divisor == 0 }
@primes << number
print "#{number} "
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment