public class eratosthenes { public static void main(String a[]) { // step 0: define max int max = 200; // step 1: set up an array of max booleans boolean sieve[] = new boolean[max]; // step 2: set all booleans (from index 2 to max -1) to false (i.e. not crossed) int i; for (i = 2; i < max; i++) { sieve[i] = false; } // step 3: run the algortihm of the sieve of Eratosthenes // (see http://en.wikipedia.org/wiki/Sieve_of_eratosthenes ) for (i = 2; i < max; i++) { // cross every i-th element starting with the (i+i)-th element // (no need to go on if the i-th element is already crossed if (sieve[i] == false) { int m = i + i; while (m < max) { sieve[m] = true; m += i; } } } // step 4: print out all i for which sieve[i] is still false for (i = 2; i < max; i++) { if (sieve[i] == false) { System.out.println(i); } } } }