
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java methods to check for prime and find the next prime
The java.math.BigInteger class provides operations analogues to all of Java's primitive integer operators and for all relevant methods from java.lang.Math. It also provides methods to verify if a number is prime and, a method to find next probable prime.
isProbablePrime() − This method accepts an integer value representing the certainty and verifies whether value represented by the current object is a prime number. It returns a boolean value which is −
true, if the given number is prime.
false, if the given number is not prime.
Example
import java.math.BigInteger; import java.util.Scanner; public class isProbablePrimeExample { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number :"); long num = sc.nextLong(); int result = 0; BigInteger bigInt = new BigInteger(String.valueOf(num)); boolean prime = bigInt.isProbablePrime(1); if (prime) { System.out.println(num+" is a prime number"); } else { System.out.println(num+" is not a prime number"); } } }
Output1
Enter a number : 25 25 is not a prime number
Output2
Enter a number : 19 19 is a prime number
nextProbablePrime() − this method returns the next first prime number (integer) greater than the current BigInteger.
Example
import java.math.BigInteger; import java.util.Scanner; public class nextProbablePrimeExample { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number :"); long num = sc.nextLong(); int result = 0; BigInteger bigInt = new BigInteger(String.valueOf(num)); BigInteger prime = bigInt.nextProbablePrime(); System.out.println("Next prime number : "+prime.intValue()); } }
Output
Enter a number : 25 Next prime number : 29
- Related Articles
- Java program to check for prime and find next Prime in Java
- Different Methods to find Prime Number in Java
- Find next palindrome prime in C++
- Different Methods to find Prime Number in Python
- Different Methods to find Prime Numbers in C#
- Different Methods to find Prime Number in Python Program
- Next smallest prime palindrome in C++
- Analysis of Different Methods to find Prime Number in Python
- Add the following:a) \( 16^{\circ} 45^{\prime} 35^{\prime \prime} \) and \( 112^{\circ} 42^{\prime} 48^{\prime \prime} \)b) \( 78^{\circ} 19^{\prime} 56^{\prime \prime} ; 95^{\circ} 48^{\prime} 17^{\prime \prime} \) and \( 114^{\circ} \) 34' 41"
- Analysis of Different Methods to find Prime Number in Python program
- Python Program to Check Prime Number
- Finding next prime number to a given number using JavaScript
- Java Program to Check Whether a Number is Prime or Not
- Pollard’s Rho Algorithm for Prime Factorization in java
- Find the difference between the smallest 3 digit prime number and largest 1digit prime number

Advertisements