Java Guava | isPrime() method of IntMath Class
The isPrime(int n) method of Guava’s IntMath class is used to check whether the parameter passed to it is a prime number or not. If the parameter passed to it is prime, then it returns True otherwise it returns False.
A number is said to be Prime if it is divisible only by 1 and the number itself.
Syntax :
public static boolean isPrime(int n)
Parameter: The method accepts only one parameter n which is of integer type and is to be checked for primality.
Return Value :
- true : if n is a prime number.
- false : if n is 0, 1 or composite number.
Exceptions : The method isPrime(int n) throws IllegalArgumentException if n is negative.
Example 1 :
// Java code to show implementation of // isPrime(int n) method of Guava's // IntMath class import java.math.RoundingMode; import com.google.common.math.IntMath; class GFG { // Driver code public static void main(String args[]) { int a1 = 63; // Using isPrime(int n) // method of Guava's IntMath class if(IntMath.isPrime(a1)) System.out.println(a1 + " is a prime number"); else System.out.println(a1 + " is not a prime number"); int a2 = 17; // Using isPrime(int n) // method of Guava's IntMath class if(IntMath.isPrime(a2)) System.out.println(a2 + " is a prime number"); else System.out.println(a2 + " is not a prime number"); } } |
Output :
63 is not a prime number 17 is a prime number
Example 2 :
// Java code to show implementation of // isPrime(int n) method of Guava's // IntMath class import java.math.RoundingMode; import com.google.common.math.IntMath; class GFG { static boolean findPrime(int n) { try { // Using isPrime(int n) method // of Guava's IntMath class // This should throw "IllegalArgumentException" // as n is negative boolean ans = IntMath.isPrime(n); // Return the answer return ans; } catch (Exception e) { System.out.println(e); return false; } } // Driver code public static void main(String args[]) { int a1 = -7; try { // Using isPrime(int n) method // of Guava's IntMath class // This should throw "IllegalArgumentException" // as a1 is negative findPrime(a1); } catch (Exception e) { System.out.println(e); } } } |
Output :
java.lang.IllegalArgumentException: n (-7) must be >= 0
Reference :
https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/IntMath.html#isPrime-int-
Recommended Posts:
- Java Guava | pow(int b, int k) method of IntMath Class
- Java Guava | mod() method of IntMath Class
- Java Guava | mean() method of IntMath Class
- Java Guava | gcd(int a, int b) method of IntMath Class
- Java Guava | floorPowerOfTwo() method IntMath Class
- Java Guava | isPowerOfTwo() method IntMath Class
- Java Guava | binomial() method of IntMath Class
- Java Guava | ceilingPowerOfTwo() method of IntMath Class
- Java Guava | factorial(int n) method of IntMath Class with Examples
- Java Guava | sqrt(int x, RoundingMode mode) method of IntMath Class
- IntMath Class | Guava | Java
- Java Guava | IntMath.checkedAdd(int a, int b) method with Examples
- Java Guava | IntMath.checkedSubtract(int a, int b) method with Examples
- Java Guava | IntMath.checkedPow(int b, int k) method with Examples
- Java Guava | IntMath.checkedMultiply(int a, int b) method with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



