VOOZH about

URL: https://www.geeksforgeeks.org/java/quick-ways-to-check-for-prime-and-find-next-prime-in-java/

⇱ Quick ways to check for Prime and find next Prime in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Quick ways to check for Prime and find next Prime in Java

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. 


Output
true

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. 


Output
17
Comment
Article Tags:
Article Tags: