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)
相關用法
- Java BigInteger add()用法及代碼示例
- Java BigInteger gcd()用法及代碼示例
- Java BigInteger nextProbablePrime()用法及代碼示例
- Java 8 BigInteger divideAndRemainder()用法及代碼示例
- Java BigInteger divide()用法及代碼示例
- Java BigInteger intValueExact()用法及代碼示例
- Java 8 BigInteger shortValueExact()用法及代碼示例
- Java BigInteger multiply()用法及代碼示例
- Java 8 BigInteger longValueExact()用法及代碼示例
- Java BigInteger sqrtAndRemainder()用法及代碼示例
- Java 8 BigInteger byteValueExact()用法及代碼示例
- Java BigInteger subtract()用法及代碼示例
- Java BigInteger mod()用法及代碼示例
- Java BigInteger pow()用法及代碼示例
- Java BigInteger xor()用法及代碼示例
注:本文由純淨天空篩選整理自Rajnis09大神的英文原創作品 BigInteger isProbablePrime() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。