📜  Java中的 BigInteger nextProbablePrime() 方法及示例

📅  最后修改于: 2022-05-13 01:55:05.910000             🧑  作者: Mango

Java中的 BigInteger nextProbablePrime() 方法及示例


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

句法:

public BigInteger nextProbablePrime()

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

异常:数字必须是非负数且不能太大,否则会引发算术异常

下面的程序说明了 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 < 0: -5

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

参考: Java : Java ()