Java.math.BigInteger.nextProbablePrime() Method


Advertisements


Description

The java.math.BigInteger.nextProbablePrime() returns the first integer greater than this BigInteger that is probably prime. The probability that the number returned by this method is composite does not exceed 2-100.

This method will never skip over a prime when searching: if it returns p, there is no prime q such that this < q < p.

Declaration

Following is the declaration for java.math.BigInteger.nextProbablePrime() method.

public BigInteger nextProbablePrime()

Parameters

NA

Return Value

This method returns the first integer greater than this BigInteger that is probably prime.

Exception

ArithmeticException − If this < 0.

Example

The following example shows the usage of math.BigInteger.nextProbablePrime() method.

package com.tutorialspoint;

import java.math.*;

public class BigIntegerDemo {

   public static void main(String[] args) {

      // create 2 BigInteger objects
      BigInteger bi1, bi2;

      bi1 = new BigInteger("20");

      // assign nextProbablePrime value of bi1 to bi2
      bi2 = bi1.nextProbablePrime();

      String str = "Next probable prime after " + bi1 +" is " +bi2;

      // print bi2 value
      System.out.println( str );
   }
}

Let us compile and run the above program, this will produce the following result −

Next probable prime after 20 is 23

java_math_biginteger.htm

Advertisements