The greatest common divisor (GCD) of two integers is the largest whole number that divides both of them with no remainder, and the least common multiple (LCM) is the smallest positive number that both of them divide into evenly. The GCD of 12 and 18 is 6; their LCM is 36. You will implement both, packaged on one object as gcdLcm = { gcd, lcm }, using Euclid's algorithm — a two-thousand-year-old method that finds the GCD by taking remainders. Each function is variadic: it accepts one or more numbers and folds the answer across all of them.
gcdLcm.gcd(...nums) // one or more integers -> their greatest common divisor
gcdLcm.lcm(...nums) // one or more integers -> their least common multiple
Once you have gcd, the LCM of two numbers follows from it: lcm(a, b) = |a * b| / gcd(a, b).
gcdLcm.gcd(12, 18); // 6
gcdLcm.gcd(17, 5); // 1 (coprime — no common factor but 1)
gcdLcm.gcd(-12, 18); // 6 (uses absolute values)
gcdLcm.gcd(0, 5); // 5 (gcd with 0 is the other number)
gcdLcm.gcd(12, 18, 24); // 6 (reduced across all three)
gcdLcm.lcm(4, 6); // 12
gcdLcm.lcm(3, 5); // 15 (coprime — LCM is the product)
gcdLcm.lcm(0, 5); // 0 (any 0 makes the LCM 0)
gcdLcm.lcm(2, 3, 4); // 12 (reduced across all three)
gcd(a, b) = gcd(b, a % b), repeated until the second number is 0; the first number is then the answer. Use this rather than testing every candidate divisor.gcd(-12, 18) is 6, and lcm(-4, 6) is 12.gcd(0, x) is |x|, and gcd(0, 0) is 0. Any 0 passed to lcm makes the whole result 0.|a| / gcd * |b| (divide before you multiply), not |a * b| / gcd, so the intermediate product stays small.