BinaryLehmerGcd

A high-performance implementation of the greatest common divisor (GCD) algorithm that combines binary GCD with Lehmer's algorithm for optimal performance on large integers.

Overview

This library provides an efficient implementation of GCD computation that scales well from small integers to very large numbers. The algorithm uses different strategies based on the size of the input numbers:

Features

Installation

Add binary_lehmer_gcd to your list of dependencies in mix.exs:

def deps do
  [
    {:binary_lehmer_gcd, "~> 1.0"}
  ]
end

Usage

Basic Usage

iex> BinaryLehmerGcd.of(48, 18)
6

iex> BinaryLehmerGcd.of(0, 42)
42

iex> BinaryLehmerGcd.of(123456789, 987654321)
9

iex> BinaryLehmerGcd.of(2_147_483_647, 2_147_483_646)
1

Large Number Performance

The algorithm is particularly efficient for large integers:

# These large numbers are handled efficiently
iex> BinaryLehmerGcd.of(123456789012345678901234567890, 987654321098765432109876543210)
90

Algorithm Details

Binary GCD

The binary GCD algorithm is based on the following properties:

Lehmer's Algorithm

For large numbers, Lehmer's algorithm reduces the problem size by:

  1. Taking the top digits of both numbers
  2. Computing a linear combination that reduces the numbers
  3. Applying this reduction to the original numbers
  4. Continuing with binary GCD on the reduced numbers

Performance

When to Use

This implementation is particularly beneficial when:

Dependencies

This library depends on:

License

Copyright (c) 2025 University of Kitakyushu

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

References

Changelog

See CHANGELOG.md for a list of changes and version history.