Quick ways to check for Prime and find next Prime in Java (original) (raw)

Last Updated : 19 Jan, 2026

Many programming contest problems are somehow related to prime numbers. Either we are required to check Prime Numbers, or we are asked to perform certain functions for all prime numbers between 1 to N. Example: Calculate the sum of all prime numbers between 1 and 1000000. Java provides two functions under java.math.BigInteger to deal with Prime Numbers.

1. Using isProbablePrime(int certainty) Method:

A method in the BigInteger class to check if a given number is prime. For certainty = 1, it returns true if BigInteger is prime and false if BigInteger is composite. Below is a Java program to demonstrate the above function.

Java `

import java.util.; import java.math.;

public class CheckPrimeTest { //Function to check and return prime numbers static boolean checkPrime(long n) { // Converting long to BigInteger BigInteger b = new BigInteger(String.valueOf(n));

    return b.isProbablePrime(1);
}

// Driver method
public static void main (String[] args)
                     throws java.lang.Exception
{
   long n = 13;
   System.out.println(checkPrime(n));
}

}

`

2. Using nextProbablePrime() Method :

Another method present in BigInteger class. This functions returns the next Prime Number greater than current BigInteger. Below is Java program to demonstrate above function.

Java `

import java.util.; import java.math.;

class NextPrimeTest { // Function to get nextPrimeNumber static long nextPrime(long n) { BigInteger b = new BigInteger(String.valueOf(n)); return Long.parseLong(b.nextProbablePrime().toString()); }

// Driver method
public static void main (String[] args)
                throws java.lang.Exception
{
    long n = 14;
    System.out.println(nextPrime(n));
}

}

`