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

更新於: 2019 年 7 月 30 日

200 次瀏覽

開啟你的 職業生涯

完成該課程以獲得認證

開始
廣告
© . All rights reserved.