gcd

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

Computes the greatest common divisor of two integers using the Euclidean algorithm.

The GCD is the largest positive integer that divides both a and b without a remainder. Negative inputs are treated as their absolute values. Returns 0 when both inputs are 0. Uses a tail-recursive implementation for stack safety with large inputs.

Example:

gcd(12, 8)   // 4
gcd(7, 13) // 1 (coprime)
gcd(0, 5) // 5
gcd(-12, 8) // 4 (negatives treated as absolute values)

Return

the greatest common divisor of |a| and |b|.

Parameters

a

the first integer. Must not be Long.MIN_VALUE.

b

the second integer. Must not be Long.MIN_VALUE.

See also

Throws