Skip to content

Instantly share code, notes, and snippets.

@IsuraManchanayake
Last active December 10, 2016 16:53
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/f73452c17bb711af956d7a766fae5abe to your computer and use it in GitHub Desktop.
Save IsuraManchanayake/f73452c17bb711af956d7a766fae5abe to your computer and use it in GitHub Desktop.
primality test using primality_sqrt_bound_coprime_30(int64_t)
L = 100000000 : R = 100100000
test 1 duration : 0.281 s
test 2 duration : 0.225 s
test 3 duration : 0.222 s
test 4 duration : 0.227 s
test 5 duration : 0.226 s
test 6 duration : 0.223 s
test 7 duration : 0.223 s
test 8 duration : 0.225 s
test 9 duration : 0.224 s
test 10 duration : 0.225 s
average of 10 test(s) : 0.2301 s
#include <cmath>
bool primality_sqrt_bound_coprime_30(int64_t a) {
if(a == 2 || a == 3 || a == 5) return true;
if(a % 2 == 0 || a % 3 == 0 || a % 5 == 0) return false;
double lim = sqrt(a);
/* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 ...
* \ / \ / \ / \ /\
* --------------------------- ------------------- --------- --------------- ----...
* 6 4 2 4 ...
*
* modulo distances between numbers coprime to 30 are stored in s[]
*/
int s[] = {6, 4, 2, 4, 2, 4, 6, 2};
for(int i = 7, t = 1; i <= lim; i += s[t], t = ++t & 7)
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