class PrimeSieve { /* * This program prints all primes <= n where n is the number entered by * the user as a command line argument, 10 primes per line. * The primes are found using the Sieve of Eratosthenes. */ public static void main(String[] args) { int n = Integer.parseInt(args[0]); boolean [] notPrime = new boolean[n+1]; notPrime[0] = notPrime[1] = true; for (int d = 2; d * d <= n; d++) { if (!notPrime[d]) { for (int k = d+d; k <= n; k += d) notPrime[k] = true; } } int count = 0; for (int i = 2; i <= n; i++) { if (!notPrime[i]) { count++; System.out.printf("%6d", i); if (count % 10 == 0) System.out.println(); } } System.out.println(); } }