1048576 should be around 30 seconds for ruby 1.9.3p327
Last active
December 18, 2015 22:49
-
-
Save tobstarr/5857373 to your computer and use it in GitHub Desktop.
Prime Tests
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
def highest_divisor(i) | |
j = 2 | |
while j <= Math.sqrt(i.to_f).ceil | |
if i % j == 0 | |
return j | |
end | |
j += 1 | |
end | |
return -1 | |
end | |
def is_prime(i) | |
return true if i < 4 | |
return highest_divisor(i) == -1 | |
end | |
started = Time.now | |
num = (ARGV.first || 1000000).to_i | |
cnt = 0 | |
num.times do |i| | |
if is_prime(i) | |
cnt += 1 | |
end | |
end | |
diff = Time.now - started | |
puts "%s\t%d\t%d\t%0.3f" % [RUBY_DESCRIPTION.split(" ", 3)[0,2].join("-"), num, cnt, diff] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment