java 获取第n个素数
//find n'th prime number import java.util.Scanner; public class Numbers { public static void main(String[] args) { int n, 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 (int i = 2; i <= Math.sqrt(num); i++) { if (num % i == 0) return false; } return true; } } OUTPUT: Enter the value of n: 5 The 5th prime number is 11