lcm function

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

Returns the least common multiple of a and b.

Computed as |a| / gcd(a, b) * |b| to avoid intermediate overflow. Returns 0 if both a and b are zero.

Implementation

int lcm(int a, int b) {
  if (a == 0 && b == 0) return 0;
  return (a.abs() ~/ gcd(a, b)) * b.abs();
}