I am posting this question for input on comparisons between other sieving methods and opinions on what I have discovered. I believe this might be the fastest (singular, non-threaded) array implementation of prime sieving to date, correct me if I am wrong.
My rudimentary benchmark tests stand as follows:
My optimized code: 4 seconds N = 1,000,000,000
My original code: 7 seconds N = 1,000,000,000
Standard Sieve-of-Eratosthenes: 16 seconds N = 1,000,000,000
I have seen PrimeSieve and as I understand it utilizes better means to compute faster, but as it seems - it must dump the data somewhere and retrieve it to utilize it again, because it cycles the computations through the cache only.
My original thread with same pseudo-code is here, however, my original Java code did not match the pseudo-code I developed (but essentially performed similar functions at a lesser speed and easier complexity to code).
Pseudo-code as follows:
- Initialize list with initial values \$(2, 3)\$.
- Use the formula \$6k ± 1\$ where \$k = 1\$ and must increment by \$1\$. Store values to list.
- Compute step two up to a product \$n\$, where \$n > 7\$ if \$k = 1\$.
- Starting with \$x = 5\$, remove all products of \$x \times y\$ (up to \$n\$),
where \$y\$ starts at \$x\$ and
increments to the next number in the list as \$x\$ remains the same. - Repeat step four by assigning \$x\$ to the next number in the list. Stop when \$x \times y > n\$.
- Final step: We must remove all factors of the square of every prime number \$(25, 49)\$
//-----------------------------------------------------------------
// Class: SixesSieve.java
// Author: Alex Lieberman
// Purpose: Finds all prime numbers up to a specified
// maxNumber (exclusive) and marks them as true in the boolean array.
//-----------------------------------------------------------------
public class SixesSieve {
public static void main (String[] args) {
int maxNumber = 1000000000;
boolean[] isPrime = new boolean [maxNumber];
isPrime[2] = true;
isPrime[3] = true;
for (int i = 5, j = 7; i < maxNumber; i+=6, j+=6) {
isPrime[i] = true;
if (j < maxNumber)
isPrime[j] = true;
}
for (int a = 5; a*a <= maxNumber; a+=2) {
if (isPrime[a]) {
for (int b = a, z = a*a; b*a <= maxNumber; ) {
if(b*a == z && z < maxNumber/2) {
while (true){
if (z <= maxNumber) {
isPrime[z] = false;
z = z+(a*a*2);
}
else
{b+=2; break;}
}
}
else {
isPrime[b*a] = false;
b+=2;
while (!isPrime[b])
{b+=2;}
}
}
}
}
for (int i = 0; i < maxNumber; i++) {
if (isPrime[i])
System.out.print(i + " ");
}
}
}