Java中的BigInteger.isProbablePrime()方法
如果此BigInteger可能是素数,则BigInteger.isProbablePrime(int确定性)返回true,如果绝对是复合的,则返回false。如果确定度≤0,则返回true。
在这里,“确定性”参数是对呼叫者愿意忍受的不确定性的度量:如果呼叫返回true,则此BigInteger为质数的概率超过(1/1/2确定性)。该方法的执行时间与该参数的值成正比。
以下是一个例子-
示例
import java.math.BigInteger;
public class Demo {
public static void main(String[] argv) throws Exception {
//创建3个BigInteger对象
BigInteger bi1, bi2;
//创建3个布尔对象
Boolean b1, b2, b3;
bi1 = new BigInteger("11");
bi2 = new BigInteger("20");
// isProbablePrime()
b1 = bi1.isProbablePrime(1);
b2 = bi2.isProbablePrime(1);
String str1 = bi1+ " is prime with certainty 1 is " +b1;
String str2 = bi2+ " is prime with certainty 1 is " +b2;
System.out.println(str1);
System.out.println(str2);
}
}输出结果
11 is prime with certainty 1 is true 20 is prime with certainty 1 is false