當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。