gcd function

int gcd(
  1. int a,
  2. int b
)

Greatest common divisor of a and b. Uses Euclidean algorithm.

Implementation

int gcd(int a, int b) {
  int x = a.abs();
  int y = b.abs();
  while (y != 0) {
    final int t = y;
    y = x % y;
    x = t;
  }
  return x;
}