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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。