Skip to content

Instantly share code, notes, and snippets.

@IsuraManchanayake
Last active December 10, 2016 12:37
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/5c359fff4b8ad1a00d7910467401891e to your computer and use it in GitHub Desktop.
Save IsuraManchanayake/5c359fff4b8ad1a00d7910467401891e to your computer and use it in GitHub Desktop.
primality test using primality_sqrt_bound_coprime_6(int64_t)
L = 100000000 : R = 100100000
test 1 duration : 0.278 s
test 2 duration : 0.269 s
test 3 duration : 0.283 s
test 4 duration : 0.258 s
test 5 duration : 0.263 s
test 6 duration : 0.272 s
test 7 duration : 0.262 s
test 8 duration : 0.265 s
test 9 duration : 0.281 s
test 10 duration : 0.274 s
average of 10 test(s) : 0.2705 s
#include <cmath>
bool primality_sqrt_bound_coprime_6(int64_t a) {
if(a == 2 || a == 3) return true;
if(a % 2 == 0 || a % 3 == 0) return false;
double lim = sqrt(a);
/* 0 1 2 3 4 5 0 1 ...
* \ / \ /
* ----------------- -------
* 4 2
* modulo distances between numbers coprime to 6 are stored in s[]
*/
int s[2] = {4, 2};
for(int i = 5, t = 1; i <= lim; i += s[t], t = ++t % 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