Euclidean algorithm

/**
 * Greatest common divisor 
 */
function gcd(a, b) {
  while (b > 0) {   
    const r = a % b;  
    a = b;
    b = r;
  }   
  return a;  
}

efficient method for computing the greatest common divisor (GCD) of two integers (numbers), the largest number that divides them both without a remainder.