/*
class Prime
Input
The maximum integer upto which primes have to be generated
Output
The prime integers each separated by a white space and ending with
a new line character
Algorithm
1) Instantiate a list of max integers with initial value 0
2) Start at the first number say "i" on the list
which has not yet been considered(should not start from 1)
3) Check if the value of the ith element in the list is 1
if it is 1 increment i otherwise proceed to step 4
4) Set all numbers of the list which are multiples of i to 1
Increment i and go back to step 2
*/
class Prime
{
public void computePrime(int max)
{
int [] intArr = new int[max+1];//Array of integers. For convenience we are making an array of max + 1 instead of max
for(int i = 2; i <= max; i++)
{
if (intArr[i] != 1)
{
System.out.println(i);
for(int j=2*i; j <= max; j+=i)
intArr[j] = 1;
}
}
}
public static void main(
String [] args)
{
int max = 50;// Max num upto which primes have to be generated
Prime prime = new Prime();
prime.computePrime(max);
}
}
Try this and solve few errors if any