当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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