當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java BigInteger isProbablePrime()用法及代碼示例


java.math.BigInteger.isProbablePrime(int surety)方法用於判斷此BigInteger可能是素數還是絕對是合成的。此方法檢查當前BigInteger上是否有素數或複合數,由此方法將被調用,並返回一個布爾值。如果此BigInteger可能是素數,則返回true;如果絕對是複合值,則返回false。如果確定

用法:

public boolean isProbablePrime(int certainty)

參數:該方法接受強製性參數確定性,該確定性是對用戶可接受的不確定性的度量。這是由於BigInteger是一個非常大的數字,並且很難準確地確定它是否是素數。因此可以說,此方法基於閾值(1- 1/2)檢查此BigInteger的質數。certainty)。


返回值:此方法返回一個布爾值,該值指示此BigInteger是否為素數。如果此BigInteger可能是素數,則返回true;如果絕對是複合值,則返回false。

下麵的程序用於說明BigInteger的isProbablePrime()方法。

範例1:

// Java program to demonstrate 
// isProbablePrime() method of BigInteger 
  
import java.math.BigInteger; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Boolean variable to store the result 
        boolean result; 
  
        // Creates one BigInteger object 
        BigInteger a 
            = new BigInteger( 
                "95848961698036841689418631330196"); 
  
        // When certainty is one, 
        // it will check number for prime or composite 
        result = a.isProbablePrime(1); 
        System.out.println(a.toString() 
                           + " with certainty 1 "
                           + result); 
  
        // When certainty is zero, 
        // it is always true 
        result = a.isProbablePrime(0); 
        System.out.println(a.toString() 
                           + " with certainty 0 "
                           + result); 
  
        // When certainty is negative, 
        // it is always true 
        result = a.isProbablePrime(-1); 
        System.out.println(a.toString() 
                           + " with certainty -1 "
                           + result); 
    } 
}
輸出:
95848961698036841689418631330196 with certainty 1 false
95848961698036841689418631330196 with certainty 0 true
95848961698036841689418631330196 with certainty -1 true

範例2:

// Java program to demonstrate 
// isProbablePrime() method of BigInteger 
  
import java.math.BigInteger; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Boolean variable to store the result 
        boolean result; 
  
        // Creates one BigInteger object 
        BigInteger a 
            = new BigInteger( 
                "654561561356879113561"); 
  
        // When certainty is one, 
        // it will check number for prime or composite 
        result = a.isProbablePrime(1); 
        System.out.println(a.toString() 
                           + " with certainty 1 "
                           + result); 
  
        // When certainty is zero, 
        // it is always true 
        result = a.isProbablePrime(0); 
        System.out.println(a.toString() 
                           + " with certainty 0 "
                           + result); 
  
        // When certainty is negative, 
        // it is always true 
        result = a.isProbablePrime(-1); 
        System.out.println(a.toString() 
                           + " with certainty -1 "
                           + result); 
    } 
}
輸出:
654561561356879113561 with certainty 1 false
654561561356879113561 with certainty 0 true
654561561356879113561 with certainty -1 true

參考: https://docs.oracle.com/javase/9/docs/api/java/math/BigInteger.html#isProbablePrime(int)



相關用法


注:本文由純淨天空篩選整理自Rajnis09大神的英文原創作品 BigInteger isProbablePrime() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。