VOOZH about

URL: https://www.geeksforgeeks.org/java/java-guava-gcdlong-a-long-b-of-longmath-class-with-examples/

⇱ Java Guava | gcd(long a, long b) of LongMath Class with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Guava | gcd(long a, long b) of LongMath Class with Examples

Last Updated : 11 Jul, 2025
The method gcd(long a, long b) of Guava's LongMath Class returns the greatest common divisor of two parameters a and b. Syntax:
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. Example 1:
Output:
GCD of 14 and 70 is 14
GCD of 23 and 15 is 1
Example 2:
Output:
java.lang.IllegalArgumentException: a (-5) must be >= 0
Reference: https://guava.dev/releases/20.0/api/docs/com/google/common/math/LongMath.html#gcd-long-long-
Comment