The Extended Euclidean Algorithm

Suppose you want a single equation expressing gcd⁡(240,46)\gcd(240, 46) as an integer combination of 240240 and 4646. The ordinary Euclidean Algorithm tells you the gcd is 22. But it does not, by itself, hand you integers xx and yy with 240x+46y=2240x + 46y = 2. Bezout's Identity guarantees such x,yx, y exist. However, existence proofs are often non-constructive in spirit, even when the underlying algorithm is finite. The Extended Euclidean Algorithm closes this gap. It runs the same division steps as the ordinary algorithm. Along the way, it records enough bookkeeping data. It then unwinds that data to produce explicit Bezout coefficients. This is not a cosmetic upgrade. The coefficients themselves are the object of interest in an enormous range of applications. Solving a Linear Congruence ax≡b(modn)ax \equiv b \pmod n reduces to finding a Bezout pair for aa and nn. Computing a Modular Inverse of aa modulo nn is nothing more than this. It is the cornerstone of modular arithmetic, the RSA cryptosystem, and the Chinese Remainder Theorem. It amounts to reading off the coefficient of aa in a Bezout identity with nn. Without an explicit algorithm, one would be reduced to search or guesswork. With it, the computation is as mechanical as long division, and just as fast. The big picture is that number theory has two layers. There are existence statements: there is a gcd, there are Bezout coefficients. And there are constructive procedures that realize those statements as finite computations. The Euclidean Algorithm itself already exemplifies this for the gcd. The Extended Euclidean Algorithm does the same job one level up. It works for the linear combination witnessing that gcd. It is the bridge between the abstract identity gcd⁡(a,b)=ax+by\gcd(a,b) = ax + by and a concrete pair (x,y)(x,y). That pair is one you can write down and verify. This article gives the precise algorithm. It proves the algorithm terminates and is correct. It works two genuinely asymmetric examples by hand. It shows exactly which hypothesis makes the whole machine run: finiteness of the division chain, and integrality of the remainders. It closes by connecting the coefficients directly to modular inverses and linear congruences, the natural next topics.

Key takeaways
  • The ordinary Euclidean Algorithm computes gcd⁡(a,b)\gcd(a,b); the extended version also computes integers x,yx, y with ax+by=gcd⁡(a,b)ax + by = \gcd(a,b).
  • It works by running the division algorithm forward, then substituting back through the chain of remainders from the last nontrivial step to the first.
  • Equivalently, one can carry a pair of auxiliary sequences forward alongside the remainders, avoiding an explicit back-substitution pass.
  • The algorithm terminates in O(log⁡min⁡(a,b))O(\log \min(a,b)) division steps, the same bound as the ordinary Euclidean Algorithm.
  • Bezout coefficients are never unique; the algorithm produces one particular solution, and all others differ by multiples of b/gcd⁡(a,b)b/\gcd(a,b) and a/gcd⁡(a,b)a/\gcd(a,b).
  • Its main downstream use is computing modular inverses: if gcd⁡(a,n)=1\gcd(a,n)=1, the xx produced satisfies ax≡1(modn)ax \equiv 1 \pmod n.
  • Key idea: run Euclid's division steps forward to reach the gcd, then substitute backward to express that gcd as an explicit integer combination of the original two numbers.

Where this lives today

The Extended Euclidean Algorithm is the standard method for computing multiplicative inverses modulo nn, which underlies the RSA cryptosystem's key generation (computing the private exponent as the inverse of the public exponent modulo φ(n)\varphi(n)) and virtually all modular-arithmetic-based cryptography, including elliptic curve arithmetic over finite fields. In computer algebra systems, the polynomial analogue of the same algorithm computes greatest common divisors and partial-fraction-style decompositions of rational functions. In the Chinese Remainder Theorem, reconstructing a residue from several congruences requires exactly the Bezout coefficients this algorithm supplies. It is also the standard route to solving general linear congruences ax≡b(modn)ax\equiv b\pmod n, and more generally linear Diophantine equations ax+by=cax+by=c, whenever d=gcd⁡(a,b)d=\gcd(a,b) divides cc: scale the algorithm's Bezout pair by c/dc/d. In lattice-based algorithms and continued fraction theory, the coefficient sequences si,tis_i, t_i are exactly the numerators and denominators of the convergents of the continued fraction expansion of a/ba/b, linking this elementary algorithm to Diophantine approximation.

Ask the tutor: How do I use the Bezout coefficients from this algorithm to solve a general linear congruence ax≡b(modn)ax \equiv b \pmod n?

DefinitionExtended Euclidean Algorithm and Bezout Coefficients

Let a,ba, b be integers, not both zero. A pair of integers (x,y)(x, y) is called a Bezout pair for (a,b)(a,b) if

ax+by=gcd⁡(a,b).ax + by = \gcd(a,b).

The Extended Euclidean Algorithm is the following procedure. Assume a,b>0a, b > 0 (the general case reduces to this one, as discussed in the Scope table). Apply the division algorithm repeatedly:

r0=a,r1=b,r0=q1r1+r2,0≤r2<r1,r1=q2r2+r3,0≤r3<r2,  ⋮rk−2=qk−1rk−1+rk,0≤rk<rk−1,rk−1=qkrk+0,\begin{aligned} r_0 &= a, \quad r_1 = b,\\ r_0 &= q_1 r_1 + r_2, \qquad 0 \le r_2 < r_1,\\ r_1 &= q_2 r_2 + r_3, \qquad 0 \le r_3 < r_2,\\ &\ \ \vdots\\ r_{k-2} &= q_{k-1} r_{k-1} + r_k, \qquad 0 \le r_k < r_{k-1},\\ r_{k-1} &= q_k r_k + 0, \end{aligned}

where rk=gcd⁡(a,b)r_k = \gcd(a,b) is the last nonzero remainder. Define auxiliary integer sequences si,tis_i, t_i by the recursions

s0=1, s1=0,si=si−2−qi−1si−1  (i≥2),s_0 = 1,\ s_1 = 0, \qquad s_i = s_{i-2} - q_{i-1} s_{i-1} \ \ (i \ge 2),
t0=0, t1=1,ti=ti−2−qi−1ti−1  (i≥2).t_0 = 0,\ t_1 = 1, \qquad t_i = t_{i-2} - q_{i-1} t_{i-1} \ \ (i \ge 2).

Then x=skx = s_k and y=tky = t_k form a Bezout pair: ask+btk=rk=gcd⁡(a,b)a s_k + b t_k = r_k = \gcd(a,b). Equivalently and non-circularly, one may compute the same pair by back-substitution: starting from the equation rk=rk−2−qk−1rk−1r_k = r_{k-2} - q_{k-1} r_{k-1}, repeatedly replace each remainder rir_i using the division equation that produced it, working backward until only r0=ar_0 = a and r1=br_1 = b remain.

Intuition

Picture the Euclidean Algorithm as a staircase descending from aa and bb down to gcd⁡(a,b)\gcd(a,b), one remainder at a time. Each step of the staircase is a division equation, and each equation is a small, exact bookkeeping fact: it says one remainder is a specific integer combination of the two remainders just above it. The ordinary algorithm only cares about the value rir_i at each step, and eventually reports rkr_k, the gcd. The extended algorithm insists on remembering the recipe — the exact combination of ri−2r_{i-2} and ri−1r_{i-1} that produced rir_i — at every rung. Once you reach the bottom rung, rk=gcd⁡(a,b)r_k = \gcd(a,b), you have a recipe expressing it in terms of rk−2,rk−1r_{k-2}, r_{k-1}. But rk−1r_{k-1} itself has a recipe in terms of rk−3,rk−2r_{k-3}, r_{k-2} from the step above, so you substitute that in. This telescopes all the way up the staircase: at each substitution the expression is rewritten purely in terms of remainders one level higher, until only r0=ar_0 = a and r1=br_1 = b remain, at which point you have gcd⁡(a,b)=ax+by\gcd(a,b) = ax + by for explicit integers x,yx,y. The forward recursion for (si,ti)(s_i, t_i) in the definition is simply this same substitution process performed forward instead of backward — it computes, at each stage ii, the coefficients such that ri=sia+tibr_i = s_i a + t_i b, so that no separate back-substitution pass is even needed.

Figure
The staircase of remainders and the back-substitution path for a=240, b=46a=240,\,b=46
r₀ = 240 r₁ = 46 r₂ = 10 r₃ = 6 r₄ = 4 240 = 5·46 + 10 46 = 4·10 + 6 10 = 1·6 + 4 6 = 1·4 + 2 → r₅ = 2 = gcd back-substitution climbs the staircase, rewriting r₄ then r₃ then r₂ purely in a, b

The central theorem

TheoremCorrectness and Termination of the Extended Euclidean Algorithm

Let a,ba, b be positive integers. Define the sequences ri,qi,si,tir_i, q_i, s_i, t_i as in the Definition above. Then: 1. (Termination) There is a finite index k≥1k \ge 1 with rk=gcd⁡(a,b)r_k = \gcd(a,b) and rk+1=0r_{k+1} = 0; the algorithm halts after kk division steps. 2. (Invariant) For every ii with 0≤i≤k0 \le i \le k,

ri=sia+tib.r_i = s_i a + t_i b.

3. (Correctness) In particular rk=ska+tkbr_k = s_k a + t_k b, so (x,y)=(sk,tk)(x,y) = (s_k, t_k) is a Bezout pair: ax+by=gcd⁡(a,b)ax + by = \gcd(a,b).

Proof

Termination. This is the finiteness statement already established for the ordinary Euclidean Algorithm, restated for completeness since the extended algorithm shares its division chain. By the division algorithm, at each step 0≤ri+1<ri0 \le r_{i+1} < r_i for i≥1i \ge 1, so the sequence r1>r2>r3>⋯r_1 > r_2 > r_3 > \cdots is a strictly decreasing sequence of nonnegative integers. A strictly decreasing sequence of nonnegative integers cannot be infinite, since it would otherwise produce an infinite strictly decreasing chain inside {0,1,…,r1}\{0, 1, \dots, r_1\}, a finite set. Hence the sequence reaches 00 after finitely many steps; let kk be the index with rk≠0r_k \ne 0 and rk+1=0r_{k+1} = 0 (such kk exists because r1=b>0r_1 = b > 0, so at least one nonzero term occurs, and the terms eventually hit 00). That rk=gcd⁡(a,b)r_k = \gcd(a,b) is the content of the ordinary Euclidean Algorithm's correctness: each division equation ri−1=qiri+ri+1r_{i-1} = q_i r_i + r_{i+1} shows gcd⁡(ri−1,ri)=gcd⁡(ri,ri+1)\gcd(r_{i-1}, r_i) = \gcd(r_i, r_{i+1}), because any common divisor of ri−1,rir_{i-1}, r_i divides ri+1=ri−1−qirir_{i+1} = r_{i-1} - q_i r_i, and any common divisor of ri,ri+1r_i, r_{i+1} divides ri−1=qiri+ri+1r_{i-1} = q_i r_i + r_{i+1}; hence the two pairs have exactly the same set of common divisors, and in particular the same gcd. Chaining these equalities,

gcd⁡(a,b)=gcd⁡(r0,r1)=gcd⁡(r1,r2)=⋯=gcd⁡(rk−1,rk)=gcd⁡(rk,0)=rk,\gcd(a,b) = \gcd(r_0, r_1) = \gcd(r_1, r_2) = \cdots = \gcd(r_{k-1}, r_k) = \gcd(r_k, 0) = r_k,

using gcd⁡(rk,0)=rk\gcd(r_k, 0) = r_k, which holds since every integer divides 00 and the largest common divisor of rkr_k and 00 is rkr_k itself.

Invariant, by strong induction on ii. Base cases i=0,1i = 0, 1: We have s0a+t0b=1⋅a+0⋅b=a=r0s_0 a + t_0 b = 1\cdot a + 0 \cdot b = a = r_0, and s1a+t1b=0⋅a+1⋅b=b=r1s_1 a + t_1 b = 0 \cdot a + 1 \cdot b = b = r_1, matching the definitions of r0,r1r_0, r_1. Inductive step: Fix i≥2i \ge 2 with i≤ki \le k, and suppose the invariant holds for indices i−2i-2 and i−1i-1, i.e.

ri−2=si−2a+ti−2b,ri−1=si−1a+ti−1b.r_{i-2} = s_{i-2} a + t_{i-2} b, \qquad r_{i-1} = s_{i-1} a + t_{i-1} b.

By the defining division equation at this stage of the algorithm,

ri=ri−2−qi−1ri−1.r_i = r_{i-2} - q_{i-1} r_{i-1}.

Substitute the inductive hypotheses:

ri=(si−2a+ti−2b)−qi−1(si−1a+ti−1b)=(si−2−qi−1si−1)a+(ti−2−qi−1ti−1)b.r_i = \big(s_{i-2} a + t_{i-2} b\big) - q_{i-1}\big(s_{i-1} a + t_{i-1} b\big) = \big(s_{i-2} - q_{i-1} s_{i-1}\big) a + \big(t_{i-2} - q_{i-1} t_{i-1}\big) b.

By the definitions si=si−2−qi−1si−1s_i = s_{i-2} - q_{i-1}s_{i-1} and ti=ti−2−qi−1ti−1t_i = t_{i-2} - q_{i-1}t_{i-1}, this is exactly ri=sia+tibr_i = s_i a + t_i b, completing the induction. Note this induction uses only the two immediately preceding cases at each stage, so it is a valid strong induction with no circularity: the recursion for rir_i (a fact already proved about the ordinary algorithm) is used only to justify the identity for the new index ii, never to reprove anything about smaller indices.

Correctness. Taking i=ki = k in the invariant (valid since k≤kk \le k) gives rk=ska+tkbr_k = s_k a + t_k b. Combined with rk=gcd⁡(a,b)r_k = \gcd(a,b) from the termination step, we obtain

ask+btk=gcd⁡(a,b),a s_k + b t_k = \gcd(a,b),

so (x,y)=(sk,tk)(x,y) = (s_k, t_k) is a Bezout pair, as claimed. ■\blacksquare

∎
CorollaryEquivalence with Back-Substitution

The value (sk,tk)(s_k, t_k) produced by the forward recursion equals the pair (x,y)(x,y) obtained by starting from rk=rk−2−qk−1rk−1r_k = r_{k-2} - q_{k-1}r_{k-1} and repeatedly eliminating rk−1,rk−2,…,r2r_{k-1}, r_{k-2}, \dots, r_2 using the division equations, until only a=r0a = r_0 and b=r1b = r_1 remain.

Proof

Both procedures produce, at the point where all remainders r2,…,rkr_2, \dots, r_k have been eliminated, an expression of the form αa+βb\alpha a + \beta b equal to rkr_k. By the invariant proved above, rk=ska+tkbr_k = s_k a + t_k b is one such expression. It remains to see that back-substitution yields the same α,β\alpha, \beta, not merely some pair satisfying the identity. This follows because at every stage of back-substitution the coefficients of aa and bb are uniquely determined: substituting ri−1=ri−3−qi−2ri−2r_{i-1} = r_{i-3} - q_{i-2} r_{i-2} into an expression already written in the remainders ri−2,ri−1r_{i-2}, r_{i-1} produces a unique new expression in ri−3,ri−2r_{i-3}, r_{i-2} by simple algebraic rearrangement (collecting coefficients of ri−3r_{i-3} and ri−2r_{i-2}), with no choice involved at any stage. Since both the forward recursion and back-substitution implement exactly the same telescoping substitution — merely computed in opposite orders — and both terminate in the unique expression rk=(coeff)⋅a+(coeff)⋅br_k = (\text{coeff}) \cdot a + (\text{coeff}) \cdot b, the coefficients they produce coincide. ■\blacksquare

∎

Worked examples

Example 1: a=1071a = 1071, b=462b = 462 (asymmetric, several steps, negative intermediate coefficients). Run the Euclidean divisions:

1071=2⋅462+147,462=3⋅147+21,147=7⋅21+0.1071 = 2\cdot 462 + 147, \qquad 462 = 3\cdot 147 + 21, \qquad 147 = 7 \cdot 21 + 0.

So r0=1071r_0 = 1071, r1=462r_1 = 462, r2=147r_2 = 147, r3=21r_3 = 21, r4=0r_4 = 0; the gcd is r3=21r_3 = 21, with k=3k = 3, q1=2q_1 = 2, q2=3q_2 = 3, q3=7q_3 = 7. Back-substitution. From the second equation, 147=462−3⋅147147 = 462 - 3\cdot 147... rather, isolate the remainder at each step:

21=462−3⋅147.21 = 462 - 3\cdot 147.

From the first equation, 147=1071−2⋅462147 = 1071 - 2\cdot 462. Substitute:

21=462−3(1071−2⋅462)=462−3⋅1071+6⋅462=7⋅462−3⋅1071.21 = 462 - 3(1071 - 2\cdot 462) = 462 - 3\cdot 1071 + 6\cdot 462 = 7\cdot 462 - 3\cdot 1071.

So

1071⋅(−3)+462⋅(7)=21.1071\cdot(-3) + 462\cdot(7) = 21.

Check directly: 1071⋅(−3)=−32131071\cdot(-3) = -3213, and 462⋅7=3234462 \cdot 7 = 3234; indeed −3213+3234=21-3213 + 3234 = 21. ✓. Forward recursion, as a cross-check. s0=1,t0=0s_0=1, t_0=0; s1=0,t1=1s_1=0,t_1=1.

s2=s0−q1s1=1−2⋅0=1,t2=t0−q1t1=0−2⋅1=−2.s_2 = s_0 - q_1 s_1 = 1 - 2\cdot 0 = 1, \qquad t_2 = t_0 - q_1 t_1 = 0 - 2\cdot 1 = -2.

Check: s2a+t2b=1071−2⋅462=1071−924=147=r2s_2 a + t_2 b = 1071 - 2\cdot 462 = 1071 - 924 = 147 = r_2. ✓

s3=s1−q2s2=0−3⋅1=−3,t3=t1−q2t2=1−3⋅(−2)=1+6=7.s_3 = s_1 - q_2 s_2 = 0 - 3\cdot 1 = -3, \qquad t_3 = t_1 - q_2 t_2 = 1 - 3\cdot(-2) = 1+6 = 7.

Check: s3a+t3b=−3⋅1071+7⋅462=−3213+3234=21=r3=gcd⁡(1071,462)s_3 a + t_3 b = -3\cdot 1071 + 7\cdot 462 = -3213 + 3234 = 21 = r_3 = \gcd(1071,462). ✓ — matching the back-substitution answer exactly, as the corollary guarantees.

Example 2: a=26a = 26, b=91b = 91 (smaller numbers, a<ba<b, and used to extract a modular inverse afterward). Divisions (r0=26,r1=91r_0 = 26, r_1 = 91 — note the algorithm works regardless of which is larger, since the very first division step 26=0⋅91+2626 = 0\cdot 91 + 26 would just swap them; to avoid a wasted step, start directly with the larger one on top): Take instead r0=91,r1=26r_0 = 91, r_1 = 26 (relabeling so a=91,b=26a=91, b=26; a Bezout pair for (91,26)(91,26) converts trivially to one for (26,91)(26,91) by swapping coordinates):

91=3⋅26+13,26=2⋅13+0.91 = 3\cdot 26 + 13, \qquad 26 = 2\cdot 13 + 0.

So r2=13=gcd⁡(91,26)r_2 = 13 = \gcd(91,26), k=2k=2, q1=3q_1 = 3, q2=2q_2 = 2. Back-substitution: directly, 13=91−3⋅2613 = 91 - 3\cdot 26, so

91⋅(1)+26⋅(−3)=13.91\cdot(1) + 26\cdot(-3) = 13.

Check: 91−78=1391 - 78 = 13. ✓. Translating back to the original order (a,b)=(26,91)(a,b)=(26,91): 26⋅(−3)+91⋅(1)=1326\cdot(-3) + 91\cdot(1) = 13, i.e. the Bezout pair for (26,91)(26,91) is (x,y)=(−3,1)(x,y) = (-3,1). Since gcd⁡(26,91)=13≠1\gcd(26,91)=13 \ne 1, this does not directly give an inverse; but suppose instead we wanted the inverse of 2626 modulo 9999 (coprime to 2626). Then a=26, b=99a=26,\,b=99:

99=3⋅26+21,26=1⋅21+5,21=4⋅5+1,5=5⋅1+0.99 = 3\cdot 26 + 21, \qquad 26 = 1\cdot 21 + 5, \qquad 21 = 4\cdot 5 + 1, \qquad 5 = 5\cdot 1 + 0.

Here r0=99,r1=26,r2=21,r3=5,r4=1r_0=99,r_1=26,r_2=21,r_3=5,r_4=1, gcd =1=1, k=4k=4, q1=3,q2=1,q3=4,q4=5q_1=3,q_2=1,q_3=4,q_4=5. Back-substitute starting from the last nontrivial remainder:

1=21−4⋅5.1 = 21 - 4\cdot 5.

Replace 5=26−1⋅215 = 26 - 1\cdot 21:

1=21−4(26−21)=5⋅21−4⋅26.1 = 21 - 4(26-21) = 5\cdot 21 - 4\cdot 26.

Replace 21=99−3⋅2621 = 99 - 3\cdot 26:

1=5(99−3⋅26)−4⋅26=5⋅99−15⋅26−4⋅26=5⋅99−19⋅26.1 = 5(99-3\cdot 26) - 4\cdot 26 = 5\cdot 99 - 15\cdot 26 - 4\cdot 26 = 5\cdot 99 - 19\cdot 26.

So 99⋅5+26⋅(−19)=199\cdot 5 + 26\cdot(-19) = 1, giving the Bezout pair (x,y)=(−19,5)(x,y)=(-19,5) for (a,b)=(26,99)(a,b)=(26,99), i.e.

26⋅(−19)≡1(mod99).26\cdot(-19) \equiv 1 \pmod{99}.

Check: 26×(−19)=−49426\times(-19) = -494, and −494+5×99=−494+495=1-494 + 5\times 99 = -494+495 = 1. ✓ So 26−1≡−19≡80(mod99)26^{-1} \equiv -19 \equiv 80 \pmod{99}; indeed 26×80=2080=21×99+126\times 80 = 2080 = 21\times 99 + 1. ✓ This is exactly the Modular Inverse computation that the Extended Euclidean Algorithm exists to make routine.

CounterexampleDropping "not both zero" / allowing b=0b=0 silently breaks the recursion, not the theorem — showing why the hypothesis on positivity of intermediate divisions matters

The theorem as stated assumes a,b>0a,b>0. If one naively allows b=0b=0 (still with a>0a>0), the very first division step r0=q1r1+r2r_0 = q_1 r_1 + r_2 becomes a=q1⋅0+r2a = q_1\cdot 0 + r_2, which is not a valid instance of the division algorithm — division by 00 is undefined, so q1q_1 cannot be chosen at all, and the recursion for s1,t1s_1,t_1, hence the whole apparatus, never gets started. Concretely, take a=12a = 12, b=0b=0. We know independently that gcd⁡(12,0)=12\gcd(12,0) = 12, and a Bezout pair is visible by inspection: 12⋅1+0⋅t=1212\cdot 1 + 0\cdot t = 12 for any integer tt. But the algorithm's machinery cannot produce this: with r1=0r_1 = 0, the required division r0=q1r1+r2r_0 = q_1 r_1 + r_2 with 0≤r2<r10 \le r_2 < r_1 demands r2<0r_2 < 0, which is impossible, since r1=0r_1=0 forces the remainder bound 0≤r2<00\le r_2<0 to be vacuous/contradictory. So the induction's base step for producing q1q_1 fails outright — there is no valid first division to anchor the invariant r1=s1a+t1br_1 = s_1 a + t_1 b. This shows precisely why the algorithm is stated for a,b>0a,b>0 (equivalently, handles the case b=0b=0 as an explicit base case outside the main loop, returning (1,0)(1,0) immediately) rather than letting the general recursion run unchecked: the hypothesis "r1>0r_1 > 0 whenever a division step is invoked" is what makes each qiq_i well-defined, and removing it produces a division by zero rather than a wrong-but-computable answer.

Scope, edge cases & common traps

SituationWhat to do
aa or bb negativeRun the algorithm on ∣a∣,∣b∣|a|,|b|, get (x0,y0)(x_0,y_0), then flip sign: if a<0a<0 replace x0x_0 by −x0-x_0 (and similarly for bb), since gcd⁡(a,b)=gcd⁡(∣a∣,∣b∣)\gcd(a,b)=\gcd(|a|,|b|).
b=0b = 0Base case: gcd⁡(a,0)=a\gcd(a,0)=a (assuming a>0a>0), Bezout pair (1,0)(1,0); do not enter the division loop.
a=b=0a=b=0gcd⁡(0,0)\gcd(0,0) is conventionally 00; the identity 0⋅x+0⋅y=00\cdot x+0\cdot y=0 holds for all x,yx,y, so Bezout pairs are not unique in the strongest possible sense — this case is usually excluded by hypothesis.
Uniqueness of (x,y)(x,y)Never unique when gcd⁡(a,b)>0\gcd(a,b)>0 and a,ba,b are both nonzero: if (x,y)(x,y) works, so does (x+n b/d, y−n a/d)(x + n\,b/d,\ y - n\,a/d) for any integer nn, where d=gcd⁡(a,b)d=\gcd(a,b).
Want smallest positive xxTake the algorithm's output (x,y)(x,y) and reduce xx modulo b/db/d, adjusting yy to compensate.

The mathematician behind it

Origins · 1 minEuclid of Alexandria and how this was discovered
EO
Euclid of Alexandria
fl. c. 300 BCE · Ptolemaic Egypt, Hellenistic mathematics

Euclid compiled and systematized geometry and number theory in the Elements, including the algorithm for the greatest common measure of two magnitudes (Book VII, Propositions 1–2), which is the ordinary Euclidean Algorithm in essentially its modern form. The extended version — with explicit integer coefficients rather than just the successive remainders — is not present in Euclid's text; it crystallizes only once Bezout's identity is formulated and one asks for its coefficients constructively. The name "Extended Euclidean Algorithm" is thus a later attribution, honoring the parent procedure while marking a genuinely later addition built directly on top of it.

History
- c. 300 BCE — Euclid's Elements, Book VII, states the algorithm for finding the greatest common measure of two magnitudes.
- 1621 — Bachet de Méziriac publishes a version of the linear Diophantine solvability result in Problèmes plaisants et délectables, an early precursor to explicit coefficient-finding.
- 1624 — Bachet's work already contains, in substance, a method equivalent to back-substitution for two specific numerical cases.
- 1779 — Étienne Bézout publishes general results on the theory of resultants and linear combinations of polynomials, later generalized in naming to the integer identity now called Bezout's Identity.
- 20th century — the "extended" formulation with explicit recursive coefficient sequences (si,ti)(s_i,t_i) becomes standard in computational number theory and algorithm design texts, driven by needs in cryptography and computer algebra.
Is the Bezout pair produced by the Extended Euclidean Algorithm unique?

No. If ax0+by0=d=gcd⁡(a,b)ax_0+by_0=d=\gcd(a,b), then for any integer nn, the pair (x0+nbd, y0−nad)\left(x_0+n\frac{b}{d},\ y_0-n\frac{a}{d}\right) also satisfies the identity, since the extra terms nabd−nabdn\frac{ab}{d} - n\frac{ab}{d} cancel. This produces infinitely many valid Bezout pairs whenever a,ba,b are both nonzero. The algorithm returns one specific pair determined by the exact sequence of quotients encountered; a different but equally valid run (for instance starting the division chain from (b,a)(b,a) instead of (a,b)(a,b)) can produce a different pair from this same infinite family. If a unique "canonical" pair is desired, one typically normalizes by requiring xx to lie in a fixed range, such as 0≤x<b/d0 \le x < b/d.

Why not just guess-and-check integer coefficients instead of running the algorithm?

Guessing works for tiny numbers but scales terribly: the search space for (x,y)(x,y) with ax+by=dax+by=d has no natural finite bound to search over without already knowing a solution exists and roughly how large it is. The Extended Euclidean Algorithm instead guarantees termination in O(log⁡min⁡(a,b))O(\log \min(a,b)) steps — the same bound as the plain Euclidean Algorithm — and produces the coefficients mechanically at each step, with no search at all. For numbers with hundreds of digits, as in cryptographic applications, guess-and-check is computationally infeasible, while the extended algorithm remains fast.

How does this connect to computing a modular inverse?

If gcd⁡(a,n)=1\gcd(a,n)=1, running the algorithm on (a,n)(a,n) yields integers x,yx,y with ax+ny=1ax+ny=1. Reducing modulo nn kills the nyny term, leaving ax≡1(modn)ax\equiv 1\pmod n, so x  nx \bmod n is precisely the multiplicative inverse of aa modulo nn. This is the standard, most efficient general method for computing modular inverses, and it is exactly what is used in place of, say, Euler's theorem (which would require computing aφ(n)−1  na^{\varphi(n)-1}\bmod n, generally far more expensive).

Does the algorithm work if aa or bb is negative?

Yes, with a small adjustment. Run the algorithm on the absolute values ∣a∣,∣b∣|a|, |b| to get a Bezout pair (x0,y0)(x_0,y_0) for (∣a∣,∣b∣)(|a|,|b|), using gcd⁡(a,b)=gcd⁡(∣a∣,∣b∣)\gcd(a,b)=\gcd(|a|,|b|). Then negate the corresponding coefficient: if a<0a<0, replace x0x_0 with −x0-x_0; if b<0b<0, replace y0y_0 with −y0-y_0. The resulting pair satisfies ax+by=gcd⁡(∣a∣,∣b∣)ax+by=\gcd(|a|,|b|) because negating both the sign of the input and the sign of its coefficient leaves the product unchanged.

What is the relationship between the sequences si,tis_i,t_i and continued fractions?

The quotients q1,q2,…,qkq_1,q_2,\dots,q_k produced during the Euclidean divisions are exactly the partial quotients of the continued fraction expansion of a/ba/b. The sequences si,tis_i,t_i (up to sign and an index shift) are precisely the numerators and denominators of the convergents of that continued fraction. This is why the Extended Euclidean Algorithm and continued fraction convergents give the same growth-rate and size bounds on the resulting Bezout coefficients, and it is the reason continued fractions provide an alternative computational route to the same coefficients.

Can the algorithm be adapted to polynomials instead of integers?

Yes. Given two polynomials f(x),g(x)f(x), g(x) over a field, the same division-with-remainder structure holds (using polynomial long division and comparing degrees instead of absolute values), so both the ordinary and extended Euclidean Algorithms carry over essentially verbatim, computing gcd⁡(f,g)\gcd(f,g) and polynomials u(x),v(x)u(x), v(x) with uf+vg=gcd⁡(f,g)u f + v g = \gcd(f,g). This polynomial version is a standard tool in computer algebra, coding theory (decoding Reed–Solomon codes), and in constructing field extensions via minimal polynomials.

Is there a matrix formulation of the algorithm?

Yes. Each division step corresponds to multiplying the column vector (ri−1,ri)T(r_{i-1}, r_i)^T by the matrix (011−qi)\begin{pmatrix}0 & 1\\ 1 & -q_i\end{pmatrix} to get (ri,ri+1)T(r_i,r_{i+1})^T. Multiplying all these matrices together in order gives a single 2×22\times2 integer matrix with determinant ±1\pm 1 that directly encodes the final Bezout coefficients as its entries. This viewpoint makes clear why the coefficients are always integers (the matrices are integer matrices with determinant ±1\pm1, hence so is any product) and connects the algorithm to the group SL2(Z)SL_2(\mathbb{Z}).

Before this

Where this leads