Short answer: The extended Euclidean algorithm returns integers x and y such that a·x + b·y = gcd(a, b). It runs the same divisions as the Euclidean algorithm and also updates two coefficient rows, one for each remainder. The modular inverse of a modulo m exists exactly when the gcd is 1, and it is the coefficient of a, reduced modulo m. The loop is O(log min(|a|, |b|)) divisions.
What the problem actually asks
The plain gcd throws away the quotients. Each remainder is an explicit integer combination of the previous two remainders, and those are combinations of the original a and b. Carrying the combinations along is enough. No extra search is required.
The inverse is the special case. If gcd(a, m) = 1, then a·x + m·y = 1, so a·x ≡ 1 (mod m). If the gcd is d > 1, no inverse exists, and the same run proves it by returning d. A caller who takes x anyway and reduces modulo m has not built an inverse.
What the algorithm guarantees
The returned x and y satisfy the Bézout identity for the inputs actually passed in, including signs. Time matches the Euclidean algorithm. Extra memory is a constant number of integers. The coefficients are not unique: x + b/d and y − a/d describe another solution. The modular inverse is x mod m, taken in the positive residue range, and only when d = 1.
Recursive code that returns the triple (g, x, y) from (b, a mod b) and then sets x’ = y, y’ = x − quotient·y is the shortest correct form. Iterative code must update both coefficient sequences in the same order as the remainders. Updating only one of them is the usual bug.
The working steps
- If b is 0, return (|a|, sign(a), 0), or the signed convention the caller documents. The rest of the steps assume the recursive form.
- Recurse on (b, a mod b) to obtain (g, x1, y1) with b·x1 + (a mod b)·y1 = g.
- The quotient q is floor(a / b) under the same division that produced the remainder. Set x = y1 and y = x1 − q·y1.
- Return (g, x, y). Then a·x + b·y = g.
- For an inverse of a modulo m, run the algorithm on (a, m). If g is not 1, report that no inverse exists. Otherwise return x mod m in the range 0..m−1.
The identity uses the same a and b that were divided. Mixing a modular reduction of a into the coefficient update without reducing the equation produces a combination of the wrong pair. Batch inverses of 1..n modulo a prime can be computed more cheaply than n separate runs. One inverse is this algorithm. RSA and Diffie-Hellman call it for the private exponent or for a field inverse, and they do not change the loop.
Approaches that look simpler and fail
| Approach | Why it fails when only one coefficient is updated |
|---|---|
| Return x from the gcd loop and set y = 1 | The second coefficient is determined by the quotients. A constant 1 satisfies the identity only for special inputs. |
| Reduce x modulo m when the gcd is not 1 | a·x is a multiple of the gcd, not of 1, in the residues. The inverse does not exist. |
| Use a quotient from floor division and a remainder from a language operator that disagrees on negatives | The identity q·b + r = a has to be exact for the q that the coefficient update uses. Mismatched division conventions break y. |
| Invert a modulo m by exponentiation when m is composite | Euler’s theorem needs φ(m) and a coprime a. The extended algorithm does not need φ and also reports the non-coprime case. Exponentiation on a composite modulus is a different, stricter route. |
The gcd alone is the plain Euclidean algorithm. A sieve builds primes and can support bulk inverses. The extended Euclidean algorithm is the Bézout coefficient loop and the standard modular inverse.
A working example
The Bash script and the Python script run the extended Euclidean algorithm. Both print 6 -1 3, and 48(-1)+183 equals 6.
# Iterative Bezout. r1*s2 style on (old, cur).
a=48
b=18
s0=1; s1=0
t0=0; t1=1
while (( b != 0 )); do
q=$((a / b))
r=$((a % b))
a=$b; b=$r
ns=$((s0 - q * s1)); s0=$s1; s1=$ns
nt=$((t0 - q * t1)); t0=$t1; t1=$nt
done
echo "$a $s0 $t0"
def egcd(a, b):
if b == 0:
return a, 1, 0
g, x1, y1 = egcd(b, a % b)
return g, y1, x1 - (a // b) * y1
g, x, y = egcd(48, 18)
assert g == 6 and 48 * x + 18 * y == g
print(g, x, y)
Where it is used
The extended Euclidean algorithm produces the modular inverse: Python pow(a, -1, m) and the inverse inside RSA key generation. Bézout’s coefficients are the extra output. Fermat’s little theorem also inverts, and only when the modulus is prime.
When you can drop this algorithm
Drop ad hoc coefficient guesses. Run the recurrence. Keep the algorithm whenever an inverse modulo m is required and the caller must also learn that the inverse does not exist when the gcd is greater than 1.
Why this stays published
We publish this so a search for the extended Euclidean algorithm returns the coefficient update x = y1, y = x1 − q·y1 and the inverse test that the gcd is 1. The extended Euclidean algorithm writes the gcd as a·x + b·y and, when the gcd is 1, returns the modular inverse of a.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
