Clever Quest Lab Tools

Extended Euclidean Algorithm Calculator

2026-10-07

Enter two integers to get their greatest common divisor and the Bézout coefficients x and y with a·x + b·y = gcd(a, b), together with every division step. The same page finds modular inverses and computes modular powers by square-and-multiply. All arithmetic uses exact big integers, so numbers with hundreds of digits work.

Extended Euclidean algorithm

Integers only: digits with an optional leading minus sign, no spaces or separators.

gcd(a, b)
x
y

Division steps

Row i holds the remainder r, and the coefficients s and t with r = |a|·s + |b|·t. From row 2 on, q is the quotient of the two remainders above it. The highlighted row, just before the remainder reaches 0, gives the gcd and the coefficients.

iqrstResult

Modular inverse

Finds the number inv in 0 … m − 1 with a × inv ≡ 1 (mod m).

Inverse of a modulo m

Modular exponentiation

Computes baseexponent mod m by square-and-multiply. A negative exponent works when the base has an inverse.

base^exponent mod m

StepBitExponent so farAfter squaringAfter multiplying

Everything is calculated in your browser. Nothing you enter is sent to a server.

How to use the extended Euclidean algorithm calculator

  1. In the first card, type two integers a and b. The gcd, the coefficients x and y, the identity with your numbers substituted and the division table update as you type.
  2. Read the highlighted row of the table: its r, s and t are the gcd and the coefficients. The line below the result gives every solution (x, y) of the same equation.
  3. In the second card, enter a and a modulus m of 2 or more. You get the inverse between 0 and m − 1, or a message explaining why none exists.
  4. In the third card, enter a base, an exponent and a modulus. For exponents of up to 64 bits the table shows each squaring and multiplication; longer exponents show only the result.
  5. Press Copy result to copy a card’s result as plain text, or Reset to example to restore the starting values. Your inputs are remembered in this browser.

How it works

For any two integers a and b that are not both zero, Bézout’s identity says there are integers x and y with:

a·x + b·y = gcd(a, b)

The extended Euclidean algorithm finds such a pair while it computes the gcd. It starts with r0 = |a|, r1 = |b|, s0 = 1, s1 = 0, t0 = 0, t1 = 1 and repeats:

q = floor(r(i−1) / r(i))
r(i+1) = r(i−1) − q·r(i)
s(i+1) = s(i−1) − q·s(i)
t(i+1) = t(i−1) − q·t(i)

Here q is the integer quotient of the division, r the remainder, and s, t are the running coefficients. In every row r = |a|·s + |b|·t holds, so when the remainder reaches 0 the previous row contains the gcd and a valid pair. The number of divisions is at most five times the number of digits of the smaller input (Lamé’s theorem).

Signs and zero. The table works on the absolute values. If a is negative, x changes sign; if b is negative, y changes sign. The identity then holds for the numbers as typed. With b = 0 the result is gcd = |a|, x = ±1, y = 0. If both inputs are 0, the gcd is undefined and the tool says so. If a < b, the first division has quotient 0 and simply swaps the two numbers, so the coefficients come out swapped too.

All solutions. If (x, y) is one solution, every other one is x + k·b/g, y − k·a/g with g = gcd(a, b) and any integer k. To solve a·x + b·y = c for a multiple c of g, multiply both coefficients by c/g; if c is not a multiple of g, there is no integer solution.

Modular inverse. An inverse of a modulo m exists exactly when gcd(a, m) = 1. Then a·x + m·y = 1, so a·x ≡ 1 (mod m), and x reduced into 0 … m − 1 is the inverse. A negative a, or one that is at least m, is reduced modulo m first.

Modular exponentiation. The exponent is read in binary from the top bit. For each bit the running result is squared and reduced modulo m; if the bit is 1 it is also multiplied by the base and reduced again. The numbers never grow beyond m², and an exponent of n bits needs at most 2n multiplications instead of an astronomically large power. A negative exponent is handled as base−n = (base−1)n, which needs the inverse from the second card.

Examples

Example 1: gcd and coefficients. For a = 240, b = 46 the tool returns gcd 2 with x = −9 and y = 47. Check: 240 × (−9) + 46 × 47 = −2160 + 2162 = 2. The general solution is x = −9 + 23k, y = 47 − 120k.

Example 2: a modular inverse. For a = 17 and m = 3120 the inverse is 2753, because 17 × 2753 = 46801 = 15 × 3120 + 1.

Example 3: no inverse. For a = 6 and m = 9 the gcd is 3, not 1, so the tool reports that no inverse exists. Indeed the multiples of 6 modulo 9 are only 0, 3 and 6, never 1.

Example 4: a modular power. 413 mod 497: the exponent 13 is 1101 in binary, and the table gives the squared and multiplied values step by step, ending in 445. Computing 413 = 67,108,864 first gives the same remainder, but that shortcut fails once the power has thousands of digits.

Where modular inverses are used

Modular inverses are the division of modular arithmetic. They appear when you solve a congruence such as 7x ≡ 3 (mod 26) (multiply by the inverse of 7, which is 15), when you decode an affine cipher, and in the key setup of RSA. Take the textbook toy values p = 61, q = 53, n = 3233, φ(n) = 3120 and public exponent e = 17. The private exponent is the inverse of 17 modulo 3120, which is 2753 (Example 2). Encrypting the message 65 gives 6517 mod 3233 = 2790, and 27902753 mod 3233 returns 65. Real keys use primes of hundreds of digits; this page is for learning and checking, not for protecting data.

Common mistakes

Frequently asked questions

What is the extended Euclidean algorithm?

It is Euclid’s algorithm for the greatest common divisor with extra bookkeeping. Besides the gcd it returns integers x and y with a·x + b·y = gcd(a, b).

Why does my result differ from another source?

There are infinitely many valid pairs. Another source may list a different one, for example x + b/g and y − a/g. If your numbers satisfy the identity, they are correct.

When does a modular inverse exist?

Exactly when the number and the modulus are coprime, that is, when their gcd is 1. Modulo a prime, every number that is not a multiple of the prime has an inverse.

What does a negative exponent mean here?

base−n mod m is computed as the n-th power of the modular inverse of the base. If the base has no inverse modulo m, the tool tells you so.

How large can the numbers be?

Each input may have up to 1,000 digits. Very long division tables show only their first and last rows.

Is my data sent anywhere?

No. All calculations run in your browser. The values you type are kept only in this browser’s local storage so they are still there on your next visit; nothing is uploaded.

Related tools