CipherMatrix

Extended Euclidean Algorithm and Modular Inverse Calculator

Find the greatest common divisor of two integers, the Bézout coefficients with a·x + b·y = gcd(a, b), and the modular inverse of a number, with every division step in a table.

The Euclidean algorithm

The Euclidean algorithm finds the greatest common divisor of two integers by repeated division. It relies on the fact that gcd(a, b) = gcd(b, a mod b). Starting with a and b, replace the pair by (b, remainder) until the remainder is 0; the last non-zero remainder is the gcd. For example, gcd(240, 46): 240 = 5·46 + 10, 46 = 4·10 + 6, 10 = 1·6 + 4, 6 = 1·4 + 2, 4 = 2·2 + 0, so the gcd is 2.

The extended version and Bézout coefficients

The extended algorithm keeps track of two more sequences while it divides, so that every remainder is written as a combination of the original numbers. At the end it produces integers x and y with a·x + b·y = gcd(a, b). This statement is Bézout's identity. The coefficients are not unique, and the calculator verifies the identity for the pair it reports.

Modular inverses

A modular inverse of a modulo m is a number x with a·x ≡ 1 (mod m). It exists exactly when gcd(a, m) = 1. In that case the Bézout identity reads a·x + m·y = 1, and reducing modulo m shows that x is the inverse. This is the step that computes the private exponent in RSA and the key inverses in the Affine and Hill ciphers.

If gcd(a, m) is greater than 1, the calculator explains that no inverse exists. For example, 6 has no inverse modulo 9 because gcd(6, 9) = 3.

Reading the table

Each row shows the quotient q, the remainder r and the coefficients s and t satisfying r = s·a + t·b. The last non-zero row gives the gcd together with x = s and y = t. Negative inputs are accepted: the gcd is always non-negative, and the signs of x and y adjust to match.

The algorithm is fast: the number of division steps is at most about five times the number of decimal digits of the smaller input (Lamé's theorem), and the worst case occurs for consecutive Fibonacci numbers. That is why it works comfortably on the 300-digit numbers used in RSA.

Worked examples

These examples are computed by the same engine as the calculator above, so you can compare each step with your own working.

gcd(240, 46) with Bézout coefficients

gcd(240, 46)
2
Bézout coefficients
x = -9, y = 47

Step-by-step solution

Formula used
a·x + b·y = gcd(a, b)
  1. CalculateExtended Euclid table
    iqrst
    0—24010
    1—4601
    25101-5
    346-421
    4145-26
    512-947
    62023-120
  2. CheckVerify the identity
    240·(-9) + 46·(47) = 2 = gcd

The modular inverse of 17 modulo 3120

17⁻¹ mod 3120
2753

Step-by-step solution

Formula used
a·x ≡ 1 (mod m) when gcd(a, m) = 1
  1. CalculateRun the algorithm on (17, 3120)
    gcd = 1, so an inverse exists.
    17·(-367) + 3120·(2) = 1
  2. ResultReduce the coefficient of 17 modulo 3120
    -367 mod 3120 = 2753
  3. CheckVerify
    17 · 2753 = 46801 = 1 + 15·3120 ≡ 1 (mod 3120)

Frequently asked questions

What is the extended Euclidean algorithm used for?

Mainly to compute modular inverses and Bézout coefficients. It is a key step in RSA key generation, in solving linear congruences and in the Chinese Remainder Theorem.

How do I find the inverse of a number modulo m?

Run the extended Euclidean algorithm on (a, m). If the gcd is 1, the coefficient of a in a·x + m·y = 1, reduced modulo m, is the inverse. If the gcd is not 1, no inverse exists.

Are the Bézout coefficients unique?

No. If (x, y) works, so does (x + k·b/g, y − k·a/g) for any integer k. The algorithm returns one particular pair.

What is gcd(a, 0)?

It equals the absolute value of a. The gcd of 0 and 0 is undefined.

Related calculators