I have made my own implementation of the Sieve of Atkin in C++, it generates primes fine until about 860,000,000. Around there and higher the program begins to return several composites, or so I think. I have a variable inside the program the counts the number of primes found, and at ~860,000,000 the count is more than it should be. I checked my count against a similar program for the Sieve of Eratosthenes, and several internet resources. I am new to programming so it is likely a stupid mistake.
Anyway, here it is:
#include <iostream>
#include <math.h>
#include <time.h>
int main(int argc, const char * argv[])
{
long double limit;
unsigned long long int term,term2,x,y,multiple,count=2;
printf("Limit: ");
scanf("%Lf",&limit);
int root=sqrt(limit);
int *numbers=(int*)calloc(limit+1, sizeof(int));
clock_t time;
//Starts Stopwatch
time=clock();
for (x=1; x<root; x++) {
for (y=1; y<root; y++) {
term2=4*x*x+y*y;
if ((term2<=limit) && (term2%12==1 || term2%12==5)){
numbers[term2]=!numbers[term2];
}
term2=3*x*x+y*y;
if ((term2<=limit) && (term2%12==7)) {
numbers[term2]=!numbers[term2];
}
term2=3*x*x-y*y;
if ((term2<=limit) && (x>y) && (term2%12==11)) {
numbers[term2]=!numbers[term2];
}
}
}
//Print 2,3
printf("2 3 ");
//Sieves Non-Primes That Managed to Get Through
for (term=5; term<=root; term++) {
if (numbers[term]==true) {
multiple=1;
while (term*term*multiple<limit){
numbers[term*term*multiple]=false;
multiple++;
}
}
}
time=clock()-time;
for (term=5; term<limit; term++) {
if (numbers[term]==true) {
printf("%llu ",term);
count++;
}
}
printf("\nFound %llu Primes Between 1 & %Lf in %lu Nanoseconds\n",count,limit,time);
return 0;
}
From wikipedia ,
for (x, y) in [1, √limit] × [1, √limit]: is your problem .
You have used :
Instead use :
Hope this helps !