I wrote an implementation of the Sieve of Atkin in C++ to calculate large primes. It works perfectly fine for finding up to the 10^8th prime, but when trying to find the 10^9th prime number, I get an incorrect result.
It looks like I'm dealing with an integer overflow, but I can't figure out how this is possible. I'm using uint64_t
everywhere, and the desired result (22801763489) lies between 2^34 and 2^35.
The result I'm getting is 1326927009
.
Here is the relevant code:
uint64_t getPrimesAtkin(uint64_t Nn)
{
(...)
}
int main(int argc, char** argv) {
for (int a = 7; a < 10; a += 2)
{
clock_t begin = clock();
uint64_t prime = getPrimesAtkin(pow(10,a));
clock_t end = clock();
printf("p(10^%d)=%12d t=%4.3f\n",a,prime, double(end - begin) / CLOCKS_PER_SEC);
}
}