BinaryExtendedGcd

A fast implementation of the binary extended GCD algorithm for Elixir.

This library provides an efficient implementation of the extended greatest common divisor (GCD) algorithm using the binary GCD method. The binary algorithm is particularly efficient for large integers as it eliminates expensive division operations by using only bit shifts and additions.

Features

Installation

The package can be installed by adding binary_extended_gcd to your list of dependencies in mix.exs:

def deps do
  [
    {:binary_extended_gcd, "~> 0.1.0"}
  ]
end

Usage

Basic Usage

# Compute extended GCD of 48 and 18
{gcd, x, y} = BinaryExtendedGcd.of(48, 18)
# Returns {6, -1, 3}
# This means: 6 = 48 * (-1) + 18 * 3

# Compute extended GCD of 17 and 13
{gcd, x, y} = BinaryExtendedGcd.of(17, 13)
# Returns {1, 4, -5}
# This means: 1 = 17 * 4 + 13 * (-5)

Edge Cases

# When one number is 0
BinaryExtendedGcd.of(0, 5)   # Returns {5, 0, 1}
BinaryExtendedGcd.of(12, 0)  # Returns {12, 1, 0}

# Coprime numbers
BinaryExtendedGcd.of(17, 13) # Returns {1, 4, -5}

Mathematical Background

Extended GCD

The extended GCD not only computes the greatest common divisor of two integers a and b, but also finds Bézout coefficients $x$ and $y$ such that:

$$gcd(a, b) = ax + by$$

This is known as Bézout's identity and is useful in many applications including:

Binary GCD Algorithm

The binary GCD algorithm is an efficient variant of the Euclidean algorithm that:

  1. Eliminates division: Uses only bit shifts and additions
  2. Handles even numbers efficiently: Divides by 2 using bit shifts
  3. Maintains Bézout coefficients: Tracks coefficients throughout the computation

The algorithm works by:

  1. Extracting common factors of 2 from both numbers
  2. Using bit operations to handle even/odd cases efficiently
  3. Maintaining Bézout coefficients throughout the computation
  4. Restoring the common factors at the end

Performance

The binary algorithm is generally faster than the Euclidean algorithm for large integers because:

API Reference

BinaryExtendedGcd.of(a, b)

Computes the extended GCD of two integers.

Parameters:

Returns:{gcd, x, y} where:

Examples:

iex> BinaryExtendedGcd.of(48, 18)
{6, 2, -5}

iex> BinaryExtendedGcd.of(17, 13)
{1, -16, 21}

iex> BinaryExtendedGcd.of(0, 5)
{5, 0, 1}

Mathematical Properties

The result satisfies the following properties:

Testing

The library includes comprehensive tests with 1000 randomized test cases that verify:

Run tests with:

mix test

Dependencies

Development

Building Documentation

mix docs

Code Quality

mix check

This runs:

License

This project is licensed under the Apache License 2.0 - see the LICENSE.md file for details.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Run mix check to ensure code quality
  6. Submit a pull request

References