java.math.BigInteger nextProbablePrime()


On this document we will be showing a java example on how to use the nextProbablePrime() method of BigInteger Class. Basically this method 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.

Tech Notes:

  • A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.
  • An ArithMeticException will be thrown if the BigInteger is less than zero or this BigInteger is too large.

Method Syntax

public BigInteger nextProbablePrime()

Method Argument

Data Type Parameter Description

Method Returns

The nextProbablePrime() method returns the first integer greater than this BigInteger that is probably prime.


Requires Java 1.5 and up

Java BigInteger nextProbablePrime() Example

Below is a java code demonstrates the use of nextProbablePrime() method of BigInteger class. The example presented might be simple however it shows the behavior of the nextProbablePrime() method.


import java.math.BigInteger;
import java.util.Scanner;

 * A java example source code to demonstrate
 * the use of nextProbablePrime() 
 * method of BigInteger class

public class BigIntegerNextProbablePrimeExample {

	public static void main(String[] args) {	
		// get user input		 
		System.out.print("Enter a value:");
		Scanner s = new Scanner(;
		String input = s.nextLine();		

		// convert string input to BigInteger
		BigInteger val = new BigInteger(input);
		// get the next probable prime number 
		BigInteger result = val.nextProbablePrime();
		System.out.println("Next Prime Number:"+result);


This example is a lot simpler than it looks. Simply we ask for a user input. And then using the nextProbablePrime() method we get the next prime number.

Sample Output

Below is the sample output when you run the above example.

BigInteger nextProbablePrime() example output