Skip to content

Instantly share code, notes, and snippets.

@tobstarr
Last active December 18, 2015 22:49
Show Gist options
  • Save tobstarr/5857373 to your computer and use it in GitHub Desktop.
Save tobstarr/5857373 to your computer and use it in GitHub Desktop.
Prime Tests

1048576 should be around 30 seconds for ruby 1.9.3p327

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