2.5.8Number Theory (Intermediate)

Extended Euclidean algorithm

2,200 words10 min readdifficulty · medium3 backlinks

Derivation from First Principles

How the Regular Algorithm Works

Start with ab>0a \geq b > 0. The Euclidean algorithm produces a sequence:

a=q1b+r1(0r1<b)b=q2r1+r2(0r2<r1)r1=q3r2+r3rn2=qnrn1+rnrn1=qn+1rn+0\begin{align} a &= q_1 b + r_1 \quad (0 \leq r_1 < b) \\ b &= q_2 r_1 + r_2 \quad (0 \leq r_2 < r_1) \\ r_1 &= q_3 r_2 + r_3 \\ &\vdots \\ r_{n-2} &= q_n r_{n-1} + r_n \\ r_{n-1} &= q_{n+1} r_n + 0 \end{align}

The last non-zero remainder rnr_n is gcd(a,b)\gcd(a, b).

Tracking Coefficients Backwards

From the second-to-last equation: rn=rn2qnrn1r_n = r_{n-2} - q_n r_{n-1}.

Substitute the expression for rn1r_{n-1} from the line above, then rn2r_{n-2}, and so on, all the way back to aa and bb. This substitution reveals rn=xa+ybr_n = x \cdot a + y \cdot b for some integers x,yx, y.

The Forward (Efficient) Method

Instead of backtracking, maintain coefficients (xi,yi)(x_i, y_i) at each step such that ri=xia+yibr_i = x_i a + y_i b.

Base cases:

  • r0=a=1a+0b    (x0,y0)=(1,0)r_0 = a = 1\cdot a + 0 \cdot b \implies (x_0, y_0) = (1, 0)
  • r1=b=0a+1b    (x1,y1)=(0,1)r_1 = b = 0 \cdot a + 1 \cdot b \implies (x_1, y_1) = (0, 1)

Recurrence: From ri=ri2qiri1r_i = r_{i-2} - q_i r_{i-1}, we have: ri=(xi2a+yi2b)qi(xi1a+yi1b)r_i = (x_{i-2} a + y_{i-2} b) - q_i (x_{i-1} a + y_{i-1} b) =(xi2qixi1)a+(yi2qiyi1)b= (x_{i-2} - q_i x_{i-1}) a + (y_{i-2} - q_i y_{i-1}) b

Why this step? Each remainder is a linear combination of the previous two. If we know how to write the previous two in terms of aa and bb, we can write the new one the same way.


Worked Examples

Step ri2r_{i-2} ri1r_{i-1} qiq_i rir_i xi2x_{i-2} xi1x_{i-1} xi=xi2qixi1x_i = x_{i-2} - q_i x_{i-1} yi2y_{i-2} yi1y_{i-1} yi=yi2qiyi1y_i = y_{i-2} - q_i y_{i-1}
0 56 1 0
1 15 0 1
2 56 15 3 11 1 0 130=11 - 3 \cdot 0 = 1 0 1 031=30 - 3 \cdot 1 = -3
3 15 11 1 4 0 1 011=10 - 1 \cdot 1 = -1 1 -3 11(3)=41 - 1 \cdot (-3) = 4
4 11 4 2 3 1 -1 12(1)=31 - 2 \cdot (-1) = 3 -3 4 324=11-3 - 2 \cdot 4 = -11
5 4 3 1 1 -1 3 113=4-1 - 1 \cdot 3 = -4 4 -11 41(11)=154 - 1 \cdot (-11) = 15
6 3 1 3 0

Result: gcd(56,15)=1\gcd(56, 15) = 1, with x=4x = -4, y=15y = 15.

Verification: 56(4)+1515=224+225=156 \cdot (-4) + 15 \cdot 15 = -224 + 225 = 1. ✓

Why this step? Each row computes the new remainder and updates coefficients using the recurrence. When ri=0r_i = 0, the previous row gives the answer.


Step ri2r_{i-2} ri1r_{i-1} qiq_i rir_i xix_i yiy_i
0 240 1 0
1 46 0 1
2 240 46 5 10 1 - 5·0 = 1 0 - 5·1 = -5
3 46 10 4 6 0 - 4·1 = -4 1 - 4·(-5) = 21
4 10 6 1 4 1 - 1·(-4) = 5 -5 - 1·21 = -26
5 6 4 1 2 -4 - 1·5 = -9 21 - 1·(-26) = 47
6 4 2 2 0

Result: gcd(240,46)=2\gcd(240, 46) = 2, with x=9x = -9, y=47y = 47.

Check: 240(9)+4647=2160+2162=2240 \cdot (-9) + 46 \cdot 47 = -2160 + 2162 = 2. ✓


Common Mistakes


Applications

  1. Modular Inverse: To find a1(modm)a^{-1} \pmod{m} (when gcd(a,m)=1\gcd(a, m) = 1), run Extended Euclidean on aa and mm. The coefficient xx satisfying ax+my=1ax + my = 1 is the inverse: ax1(modm)ax \equiv 1 \pmod{m}.

  2. Linear Diophantine Equations: To solve ax+by=cax + by = c, first find gcd(a,b)\gcd(a,b). If cc is not a multiple of gcd(a,b)\gcd(a,b), no solution exists. Otherwise, scale the Bézout coefficients: if ax0+by0=gcd(a,b)ax_0 + by_0 = \gcd(a,b), then x=x0(c/gcd(a,b))x = x_0 \cdot (c / \gcd(a,b)) and y=y0(c/gcd(a,b))y = y_0 \cdot (c / \gcd(a,b)) is a particular solution.

  3. RSA Cryptography: Computing the private key dd such that ed1(modϕ(n))ed \equiv 1 \pmod{\phi(n)} uses the Extended Euclidean Algorithm.


Diagram

Figure — Extended Euclidean algorithm

Recall Feynman: Explain to a 12-year-old

Imagine you have two piles of coins, say 56 and 15. The regular Euclidean algorithm is like repeatedly removing the smaller pile from the larger until you're left with the biggest pile size that divides both (the gcd).

Now, the Extended version asks: "Can I combine the original two pile sizes using addition and subtraction to build exactly this gcd?" The answer is always yes! The algorithm keeps a running tally: "So far, this remainder is made of __ copies of the first pile and ___ copies of the second pile (where negative means we're subtracting)."

At the end, you know exactly how many of each original pile you need to add/subtract to get the gcd. That's the magic formula ax+by=gcd(a,b)ax + by = \gcd(a,b) and it's used everywhere in number theory!



Connections

  • Euclidean Algorithm — the "non-extended" version that only finds gcd(a,b)\gcd(a,b)
  • Bézout's Identity — theorem guaranteeing integer solutions ax+by=gcd(a,b)ax + by = \gcd(a,b)
  • Modular Multiplicative Inverse — found using Extended Euclidean when gcd(a,m)=1\gcd(a, m) = 1
  • Linear Diophantine Equations — solved by scaling Bézout coefficients
  • Chinese Remainder Theorem — uses modular inverses in its construction
  • RSA Algorithm — private key calculation relies on Extended Euclidean

#flashcards/maths

What does the Extended Euclidean Algorithm compute? :: It computes gcd(a,b)\gcd(a, b) and integers x,yx, y such that ax+by=gcd(a,b)ax + by = \gcd(a, b) (Bézout's identity).

What are the base cases for the coefficient recurrence?
(x0,y0)=(1,0)(x_0, y_0) = (1, 0) for r0=ar_0 = a, and (x1,y1)=(0,1)(x_1, y_1) = (0, 1) for r1=br_1 = b.
What is the recurrence for xix_i in the Extended Euclidean Algorithm?
xi=xi2qixi1x_i = x_{i-2} - q_i \cdot x_{i-1}, where qiq_i is the quotient at step ii.

How do you find the modular inverse of aa modulo mm using Extended Euclidean? :: Run Extended Euclidean on aa and mm. If gcd(a,m)=1\gcd(a, m) = 1, the coefficient xx from ax+my=1ax + my = 1 is the inverse a1(modm)a^{-1} \pmod{m}.

When does the linear Diophantine equation ax+by=cax + by = c have integer solutions?
If and only if gcd(a,b)\gcd(a, b) divides cc.

If 56x+15y=gcd(56,15)56x + 15y = \gcd(56, 15), what are xx and yy? :: x=4x = -4, y=15y = 15, since 56(4)+15(15)=156(-4) + 15(15) = 1 and gcd(56,15)=1\gcd(56, 15) = 1.

Why do we use the second-to-last row for the gcd and coefficients?
The last remainder is always 0. The gcd is the last non-zero remainder, which appears in the second-to-last row.
What is Bézout's Identity?
For any integers a,ba, b (not both zero), there exist integers x,yx, y such that ax+by=gcd(a,b)ax + by = \gcd(a, b).

Concept Map

computes

tracks coefficients

finds x and y

linear combination of

expressed via

gives coefficients

start

final row is

enables

last nonzero equals

Euclidean algorithm

gcd of a and b

Extended Euclidean algorithm

Bezout identity ax + by = gcd

Each remainder r_i

a and b

Recurrence r_i = r_i-2 - q_i r_i-1

x_i = x_i-2 - q_i x_i-1

Base cases 1,0 and 0,1

Modular inverses, RSA, Diophantine

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Extended Euclidean Algorithm ek powerful technique hai jo tumhe sirf gcd nahi, balki ek special combination bhi deta hai: ax+by=gcd(a,b)ax + by = \gcd(a,b). Matlab agar tumhare pas do numbers hain, toh yeh algorithm tumhe bata dega kitne copies first number ke aur kitne copies second number ke (addition ya subtraction ke sath) mil kar exactly gcd ban sakta hai. Yeh Bézout's identity kehlata hai aur number theory mein bahut kaam ata hai.

Algorithm simple hai — regular Euclidean algorithm ki tarah divisions karte raho, lekin sath-saath do extra columns maintain karo: xix_i aur yiy_i. Har step par recurrence use karo: xi=xi2qixi1x_i = x_{i-2} - q_i \cdot x_{i-1} (same for yiy_i). Base case yad rakho: pehla number (1,0)(1, 0) se start hota hai aur dosra (0,1)(0, 1) se, kyunki a=1a+0ba = 1 \cdot a + 0 \cdot b aur b=0a+1bb = 0 \cdot a + 1 \cdot b.

Iska sabse bada use modular inverse nikalne mein hota hai. Agar tumhe a1(modm)a^{-1} \pmod{m} chahiye (cryptography aur coding theory mein zaroori), toh Extended Euclidean chala do aa aur mm par. Jo xx milega wahi inverse hai! Linear Diophantine equations solve karne ke liye bhi yahi method use hota hai. Basically, yeh algorithm tumhe number theory ke bahut sare problems ka direct solution de deta hai, efficiently aur exactly.

Go deeper — visual, from zero

Test yourself — Number Theory (Intermediate)

Connections