BigInteger.isProbablePrime() 方法在 Java 中
BigInteger.isProbablePrime(int certainty) 如果这个 BigInteger 可能是素数,则返回 true,如果是合数,则返回 false。如果 certainty ≤ 0,则返回 true。
这里,“certainty”参数是调用者愿意容忍的不确定性程度的衡量:如果调用返回 true,则此 BigInteger 为素数的概率将超过 (1 - 1/2certainty)。此方法的执行时间与此参数的值成正比。
以下是一个示例 −
示例
import java.math.BigInteger;
public class Demo {
public static void main(String[] argv) throws Exception {
// create 3 BigInteger objects
BigInteger bi1, bi2;
// create 3 Boolean objects
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
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP