- Feb 27, 2004
- 22
- 0
- 0
From 0 to n, we multiply X by Y and store the results in an array. If X and Y are the range of integers from 0 to n, the result will give prime numbers from 0 to n.
Optimizations:
X and Y may only be odd numbers. The product of an even number and an integer may only be even. Even numbers are not prime.
Y may never be less than X, as X * Y == Y * X.
X may never be greater than sqrt(n) and Y may never be greater than n/2, as results greater than n are not needed.
How could the event (X * Y > n) be predicted?
Pseudo code:
The section in bold increases calculation speed a bit. Why is this? When/which other numbers may be divided to increase performance?
Optimizations:
X and Y may only be odd numbers. The product of an even number and an integer may only be even. Even numbers are not prime.
Y may never be less than X, as X * Y == Y * X.
X may never be greater than sqrt(n) and Y may never be greater than n/2, as results greater than n are not needed.
How could the event (X * Y > n) be predicted?
Pseudo code:
while(X<=sqrt(n))
{
i=n/X;
while(Y<=i)
{
q[X*Y]=1;
Y+=2;
}
X+=2;
Y=X;
if(X%3==0)
{X+=2;}
}
The section in bold increases calculation speed a bit. Why is this? When/which other numbers may be divided to increase performance?