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


Java BigInteger nextProbablePrime()用法及代码示例



java.math.BigInteger.nextProbablePrime() 用于查找大于可能是素数的 BigInteger 的第一个整数。如果此方法返回 ‘p’ 则在此 Biginteger 和 ‘p’ (this < q < p ) 之间没有素数 ‘q’,即它永远不会跳过用于调用该方法的大于 BigInteger 的任何素数。

用法:

public BigInteger nextProbablePrime()

参数:该方法不接受任何参数。
返回:此方法返回一个 Biginteger,其中包含大于此 BigInteger 的第一个整数,该整数可能是素数。

Exception:数字必须是非负数,否则不能太大算术异常被抛出。

以下示例程序旨在说明BigInteger类的nextProbablePrime()方法

示例 1:


// Java program to demonstrate
// nextProbablePrime() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // BigInteger object to store the result
        BigInteger result;
  
        // For user input
        // Use Scanner or BufferedReader
  
        // object of String created
        // Holds the value to find next prime number
        String input1 = "1516132";
  
        // Creating BigInteger object
        BigInteger a
            = new BigInteger(input1);
  
        // Using nextProbablePrime()
        result = a.nextProbablePrime();
  
        // Print result
        System.out.println("The next probable"
                           + " Prime number is "
                           + result);
    }
}
输出:
The next probable Prime number is 1516153

示例 2:打印所有低于 100 的质数


// Java program to demonstrate
// nextProbablePrime() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // BigInteger object to store the result
        BigInteger result;
  
        // Creating BigInteger objects
        BigInteger a;
  
        // Printing all prime numbers
        // Below 100
        for (int i = 0; i < 100; ){
            a = new BigInteger(
                Integer.toString(i));
  
            // Using nextProbablePrime()
            result = a.nextProbablePrime();
  
            // Print the answer
            if (Integer.parseInt(
                    result.toString())
                < 100) {
                System.out.print(result + " ");
            }
  
            i = Integer.parseInt(
                result.toString());
        }
    }
}
输出:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

示例 3:当值为负时程序显示异常。


// Java program to demonstrate
// nextProbablePrime() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // BigInteger object to store the result
        BigInteger result;
  
        // For user input
        // Use Scanner or BufferedReader
  
        // object of String created
        // Holds the value to find prime number
        String input1 = "-5";
  
        // Creating BigInteger objects
        BigInteger a
            = new BigInteger(input1);
  
        try {
            // Using nextProbablePrime()
            result = a.nextProbablePrime();
        }
        catch (ArithmeticException e) {
            System.out.println(e);
        }
    }
}
输出:
java.lang.ArithmeticException: start 

要了解检查当前BigInteger 是否为素数,请访问isProbablePrime() Method in Java 页面。

参考: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigInteger.html#nextProbablePrime()


相关用法


注:本文由纯净天空筛选整理自Rajnis09大神的英文原创作品 BigInteger nextProbablePrime() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。