Java Guava | isPrime() method of IntMath Class Last Updated : 23 Jan, 2019 Comments Improve Suggest changes Like Article Like Report 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 // 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 // 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- Comment More infoAdvertise with us Next Article Java Guava | isPrime() method of IntMath Class bansal_rtk_ Follow Improve Article Tags : Misc Java Java-Functions java-guava Practice Tags : JavaMisc Similar Reads Java Guava | mod() method of IntMath Class The mod(int x, int m) method of Guava's IntMath class accepts two parameters x and m and used to calculate the value of x modulus under m. Syntax : public static int mod(int x, int m) Parameters: This method accepts two parameters x and m which are of integer types and calculate x modulo m. Return V 2 min read Java Guava | isPowerOfTwo() method IntMath Class The isPowerOfTwo() method of Guava's IntMath class is used to check if a number is power of two or not. It accepts the number to be checked as a parameter and return boolean value true or false based on whether the number is a power of 2 or not. Syntax : public static boolean isPowerOfTwo(int x) Par 2 min read Java Guava | gcd(int a, int b) method of IntMath Class The method gcd(int a, int b) of Guava's IntMath class returns the greatest common divisor of a, b. Syntax : public static int gcd(int a, int b) Where a and b are integers. Return Value : Greatest common divisor of integers a and b. Exceptions : The method gcd(int a, int b) throws IllegalArgumentExce 2 min read IntMath Class | Guava | Java Introduction : IntMath is used to perform mathematical operations on Integer values. Basic standalone math functions are divided into the classes IntMath, LongMath, DoubleMath, and BigIntegerMath based on the primary numeric type involved. These classes have parallel structure, but each supports onl 3 min read Java Guava | ceilingPowerOfTwo() method of IntMath Class The ceilingPowerOfTwo(int x) method of Guava's IntMath class accepts a parameter and calculates the smallest power of two greater than the values passed in the parameter. This method is equivalent to checkedPow(2, log2(x, CEILING)). Syntax : public static int ceilingPowerOfTwo(int x) Parameter: This 2 min read Ints Class | Guava | Java Ints is a utility class for primitive type int. It provides Static utility methods pertaining to int primitives, that are not already found in either Integer or Arrays. Declaration : @GwtCompatible(emulated=true) public final class Ints extends Object Below table shows the Field summary for Guava In 3 min read Java Guava | factorial(int n) method of IntMath Class with Examples The factorial(int n) method of Guava's IntMath Class returns the product of the first n positive integers, which is n!. Syntax: public static int factorial(int n) Parameter: The method accepts only one parameter n which is of integer type and is to be used to find the factorial. Return Value: This m 2 min read Class isPrimitive() method in with Examples The isPrimitive() method of java.lang.Class class is used to check if this Class is the Primitive class. The method returns true if this Class is the Primitive class. It returns false otherwise.Syntax: public boolean isPrimitive() Parameter: This method does not accept any parameter.Return Value: Th 1 min read LongMath Class | Guava | Java LongMath is used to perform mathematical operations on Long values. Basic standalone math functions are divided into the classes IntMath, LongMath, DoubleMath, and BigIntegerMath based on the primary numeric type involved. These classes have parallel structure, but each supports only the relevant su 3 min read Java Guava | IntMath.checkedAdd(int a, int b) method with Examples The checkedAdd(int a, int b) is a method of Guava's IntMath Class which accepts two parameters a and b, and returns their sum. Syntax: public static int checkedAdd(int a, int b) Parameters: The method accepts two int values a and b and computes their sum. Return Value: The method returns the sum of 2 min read Like