lcm function

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

Least common multiple. Returns 0 if either argument is 0. Audited: 2026-06-12 11:26 EDT

Implementation

int lcm(int a, int b) {
  if (a == 0 || b == 0) return 0;
  // Divide before multiplying: `a * b` can overflow 64-bit for large operands,
  // whereas `(a / gcd) * b` keeps the intermediate small for the same result.
  return (a.abs() ~/ gcd(a, b)) * b.abs();
}