import java.util.*; import static java.lang.Math.*; public class NaivePrimes { public static void main (String...args) { final int maxValue = args.length < 1 ? 1000000 : (int)Double.parseDouble(args[0]); final int maxSqrt = (int)ceil(sqrt(maxValue)); System.out.println("Creating bit set"); BitSet primeBits = new BitSet(maxValue); primeBits.set(0, maxValue, true); System.out.println("Sieving"); /* * Sieve of Eratosthenes * primeBits is a simple BitSet where all bit is an integer * and we mark composite numbers as false */ primeBits.set(0, false); // You don't actually need this, just primeBits.set(1, false); // remindig you that 2 is the smallest prime for (int p = 2; p < maxValue; p++) { if (primeBits.get(p)) { // These are going to be the multiples of P if it is a prime if (p < maxSqrt) for (int pMultiply = p * p; pMultiply < maxValue; pMultiply += p) primeBits.set(pMultiply, false); WheelPrimes.printPrime(p); } } System.out.println("found " + WheelPrimes.primeCount + " primes below " + maxValue + "."); } }