java 求第n小的质数
1 package a小米; 2 3 import java.util.Scanner; 4 5 public class prime { 6 public static boolean isPrime(int n){ 7 for(int i=2;i<=Math.sqrt(n);i++) 8 { 9 if(n%i==0){ 10 return false; 11 } 12 } 13 return true; 14 } 15 16 public static void main(String[] args) { 17 18 Scanner in=new Scanner(System.in); 19 while(in.hasNext()){ 20 int n=in.nextInt(); 21 int count=0; 22 int i=0; 23 for( i=2;i<=1000;i++){ 24 if(isPrime(i)){ 25 count++; 26 } 27 if(count==n) break; 28 } 29 System.out.println(i); 30 31 } 32 } 33 34 }