Prime

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

public class Prime {

public static void main(String[] args) {


int n = Integer.parseInt(args[0]);
int primesFound=0;
if (n==1) {System.out.println("1st prime is: "+2);return;}
if (n==2) {System.out.println("2nd prime is: "+3);return;}
int current=4;
int primesRemain=n-2;
while (primesRemain > 0) {
current++;
if (isPrime(current)) {
primesRemain--;
}
}
System.out.println(n+"th prime is: "+current);
}
public static boolean isPrime(int n) {
double sqrt = Math.sqrt(n);
for(int divisor=2; divisor<=sqrt; divisor++) {
if ((n%divisor)==0) {
//not prime
return false;
}
}
return true;
}
}

You might also like