lcm

fun lcm(a: Long, b: Long): Long(source)

Computes the least common multiple of two integers.

The LCM is the smallest positive integer that is divisible by both a and b. Returns 0 when either input is 0. Negative inputs are treated as their absolute values. Computed as |a| / gcd(|a|, |b|) * |b| to avoid intermediate overflow.

Example:

lcm(12, 8)    // 24
lcm(7, 13) // 91 (coprime, so LCM = a * b)
lcm(0, 5) // 0
lcm(-12, 8) // 24 (negatives treated as absolute values)

Return

the least common multiple of |a| and |b|, or 0 if either input is 0.

Parameters

a

the first integer. Must not be Long.MIN_VALUE.

b

the second integer. Must not be Long.MIN_VALUE.

See also

Throws