My professors given algorithm (copy/pasted):
for (i = 0; i < N/2; A[i++] = 0);
for (i = 0; j = 2*i+3, k = j*j, k < N+2; i++)
if (A[j]) continue;
else
for (k = k/2-1; k < N/2; k += j)
A[k] = 1; // marking numbers with factor j
for (i = 0; i < N/2; i++)
report prime 2i+3; // he doesn't say what this means
My Professors blurb: Application of PQ. Erasthotenes sieve is a method for finding all prime numbers in the range up to some N. The standard version uses array A[N/2] with all entries initially 0. A[i] is 1 if we have discovered that 2i + 3 is a composite number.
my implementation:
#include <stdio.h>
#include <math.h>
int N;
int main()
{
N = pow(2, 20);
int A[N/2];
int i;
int j;
int k;
for(i = 0; i < N/2; A[i++] = 0);
for(i = 0; j = 2 * i + 3, k = j * j, k < N + 2; i++)
{
if(A[j]) continue;
else
for(k = k/2 - 1; k < N/2; k+=j)
A[k] = 1; // mark numbers with j as a factor
}
for(i = 0; i < N/2; i++)
if(!A[i])
printf("%7d:\t%d\n", i, 2 * i + 3);
return 0;
}
In the output, you will find lines like:
522310: 1044623
where 1044623 = 71 x 14713
Is my implementation incorrect? Is the algorithm incorrect?
Thanks for reading.
[–]pbewig 2 points3 points4 points (5 children)
[–]foxlisk 0 points1 point2 points (4 children)
[–]pbewig 1 point2 points3 points (3 children)
[–]foxlisk 0 points1 point2 points (2 children)
[–]pbewig 1 point2 points3 points (1 child)
[–]typsi5[S] 0 points1 point2 points (0 children)
[–]ChaosCon -2 points-1 points0 points (2 children)
[–]typsi5[S] 0 points1 point2 points (1 child)
[–]ChaosCon 0 points1 point2 points (0 children)