Java Guava | gcd(int a, int b) method of IntMath Class Last Updated : 28 Jan, 2019 Comments Improve Suggest changes Like Article Like Report 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 IllegalArgumentException if a < 0 or b < 0. Example 1 : Java // Java code to show implementation of // gcd(int a, int b) 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 = 64; int b1 = 36; // Using gcd(int a, int b) method // of Guava's IntMath class int ans1 = IntMath.gcd(a1, b1); System.out.println("GCD of a1 & b1 is: " + ans1); int a2 = 23; int b2 = 15; // Using gcd(int a, int b) method // of Guava's IntMath class int ans2 = IntMath.gcd(a2, b2); System.out.println("GCD of a2 & b2 is: " + ans2); } } Output: GCD of a1 & b1 is: 4 GCD of a2 & b2 is: 1 Example 2 : Java // Java code to show implementation of // gcd(int a, int b) 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 = -5; int b1 = 15; try { // Using gcd(int a, int b) method // of Guava's IntMath class // This should throw "IllegalArgumentException" // as a1 < 0 int ans1 = IntMath.gcd(a1, b1); } catch (Exception e) { System.out.println(e); } } } Output: java.lang.IllegalArgumentException: a (-5) must be >= 0 Note: The method returns 0 if a == 0 && b == 0. Reference: https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/IntMath.html#gcd-int-int- Comment More infoAdvertise with us Next Article Java Guava | gcd(int a, int b) method of IntMath Class bansal_rtk_ Follow Improve Article Tags : Misc Java Java-Functions java-guava Guava-IntMath +1 More 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 | 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 Java Guava | IntMath.checkedMultiply(int a, int b) method with Examples The checkedMultiply(int a, int b) is a method of Guava's IntMath Class which accepts two parameters a and b, and returns their product. Syntax: public static int checkedMultiply(int a, int b) Parameters: The method accepts two int values a and b and computes their product. Return Value: The method r 2 min read 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. S 2 min read Java Guava | IntMath.checkedSubtract(int a, int b) method with Examples The checkedSubtract(int a, int b) is a method of Guava's IntMath Class which accepts two parameters a and b, and returns their difference. Syntax: public static int checkedSubtract(int a, int b) Parameters: The method accepts two int values a and b and computes their difference. Return Value: The me 2 min read Java Guava | IntMath.checkedPow(int b, int k) method with Examples checkedPow(int b, int k) is a method of Guava's IntMath Class which accepts two parameters b and k and is used to find the k-th power of b. Syntax: public static int checkedPow(int b, int k) Parameters: The method accepts two parameters, b and k. The parameter b is called base which is raised to the 2 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 Java Guava | IntMath.divide(int, int, RoundingMode) method with Examples The divide(int p, int q, RoundingMode mode) method of Guava's IntMath Class accepts three parameters and calculates the result of dividing first parameter by second parameter, rounded according to the rounding mode specified by the third parameter. Syntax: public static int divide(int p, int q, Roun 3 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 | LongMath.checkedMultiply(int a, int b) method with Examples The checkedMultiply(long a, long b) is a method of Guava's LongMath Class which accepts two parameters a and b, and returns their product. Syntax: public static long checkedMultiply(long a, long b) Parameters: The method accepts two long values a and b and computes their product. Return Value: The m 2 min read Like