Help With an Assignment
import java.io.*;
public class Lab13A100
{
public static void main(String args[])throws IOException
{
System.out.println("\nLAB13A 100 POINT VERSION\n");
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the primes upper bound: ");
final int MAX = Integer.parseInt(input.readLine());
boolean primes[] = new boolean[MAX+1];
System.out.println("Computing Prime Numbers");
System.out.println("");
computePrimes(primes);
System.out.println("Primes Between 1 and " +MAX);
System.out.println("");
displayPrimes(primes);
}
public static void computePrimes(boolean primes[])// computes prime numbers
{
int max = MAX
//sets all numbers to primes
for(int i = 0; i <= max; i++)
primes[i] = true;
primes[0] = primes[1] = false;
// To compute all primes less than MAX, we need to rule out
// multiples of all integers less than the square root of max.
int n = (int) Math.ceil(Math.sqrt(max));
//For each integer i from 0 to n:
//If i is a prime, then none of its multiples are primes, so
//indicate this in the array by looping through its multiples
// noting they are not prime.
for(int i = 0; i <= n; i++)
{
if (primes[i])
for(int j = 2*i; j <= max; j = j + i)
primes[j] = false;
}
}
public static void displayPrimes(boolean primes[])
{
int max = MAX;
for(int i=0;i<=max;i++)
if (primes[i]==true)
System.out.print(i+" ");
}
}

