BigInteger nextProbablePrime() Method in Java with Examples Last Updated : 08 Apr, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report The java.math.BigInteger.nextProbablePrime() is used to find the first integer greater than this BigInteger that is probably prime. If this method returns 'p' then there is no prime number 'q' between this Biginteger and 'p' (this < q < p ) i.e. it never skips any prime number greater than BigInteger that is used to call the method. Syntax: public BigInteger nextProbablePrime() Parameters: The method doesn't accept any Parameters. Return: This method returns a Biginteger which holds the first integer greater than this BigInteger that is probably prime number. Exception: The number must be non - negative and not too large otherwise Arithmetic Exception is thrown. Below programs illustrate nextProbablePrime() method of BigInteger class Example 1: java // 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); } } Output: The next probable Prime number is 1516153 Example 2: Printing all Prime number below 100 Java // 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()); } } } Output: 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 Example 3: Program showing exception when value is negative. Java // 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); } } } Output: java.lang.ArithmeticException: start < 0: -5 To know about checking current BigInteger is prime or not, visit isProbablePrime() Method in Java page. Reference: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigInteger.html#nextProbablePrime() Comment More infoAdvertise with us Next Article BigInteger nextProbablePrime() Method in Java with Examples R Rajnis09 Follow Improve Article Tags : Java Java-Functions Java-BigInteger Java-math-package Practice Tags : JavaJava-BigInteger Similar Reads BigInteger gcd() Method in Java with Examples GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them. The java.math.BigInteger.gcd(BigInteger val) method is used to calculate gcd of two BigIntegers. This method calculates gcd upon the current BigInteger by which this method is 2 min read Random nextDouble() method in Java with Examples The nextDouble() method of Random class returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence. Syntax: public double nextDouble() Parameters: The function does not accepts any parameter. Return Value: This method returns th 1 min read Java.math.BigInteger.probablePrime() method in Java Prerequisite : BigInteger Basics The probablePrime() method will return a Biginteger of bitLength bits which is prime. bitLength is provided as parameter to method probablePrime() and method will return a prime BigInteger of bitLength bits. The probability that a BigInteger returned by this method i 2 min read Random next() method in Java with Examples The next() method of Random class returns the next pseudorandom value from the random number generator's sequence. Syntax: protected int next(int bits) Parameters: The function accepts a single parameter bits which are the random bits. Return Value: This method returns the next pseudorandom number. 1 min read BigInteger isProbablePrime() Method in Java with Examples The java.math.BigInteger.isProbablePrime(int certainty) method is used to tell if this BigInteger is probably prime or if it's definitely composite. This method checks for prime or composite upon the current BigInteger by which this method is called and returns a boolean value. It returns true if th 3 min read Like