java求第n个质数
import java.util.Scanner;
publicclass Numbers {
public staticvoid main(String[] args) {
intn, count =0, i =2;
Scanner ip =new Scanner(System.in);
System.out.print("Enter the value of n: ");
n = ip.nextInt();
while(count <= n) {
if (isPrime(i)) {
count++;
if(count == n)
System.out.println("The "+ n +"th prime number is "+ i);
}
i++;
}
ip.close();
}
private static boolean isPrime(int num) {
for(inti =2; i <= Math.sqrt(num); i++) {
if(num % i ==0)
returnfalse;
}
returntrue;
}
}
网友评论