/*
****************************************************
*  Kelli Wiseth
*  kelli@alameda-tech-lab.com
*  CIS 255AX
*  Program name: Sieve.java
*  Program Description: The Sieve class is an implementation
*    of the Sieve of Eratosthenes, which is a means of quickly finding all the
*    prime numbers in a specific range by sequentially excluding multiples of each
*    prime number in the given range. The Sieve class emulates the process by 
*    first, creating an array of 1000 booleans (array indices comprise our list of 
*    numbers), with elements  2-999 set to true. With the array initialized properly, we then
*    step through it using two nested for-loops.  The outer loop controls 
*    stepping through the list of numbers, while the inner loop sets to false 
*    all the multiples of each prime number from the list (outer loop) up to
*    the square root of the maximum number in the list. At the end of the process,
*    the remaining 'trues' are the prime numbers; a final for-loop prints these
*    in tabular format, 10 numbers to a row.
*  Assignment #1
*  7 February 2005
****************************************************
*/

import java.awt.*;
import javax.swing.*;

public class Sieve extends JApplet 
   {

   // initialize applet 
   
   public void init()
      {

      int idx = 0;
      int idxCtr = 0;
      boolean array[] = new boolean[1000];
      int printCtr = 0;
      
      JTextArea outputArea = new JTextArea();
      outputArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
      Container container = getContentPane();
      container.add(outputArea);
      String output = "Programmed by Kelli Wiseth\n";
      output += "\nPrimes between 2 and 999\n";

      /**************************************************************
       * Initialize all the elements of the array from 2 to 999 with 
       * value 'true'; we can skip 0 and 1 since we know they are not
       * primes.
       **************************************************************
       */ 
      
      for (idx = 2; idx < array.length; idx++)
          {
          array[idx] = true;
          }
         
     
    /*********************************************************
     * Now that we have an array from 2-999 with all trues, 
     * we can start with index number 2 and use the inner loop
     * to mark off [by setting to false] all multiples of that
     * number (2), and so on.  
     * The outer loop doesn't need to go beyond the square root
     * of the largest number in the range; the inner loop 
     * shouldn't go beyond the largest multiple of the outer-loop-
     * number that will safely fall within the range. 
     *********************************************************
     */
     
      for (idx = 2; idx <= (int)(Math.sqrt(array.length)); idx++)
          {
           if(array[idx])
             {
             for (idxCtr = idx; idxCtr <= (int)((array.length-1)/idx); idxCtr++)
                 {
            	  array[idx*idxCtr]=false;
                  } //closing brace for inner for-loop
             } // closing brace for if-true
           } // closing brace for outer for-loop
   
    /*********************************************************
     * Printing routine that finds all the 'trues' and prints
     * their index values in rows of 10 digits
     *********************************************************
     */
     
     for (idx = 2; idx < array.length; idx++)
         {
         if (array[idx])
            {
            printCtr++;
            if (printCtr%10==0)
                output += "\t" + idx + "\n";
            else
               output += "\t" + idx;
            }
          }

      outputArea.setText(output);

   } // end method init

} // end class Sieve