Skip to content

Instantly share code, notes, and snippets.

@IsuraManchanayake
Last active December 10, 2016 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IsuraManchanayake/fb5aab8e4f26e5c3fac51cd002455b29 to your computer and use it in GitHub Desktop.
Save IsuraManchanayake/fb5aab8e4f26e5c3fac51cd002455b29 to your computer and use it in GitHub Desktop.
primality test using primality_sqrt_bound_coprime_2(int64_t)
L = 100000000 : R = 100100000
test 1 duration : 0.418 s
test 2 duration : 0.393 s
test 3 duration : 0.397 s
test 4 duration : 0.406 s
test 5 duration : 0.379 s
test 6 duration : 0.381 s
test 7 duration : 0.383 s
test 8 duration : 0.383 s
test 9 duration : 0.376 s
test 10 duration : 0.38 s
average of 10 test(s) : 0.3896 s
#include <cmath>
bool primality_sqrt_bound_coprime_2(int64_t a) {
if(a % 2 == 0) return a == 2;
double lim = sqrt(a);
for(int i = 3; i <= lim; i += 2)
if(a % i == 0)
return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment