- 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 multiplesfactors of the square of every prime number \$(25, 49)\$
public class SixesSieve3
{//-----------------------------------------------------------------
public// static voidClass: main(String[] args) {SixesSieve.java
// maxNumber mustAuthor: be an evenAlex numberLieberman
// Purpose: Finds all prime numbers intup maxNumberto =a 1000;specified
// boolean[] isPrime = new boolean[maxNumber];
maxNumber (exclusive) and marks them as true in isPrime[2]the =boolean true;array.
//-----------------------------------------------------------------
public class SixesSieve { isPrime[3] = true;
public //static thevoid for-loopmain below(String[] loadsargs) the{ array
with potential primesint inmaxNumber a= manner1000000000;
boolean[] isPrime = new boolean //[maxNumber];
according to 6k+1,isPrime[2] 6k-1= true;
isPrime[3] = true;
for (int possiblePrime1i = 5, possiblePrime2j = 7;
possiblePrime1i < maxNumber; possiblePrime1 += 6i+=6, possiblePrime2 += 6j+=6) {
isPrime[possiblePrime1]isPrime[i] = true;
if (possiblePrime2j < maxNumber) {
isPrime[possiblePrime2]isPrime[j] = true;
}
}
// the for-loop below removes all non-primes from the equation
for (int primea = 5; prime * primea*a <= maxNumber; prime += 2a+=2) {
// the if-statement below skips the testing of all non-prime numbers
if (isPrime[prime]isPrime[a]) {
for (int secondPrimeb = primea, squarez = prime * prime; secondPrime
*a*a; primeb*a <= maxNumber; ) {
// if the product of 'prime' and 'secondPrime' equals
// 'square'
if (secondPrime * primeb*a == squarez && squarez < maxNumber / 2) {
// searches for all multiples of square that may exist in
// array
while (true) {
if (squarez <= maxNumber) {
isPrime[square]isPrime[z] = false;
squarez +== z+(prime * prime * 2a*a*2);
}
else {
secondPrime += 2;
{b+=2; break;
}
}
}
// if 'prime' and 'secondPrime' are not equal to square
// then search for all multiples of 'prime' by 'secondPrime'
else {
isPrime[secondPrime * prime]isPrime[b*a] = false;
secondPrime += 2;b+=2;
while (!isPrime[secondPrime]isPrime[b]) {
{b+=2;} secondPrime += 2;
}
}
}
}
}
for (int printPrimesi = 0; printPrimesi < maxNumber; printPrimes++i++) {
if (isPrime[printPrimes]isPrime[i]) {
System.out.printlnprint(printPrimesi + " ");
}
}
}
}