// This program takes in an integer (maxNum) and outputs 
// every number from 1 to n that is prime.
// Algorithm:
// INPUT: An integer >= 1 (maxNum)
// PROCESS:
// 1. Loop from 1 to maxNum (current number: checkNum)
//    a) Loop from 2 to the square root of the current number.
//       If the current number is divisible by any of these numbers,
//       then it is not prime.
// OUTPUT:
// 1. Tell user which numbers are prime.
// 2. Write this information out to a file.

// You don't need to know what this line does (yet!).
// It enables us to write information out to file, later in the program.
import java.io.*;

public class PrimeNumbers
{
  public static void main(String args[])
  {
      // Declare variables
      int checkNum;
	
      // Prompt user for maximum value
      System.out.println("This program will calculate all of the prime " +
			 "numbers from 1 to a number you specify. " +
			 "Please enter a positive number:");
      int maxNum = SavitchIn.readInt();
      // Check that the input is valid
      if (maxNum < 1)
      {
	  System.out.println("Invalid number: " + maxNum + ", exiting.");
	  System.exit(1);  // exit(1) signals that an error occurred.
      }
      
      // Echo value back to user
      System.out.println("Now calculating all of the primes between 1 and " +
			 maxNum);

      // We want to write out all of the primes to a file (primes.txt)
      // in the format: (one number per line)
      // <1 if prime, 0 if not>
      // (You don't need to understand this file part yet.
      //  But have a look - it will be coming up later.)
      try
      {
	  // This line opens the file.
	  PrintWriter outfile = new PrintWriter(new FileOutputStream("primes.txt"));

	  // PROCESS: Step 1 is to loop over all possible numbers
	  // from 1 to the max
	  for (checkNum=1; checkNum <= maxNum; checkNum++)
	  {
	      // Store whether checkNum is prime here.  Initialize it to true.
	      boolean isPrime = true;

	      // Calculate the square root.  We'll store this in an int
	      // because our divisors have to be integers and this provides
	      // the largest integer that is <= the true square root.
	      int squareRoot = (int)Math.sqrt(checkNum);

	      // PROCESS: (Your implementation of part 1a) goes here)

	      
	  
	      // If isPrime is still true, then checkNum is prime.
	      if (isPrime)
	      {
		  System.out.println(checkNum + " is prime.");
		  // Write this out to a file.
		  outfile.println(1);
	      }
	      else
	      {    
		  outfile.println(0);
	      }
	  }
	  // Now that we're done with the file, we must close it.
	  outfile.close();
      }
      // Again, you don't have to understand this part.
      // This is where you tell Java how to respond if there is a
      // problem when you try to open or write to a file.
      catch(IOException e)
      {
	  System.out.println("Could not create output file.");
	  System.exit(1);
      }
      
  }
}

 
