Created
May 30, 2009 18:14
-
-
Save sarahhodne/120589 to your computer and use it in GitHub Desktop.
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
sine:~% ruby primetester.rb | |
Rehearsal ------------------------------------ | |
2.980000 1.160000 4.140000 ( 4.164864) | |
--------------------------- total: 4.140000sec | |
Primesum: 496165411 | |
user system total real | |
2.940000 1.180000 4.120000 ( 4.139064) | |
Primesum: 496165411 |
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
primesbool = Array.new(1000000) | |
primesum = 0 | |
primesbool[0] = true | |
for i in 1..(Math.sqrt(1000000.to_f)) | |
unless primesbool[i] | |
j = (2 * (i+1))-1 | |
loop do | |
break unless j < 1000000 | |
primesbool[j] = true | |
j += i+1 | |
end | |
end | |
end | |
j = 0 | |
i = 0 | |
loop do | |
break unless i < 1000000 && j < 10000 | |
unless primesbool[i] | |
primesum += i+1 | |
j += 1 | |
end | |
i += 1 | |
end | |
puts "Primesum: #{primesum}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment