LehmerGcd (Lehmer gcd v1.0.1)
Implementation of the Lehmer GCD algorithm for computing the greatest common divisor of two non-negative integers.
The Lehmer GCD algorithm is particularly efficient for very large integers, using matrix operations to reduce the problem size while maintaining mathematical correctness. For smaller numbers (less than $2^{32}$), it falls back to the Binary GCD algorithm for optimal performance.
Algorithm Overview
The Lehmer algorithm works by:
- Using the most significant bits of the input numbers to create a smaller problem
- Solving this smaller problem using matrix operations
- Using the solution to reduce the original problem size
- Repeating until the numbers are small enough for direct computation
Examples
iex> LehmerGcd.of(48, 18)
6
iex> LehmerGcd.of(123456789, 987654321)
9
iex> LehmerGcd.of(0, 42)
42
iex> LehmerGcd.of(42, 0)
42Performance
- For numbers smaller than $2^{32}$: Uses BinaryGcd for optimal performance
- For larger numbers: Uses Lehmer algorithm with $O(\log{n})$ complexity
- Memory usage: $O(1)$ additional space beyond input storage
Summary
Functions
@spec of(non_neg_integer(), non_neg_integer()) :: non_neg_integer()
Computes the greatest common divisor of two non-negative integers using the Lehmer algorithm.
Parameters
m- First non-negative integern- Second non-negative integer
Returns
- The greatest common divisor of
mandn
Examples
iex> LehmerGcd.of(48, 18)
6
iex> LehmerGcd.of(123456789, 987654321)
9
iex> LehmerGcd.of(0, 42)
42Algorithm Details
The function delegates to outer/2 which implements the main Lehmer algorithm logic.
For numbers smaller than $2^{32}$, it uses BinaryGcd for better performance.