Program
public class Prime {
final static int MAX = 10000 ;
static boolean prime[] = new boolean [10001] ;
static void primeNumberPreCalculation()
{
for(int i = 0; i <= MAX; i++)
prime[i] = true ;
prime[1] = false;
for (int p = 2; p <= MAX; p++) {
// If prime[p] is not changed, then it is a prime
if (prime[p] == true) {
// Set all multiples of p to non-prime
for (int i = p*2 ; i <= MAX; i += p)
prime[i] = false;
}
}
}
public static void main(String args[])
{
// method call
primeNumberPreCalculation();
int arr[] = new int[5000];
int k =1;
for (int p = 1; p <= MAX; p++) {
if(prime[p] == true){
arr[k++] = p;
}
}
System.out.println("10th prime number = "+arr[10]);
System.out.println("50th prime number = "+arr[50]);
}
}
output
10th prime number = 2950th prime number = 229
No comments:
Post a Comment