Java BigInteger isProbablePrime() 方法

2025 年 3 月 20 日 | 阅读 2 分钟

Java BigInteger 类的 isProbablePrime() 方法用于确定给定的数字是素数还是合数。当 certainty 为 1 时,如果此 BigInteger 是素数,则此方法返回 true,如果此 BigInteger 是合数,则返回 false。

语法

参数

certainty - 调用者愿意容忍的不确定性度量:如果调用返回 true,则此 BigInteger 为素数的概率超过 (1 - 1/2certainty)。此方法的执行时间与此参数的值成正比。

返回值

如果此 BigInteger 可能是素数,则此方法返回 true;如果它肯定是合数,则返回 false。

Exception

不适用

注意:如果 certainty 小于或等于 0,则此方法返回 true。

示例 1

输出

5 is prime with certainty 1 is true
5 is prime with certainty 0 is true
5 is prime with certainty -1 is true 

示例 2

输出

8 is prime with certainty 1 is false
8 is prime with certainty 0 is true
8 is prime with certainty -1 is true

示例 3

输出

9 is prime with certainty 1 is false
9 is prime with certainty 0 is true
9 is prime with certainty -1 is true
 
下一主题Java BigInteger