使用BigInteger中的isProblePrime方法进行判断
public class Main{
public static void searchNumbers(BigInteger m, int n){
int count=0,flag;
BigInteger temp=m,i;
while (count<n){
if (temp.isProbablePrime(1)){
count++;
System.out.println(temp.toString());
}
temp=temp.add(BigInteger.ONE);
}
}
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
while (input.hasNextLine()){
searchNumbers(input.nextBigInteger(),input.nextInt());
}
}
}
如果该 BigInteger 可能是素数,则返回 true ;如果它很明确是一个合数,则返回 false 。 参数 certainty 是对调用者愿意忍受的不确定性的度量:如果该数是素数的概率超过了 1 - 1/2*certainty方法,则该方法返回 true 。执行时间正比于参数确定性的值。
java中的isProbablePrime函数是针对BigInteger类的一个素数判断函数,它的实现原理其实并不复杂,只是要分许多情况讨论,要用到Miller-Rabin素数测试和Lucas-Lehmer测试,它是一个概率算法,返回的结果:一个数不是素数或者一个数可能是素数。
741

被折叠的 条评论
为什么被折叠?



