- Java.math 包额外内容
- Java.math - 枚举
- Java.math - 讨论
Java.math.BigInteger.isProbablePrime() 方法
说明
java.math.BigInteger.isProbablePrime(int certainty) 返回 true 表示此 BigInteger 可能为素数,返回 false 表示肯定为合数。若 certainty ≤ 0,则返回 true。
声明
以下是 java.math.BigInteger.isProbablePrime() 方法的声明。
public boolean isProbablePrime(int certainty)
参数
certainty - 呼叫者愿意容忍的不确定性测量:如果呼叫返回 true,则此 BigInteger 为素数的概率超过(1 - 1/2certainty)。此方法的执行时间与此参数值成正比。
返回值
此方法返回 true 表示此 BigInteger 可能为素数,返回 false 表示肯定为合数。
异常
无
示例
以下示例显示了 math.BigInteger.isProbablePrime() 方法的用法。
package com.tutorialspoint;
import java.math.*;
public class BigIntegerDemo {
public static void main(String[] args) {
// create 3 BigInteger objects
BigInteger bi1, bi2, bi3;
// create 3 Boolean objects
Boolean b1, b2, b3;
// assign values to bi1, bi2
bi1 = new BigInteger("7");
bi2 = new BigInteger("9");
// perform isProbablePrime on bi1, bi2
b1 = bi1.isProbablePrime(1);
b2 = bi2.isProbablePrime(1);
b3 = bi2.isProbablePrime(-1);
String str1 = bi1+ " is prime with certainity 1 is " +b1;
String str2 = bi2+ " is prime with certainity 1 is " +b2;
String str3 = bi2+ " is prime with certainity -1 is " +b3;
// print b1, b2, b3 values
System.out.println( str1 );
System.out.println( str2 );
System.out.println( str3 );
}
}
让我们编译并运行上述程序,这将生成以下结果 -
7 is prime with certainity 1 is true 9 is prime with certainity 1 is false 9 is prime with certainity -1 is true
java_math_biginteger.htm
广告