Created
December 6, 2009 22:48
-
-
Save jpignata/250480 to your computer and use it in GitHub Desktop.
This file contains 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
This file contains 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 | |
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