
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to check for prime and find next Prime in Java
In this article, let's learn how to check for a prime and find the next prime number in Java. Following are the different ways to do so:
Using the isProbablePrime() method
The isProbablePrime() is method of the java.math.BigInteger class. It accepts an integer value as a parameter, checks if it is prime or not, and returns true if it is prime, else it returns false.
To check if a number is prime using the isPrime() method, follow the steps below:
- Instantiate the BigInteger class by passing the number to be checked as a parameter.
- Invoke the isProbablePrime() method of the BigInteger class and pass the certainty value as a parameter.
- The certainty value is the probability that the number is prime. The higher the certainty value, the more accurate the result will be.
- If the isProbablePrime() method returns true, then the number is prime.
Example
Following is the Java program to check if a number is prime using the isPrime() method:
import java.math.BigInteger; public class PrimeNumber { public static void main(String args[]) { //Instantiating the BigInteger class BigInteger number = new BigInteger("17"); //Checking if the number is prime boolean isPrime = number.isProbablePrime(1); if(isPrime) { System.out.println(number + " is a prime number"); } else { System.out.println(number + " is not a prime number"); } } }
Output
When the above code is executed, it produces the following output:
17 is a prime number
Using the nextProbablePrime() method
The nextProbablePrime() is method of the java.math.BigInteger class. It returns the next prime number after the specified number.
To find the next prime number using the nextPrime() method, follow the steps below:
- Instantiate the BigInteger class by passing the number to be checked as a parameter.
- Invoke the nextProbablePrime() method of the BigInteger class.
- The nextProbablePrime() method returns the next prime number after the specified number.
- If the nextProbablePrime() method returns a number, then it is the next prime number.
Example
Following is the Java program to find the next prime number using the nextPrime() method:
import java.math.BigInteger; public class NextPrime { public static void main(String args[]) { //Instantiating the BigInteger class BigInteger number = new BigInteger("17"); //Finding the next prime number BigInteger nextPrime = number.nextProbablePrime(); System.out.println("Next prime number after " + number + " is " + nextPrime); } }
Output
When the above code is executed, it produces the following output:
Next prime number after 17 is 19
Manual checking for prime and next prime
Any whole number that is greater than 1 and has only two factors, that is, 1 and the number itself, is called a prime number. Other than these two number, it has no positive divisor. For example: 7 = 1 Ã 7
To check if a number is prime, follow the steps below:
- Take integer variable A.
- Divide the variable A with (A-1 to 2).
- If A is divisible by any value (A-1 to 2) it is not prime.
- Else it is prime.
To find the next prime number, follow the steps below:
- Take integer variable A.
- Increment the variable A by 1.
- Check if A is prime using the above method.
- If A is prime, return A.
- Else repeat the above steps.
Example
Following is the Java program to check if a number is prime and find the next prime number:
public class PrimeAndNextPrime { public static void main(String args[]) { int num = 17; boolean isPrime = true; for (int i = 2; i <= num / 2; i++) { if (num % i == 0) { isPrime = false; break; } } if (isPrime) { System.out.println(num + " is a prime number"); } else { System.out.println(num + " is not a prime number"); } int nextPrime = num + 1; while (true) { isPrime = true; for (int i = 2; i <= nextPrime / 2; i++) { if (nextPrime % i == 0) { isPrime = false; break; } } if (isPrime) { System.out.println("Next prime number after " + num + " is " + nextPrime); break; } nextPrime++; } } }
Output
When the above code is executed, it produces the following output:
17 is a prime number Next prime number after 17 is 19