Java.math.BigInteger.isProbablePrime() 方法

描述

如果此 BigInteger 可能是素数,则 java.math.BigInteger.isProbablePrime(int certainty) 返回 true,如果它肯定是复合数,则返回 false。 如果确定性是 ≤ 0,返回 true。


声明

以下是 java.math.BigInteger.isProbablePrime() 方法的声明。

public boolean isProbablePrime(int certainty)

参数

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


返回值

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


异常

NA


示例

下面的例子展示了 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