Q:

Does this program use the Sieve of Eratosthenes?

I have an assignment as follows,
:  Use the Sieve of Eratosthenes to locate and print out all prime numbers from 1 to 1000.
Follow a procedure similar to this:

Write down, in order, all number to be considered.
Cross out 1, since it is not considered prime.
Go to the next number not crossed out; leave it, but cross out all multiples of that number.
Repeat step 3 until you pas the number which is half of the largest number considered. At that point, all numbers not crossed out are the desired primes.

Your algorithm may vary slightly from the one above but speed is important.
I wrote this program out using the knowledge I have of math and arrays however as I was researching Sieve, I had no clue if this was the method.
public class PrimeSieve
 {

     public static void main( String[] args) 
     { 
         int max=1000;
         calcPrimes( max ); 
        } 

        public static void calcPrimes( int max ) 
        { 
            // each boolean value indicates whether corresponding index 
            // position is composite (non-prime) 
            boolean[] array = new boolean[max +1 ]; 

            // mark composites as true 
             for (int i = 2; i <= (int) Math.sqrt( max ); i++) 
             {
                 for (int j = i*i; j <= max; j += i) array [j ] = true; 
                 {

             // print indexes with corresponding false values 
                    for (int k = 2;k <= max; k++) 
                    {

                        if ( !array[ k ] ) 
                        System.out.format( k + "\n" ); 
                    }

                }
            }
      } 
} 

Any help would be nice!

A:

The problem is that you do not complete the process of marking off composites before printing out the results, possibly because your loops are nested in a confused sort of way.
public static void calcPrimes(int max) {
    // each boolean value indicates whether corresponding index
    // position is composite (non-prime)
    boolean[] array = new boolean[max + 1];

    // mark composites as true
    for (int i = 2; i <= (int) Math.sqrt(max); i++) {
        for (int j = i*i; j <= max; j += i) array[j] = true;
    }

    // print indexes with corresponding false values
    for (int k = 2; k <= max; k++) {
        if (!array[k]) System.out.println(k);
    }
}

In this example, I have moved the code to print the primes outside of the loop that performs the sieve.

