The method gcd(long a, long b) of Guava’s LongMath Class returns the greatest common divisor of two parameters a and b.
public static long gcd(long a, long b)
Parameters: This method accepts two parameters a and b of the long type of whose GCD is to be found.
Return Type: This method returns the largest positive long value that divides both of the parameters passed to the function.
Exceptions: The method gcd(long a, long b) throws IllegalArgumentException if a is negative or b is negative.
Note: If a and b both are zero, the method returns zero.
GCD of 14 and 70 is 14 GCD of 23 and 15 is 1
java.lang.IllegalArgumentException: a (-5) must be >= 0
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.