Java BigInteger isProbablePrime()方法及示例

Java BigInteger isProbablePrime()方法及示例

java.math.BigInteger .isProbablePrime(int certainty) 方法是用来判断这个BigInteger是否可能是质数或肯定是复合数。该方法对调用该方法的当前BigInteger进行质数或合数的检查,并返回一个布尔值。如果这个BigInteger可能是质数,则返回真,如果肯定是合数,则返回假。如果确定度<=0,则返回true。

语法

public boolean isProbablePrime(int certainty)

参数: 该方法接受一个强制性的参数certainty,它是对用户可接受的不确定性的一种衡量。这是由于BigInteger是一个非常非常大的数字,准确地找到它是否为素数是非常困难和昂贵的。因此,可以说这个方法是根据一个阈值(1-1/2的确定性 )来检查这个BigInteger的素数。

返回值: 本方法返回一个布尔值,说明这个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)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程