MATH2400 1,689 words·9 min read

Bezouts Identity and the Extended Euclidean Algorithm

The Euclidean Algorithm in Reverse#

Recall that the Euclidean algorithm finds gcd⁡(a,b)\gcd(a,b) by repeatedly applying the division theorem until the remainder hits 00; the last non-zero remainder is the gcd. Running it on 403403 and 286286:

403=1×286+117,(1)286=2×117+52,(2)117=2×52+13,(3)52=4×13+0,\begin{align*} 403 &= 1 \times 286 + 117, && (1)\\ 286 &= 2 \times 117 + 52, && (2)\\ 117 &= 2 \times 52 + 13, && (3)\\ 52 &= 4 \times 13 + 0, \end{align*}

so gcd⁡(403,286)=13\gcd(403,286) = 13.

Notice how each line lets us rewrite its remainder in terms of the two numbers sitting above it; i.e. line (3)(3) says 13=117−2×5213 = 117 - 2 \times 52. If we start at the penultimate line and keep substituting upwards, we can eventually write the gcd using only 403403 and 286286:

13=117−2×52(from (3))=117−2×(286−2×117)(substituting from (2))=5×117−2×286(collecting terms)=5×(403−286)−2×286(substituting from (1))=5×403−7×286(collecting terms).\begin{align*} 13 &= 117 - 2 \times 52 && \text{(from } (3))\\ &= 117 - 2 \times (286 - 2 \times 117) && \text{(substituting from } (2))\\ &= 5 \times 117 - 2 \times 286 && \text{(collecting terms)}\\ &= 5 \times (403 - 286) - 2 \times 286 && \text{(substituting from } (1))\\ &= 5 \times 403 - 7 \times 286 && \text{(collecting terms)}. \end{align*}

Therefore, we have written gcd⁡(403,286)\gcd(403,286) in the form 403x+286y403x + 286y, specifically with x=5x = 5 and y=−7y = -7. An expression of the form ax+byax+by, where x,y∈Zx,y \in \mathbb{Z}, is called an integer linear combination of aa and bb; basically, what we just showed is that the gcd of two numbers can be built out of the numbers themselves.

When collecting terms, never evaluate the products (do not turn 5×1175 \times 117 into 585585); every line must stay as a combination of two of the remainders, otherwise you lose the structure you are trying to find. The whole point is to keep 403403 and 286286 visible so the final coefficients can be read straight off.

Bézout's Identity#

The back-substitution method clearly works for any pair of integers, not just 403403 and 286286; this generalisation is the main theorem of this lecture.

Note

Theorem (Bézout's Identity)
Given any integers aa and bb, there exist integers xx and yy such that

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

Basically, no matter which two integers you pick, their gcd is always reachable as an integer linear combination of them. The proof approach is exactly what we did above: apply the Euclidean algorithm to aa and bb, then work backwards through the lines, substituting for each remainder until only aa and bb are left.

It is important to note that the solution pair (x,y)(x,y) is not unique. We found 13=5×403+(−7)×28613 = 5 \times 403 + (-7) \times 286, but it is also true that

13=(−17)×403+24×286,13 = (-17) \times 403 + 24 \times 286,

(check: −6851+6864=13-6851 + 6864 = 13). We will see how to produce all such solutions in Topic 4, but a preview is given below.

Example. Find integers xx and yy such that gcd⁡(283,193)=283x+193y\gcd(283,193) = 283x + 193y.
First, run the Euclidean algorithm on 283283 and 193193:

283=1×193+90,(1)193=2×90+13,(2)90=6×13+12,(3)13=1×12+1,(4)12=12×1+0,\begin{align*} 283 &= 1 \times 193 + 90, && (1)\\ 193 &= 2 \times 90 + 13, && (2)\\ 90 &= 6 \times 13 + 12, && (3)\\ 13 &= 1 \times 12 + 1, && (4)\\ 12 &= 12 \times 1 + 0, \end{align*}

so gcd⁡(283,193)=1\gcd(283,193) = 1. Now back-substitute, starting from the penultimate line:

1=13−1×12(from (4))=13−1×(90−6×13)(substituting from (3))=7×13−1×90(collecting terms)=7×(193−2×90)−1×90(substituting from (2))=7×193−15×90(collecting terms)=7×193−15×(283−1×193)(substituting from (1))=22×193−15×283(collecting terms).\begin{align*} 1 &= 13 - 1 \times 12 && \text{(from } (4))\\ &= 13 - 1 \times (90 - 6 \times 13) && \text{(substituting from } (3))\\ &= 7 \times 13 - 1 \times 90 && \text{(collecting terms)}\\ &= 7 \times (193 - 2 \times 90) - 1 \times 90 && \text{(substituting from } (2))\\ &= 7 \times 193 - 15 \times 90 && \text{(collecting terms)}\\ &= 7 \times 193 - 15 \times (283 - 1 \times 193) && \text{(substituting from } (1))\\ &= 22 \times 193 - 15 \times 283 && \text{(collecting terms)}. \end{align*}

Therefore, gcd⁡(283,193)=283×(−15)+193×22\gcd(283,193) = 283 \times (-15) + 193 \times 22; i.e. x=−15x = -15 and y=22y = 22. (Quick sanity check: 4246−4245=14246 - 4245 = 1.)

Example. Find integers xx and yy such that gcd⁡(1001,91)=1001x+91y\gcd(1001, 91) = 1001x + 91y.
This one is an edge case; running the Euclidean algorithm,

1001=11×91+0,1001 = 11 \times 91 + 0,

the remainder is already 00 on the very first line, since 91∣100191 \mid 1001. That's genuinely the entire run, so there is no penultimate line to back-substitute from. When b∣ab \mid a, the gcd is just bb itself, and Bézout's identity is immediate:

gcd⁡(1001,91)=91=1001×0+91×1.\gcd(1001,91) = 91 = 1001 \times 0 + 91 \times 1.

Therefore, (x,y)=(0,1)(x,y) = (0,1). Don't go hunting for substitutions that don't exist.

Bézout in Reverse#

It is important to remember that Bézout's identity cannot be used in reverse; being able to write d=ax+byd = ax + by does not mean that d=gcd⁡(a,b)d = \gcd(a,b). For instance, 52=403×(−2)+286×352 = 403 \times (-2) + 286 \times 3, but gcd⁡(403,286)\gcd(403,286) is certainly not 5252. However, the following weaker statement is true.

Note

Theorem
Given d=ax+byd = ax + by for some integers a,b,x,ya,b,x,y, we have that

gcd⁡(a,b)∣d.\gcd(a,b) \mid d.

Proof. Let g=gcd⁡(a,b)g = \gcd(a,b). Then g∣ag \mid a and g∣bg \mid b, so by Lemma 1.3 from Divisibility and Primes, gg divides any integer linear combination of aa and bb;

g∣ax+by,i.e. g∣d.■g \mid ax + by, \quad \text{i.e. } g \mid d. \quad \blacksquare

Basically, every integer linear combination of aa and bb is a multiple of gcd⁡(a,b)\gcd(a,b); the gcd is the smallest positive number you can build this way, and everything else you can build is a multiple of it. That is why 5252 showed up above: 5252 is a multiple of 1313.

Combining Bézout's identity with this weaker converse gives us a very useful test for coprimality.

Note

Corollary
Integers aa and bb satisfy gcd⁡(a,b)=1\gcd(a,b)=1 if and only if there exist integers xx and yy such that

ax+by=1.ax + by = 1.

Proof. If gcd⁡(a,b)=1\gcd(a,b) = 1, then Bézout's identity directly provides such xx and yy. Conversely, if ax+by=1ax+by=1 then by the previous theorem gcd⁡(a,b)∣1\gcd(a,b) \mid 1; since the gcd is positive, gcd⁡(a,b)=1\gcd(a,b) = 1. ■\blacksquare

Recall that two integers with gcd⁡(a,b)=1\gcd(a,b)=1 are called coprime (or relatively prime). The power of this corollary is the backwards direction: if you can exhibit any combination equal to 11, you get the gcd for free, with no Euclidean algorithm needed.

Example. Show that any two consecutive integers are coprime.
Let n∈Zn \in \mathbb{Z} and consider nn and n+1n+1. Notice that

(n+1)×1+n×(−1)=n+1−n=1,\begin{align*} (n+1) \times 1 + n \times (-1) &= n + 1 - n \\ &= 1, \end{align*}

so we have written 11 as an integer linear combination of n+1n+1 and nn (with x=1x = 1 and y=−1y = -1). Therefore, by the corollary, gcd⁡(n,n+1)=1\gcd(n, n+1) = 1 for every integer nn; consecutive integers are always coprime.

Using Bézout's Identity in Proofs#

Recall the following theorem from Lecture 1.1, which we stated without proof; we now have the machinery to prove it.

Note

Theorem (Euclid's Lemma)
Suppose that aa and bb are integers and pp is a prime integer. If p∣abp \mid ab, then p∣ap \mid a or p∣bp \mid b.

Proof. Suppose that p∣abp \mid ab. If p∣ap \mid a, we are done; so suppose instead that p∤ap \nmid a. Since pp is prime, its only divisors are ±1\pm 1 and ±p\pm p, and because p∤ap \nmid a, the only common divisors of pp and aa are ±1\pm 1; hence gcd⁡(p,a)=1\gcd(p,a) = 1. By Bézout's identity, there exist integers xx and yy such that

px+ay=1.px + ay = 1.

Multiplying both sides by bb (to force an abab term to appear),

pbx+aby=b.\begin{align*} pbx + aby &= b. \end{align*}

Now, p∣pbxp \mid pbx trivially, and p∣abyp \mid aby since p∣abp \mid ab by assumption; therefore pp divides their sum,

p∣pbx+aby,i.e. p∣b.■p \mid pbx + aby, \quad \text{i.e. } p \mid b. \quad \blacksquare

This "multiply the Bézout equation by whatever you need" trick is worth remembering; it appears constantly in number theory proofs.

The Extended Euclidean Algorithm#

Back-substitution works, but it is slow and easy to fumble when there are many lines; the extended Euclidean algorithm packages the exact same computation into a table.

Consider again the Euclidean algorithm for 403403 and 286286. Notice that we only ever care about the quotient and the remainder in each line. Writing qiq_i for the quotient and rir_i for the remainder found in line ii, and seeding the remainder row with the original numbers 403403 and 286286, the whole algorithm compresses to:

qiq_i 11 22 22 44
rir_i 403403 286286 117117 5252 1313 00

Each remainder is obtained from the two before it via

ri=ri−2−qi×ri−1;r_i = r_{i-2} - q_i \times r_{i-1};

i.e. divide the remainder two spots to the left by the one directly to the left, record the quotient qiq_i up top, and the remainder rir_i below it.

The extended Euclidean algorithm (EEA) performs exactly these steps, except it introduces two new rows for xix_i and yiy_i, seeded with (1,0)(1,0) and (0,1)(0,1) respectively, which obey the same recursion as the remainders:

xi=xi−2−qixi−1,yi=yi−2−qiyi−1.x_i = x_{i-2} - q_i x_{i-1}, \qquad y_i = y_{i-2} - q_i y_{i-1}.

In words: each new entry is the entry two spaces to its left, minus the qiq_i above it times the entry directly to its left. Applying this to 403403 and 286286, we start with the unfilled table

qiq_i
rir_i 403403 286286
xix_i 11 00
yiy_i 00 11

and filling it out column by column gives:

qiq_i 11 22 22 44
rir_i 403403 286286 117117 5252 1313 00
xix_i 11 00 11 −2-2 55 −22-22
yiy_i 00 11 −1-1 33 −7-7 3131

The magic of this table is that in every column,

ri=axi+byi.\boxed{r_i = ax_i + by_i.}

You can check this on any column you like; e.g. the ri=52r_i = 52 column gives 403×(−2)+286×3=−806+858=52403 \times (-2) + 286 \times 3 = -806 + 858 = 52. In particular, the second-last column (the one where ri=gcd⁡(a,b)r_i = \gcd(a,b)) tells us that 13=5×403−7×28613 = 5 \times 403 - 7 \times 286, exactly matching the back-substitution answer from earlier.

Note

Algorithm (Extended Euclidean Algorithm)
Given integers aa and bb, to find integers xx and yy such that gcd⁡(a,b)=ax+by\gcd(a,b) = ax+by:

  • Start with the unfilled table: seed the rir_i row with aa and bb, the xix_i row with 1,01, 0 and the yiy_i row with 0,10, 1.
  • Fill the top two rows by applying the Euclidean algorithm to aa and bb as usual, recording each quotient qiq_i and remainder rir_i (with practice this can be done directly in the table).
  • Fill the bottom two rows using the recursive formulae xi=xi−2−qixi−1x_i = x_{i-2} - q_i x_{i-1} and yi=yi−2−qiyi−1y_i = y_{i-2} - q_i y_{i-1}.
  • In every column, ri=axi+byir_i = ax_i + by_i; so the required xx and yy are the xix_i and yiy_i entries in the second-last column, where ri=gcd⁡(a,b)r_i = \gcd(a,b).

A good habit is to always verify the second-last column by actually computing axi+byiax_i + by_i before writing down your final answer; it is a two-second check that catches almost every arithmetic slip in the table.

Example. Find integers xx and yy such that gcd⁡(283,193)=283x+193y\gcd(283,193) = 283x + 193y, using the EEA.
We already know the quotients from the earlier run (1,2,6,1,121, 2, 6, 1, 12), so we fill out the table:

qiq_i 11 22 66 11 1212
rir_i 283283 193193 9090 1313 1212 11 00
xix_i 11 00 11 −2-2 1313 −15-15 193193
yiy_i 00 11 −1-1 33 −19-19 2222 −283-283

For instance, the xix_i entry under qi=6q_i = 6 is 1−6×(−2)=131 - 6\times(-2) = 13, and the yiy_i entry below it is −1−6×3=−19-1 - 6 \times 3 = -19. Reading off the second-last column (where ri=1=gcd⁡(283,193)r_i = 1 = \gcd(283,193)):

1=283×(−15)+193×22=−4245+4246=1. ✓\begin{align*} 1 &= 283 \times (-15) + 193 \times 22 \\ &= -4245 + 4246 \\ &= 1. \ \checkmark \end{align*}

Therefore, x=−15x = -15 and y=22y = 22, agreeing with the back-substitution method.

Notice how both methods are computing the same thing; for two or three division steps, back-substitution is perfectly fine, but once the Euclidean algorithm takes four or more lines (like this one), the table is faster and far less error-prone, since you never have to expand brackets.

Solving ax + by = c in General#

Bézout's identity only directly produces combinations equal to the gcd; a natural exam-style question is whether ax+by=cax + by = c can be solved for other values of cc.

Note

Theorem
Given integers a,b,ca, b, c, the equation ax+by=cax + by = c has integer solutions x,yx, y if and only if

gcd⁡(a,b)∣c.\gcd(a,b) \mid c.

Proof. If a solution exists, then cc is an integer linear combination of aa and bb, so gcd⁡(a,b)∣c\gcd(a,b) \mid c by the weaker theorem above. Conversely, if c=kgcd⁡(a,b)c = k \gcd(a,b) for some k∈Zk \in \mathbb{Z}, take a Bézout pair (x0,y0)(x_0, y_0) with ax0+by0=gcd⁡(a,b)ax_0 + by_0 = \gcd(a,b) and multiply through by kk; then x=kx0x = kx_0, y=ky0y = ky_0 is a solution. ■\blacksquare

Basically, the reachable values of ax+byax+by are precisely the multiples of the gcd; nothing more, nothing less. So the decision process for these questions is: compute the gcd first, then check divisibility.

Example. Find integers xx and yy such that 12x+20y=312x + 20y = 3, or explain why none exist.
Running the Euclidean algorithm on 2020 and 1212:

20=1×12+8,12=1×8+4,8=2×4+0,\begin{align*} 20 &= 1 \times 12 + 8, \\ 12 &= 1 \times 8 + 4, \\ 8 &= 2 \times 4 + 0, \end{align*}

so gcd⁡(12,20)=4\gcd(12,20) = 4. Since 4∤34 \nmid 3, no integer solutions exist; every integer linear combination of 1212 and 2020 is a multiple of 44, and 33 is not. Do not waste time back-substituting when the divisibility check already fails.

Example. Find integers xx and yy such that 12x+20y=812x + 20y = 8.
This time gcd⁡(12,20)=4\gcd(12,20) = 4 and 4∣84 \mid 8, so solutions exist. First find a Bézout pair for the gcd by back-substituting the run above:

4=12−1×8(from the second line)=12−1×(20−1×12)(substituting from the first line)=2×12−1×20(collecting terms).\begin{align*} 4 &= 12 - 1 \times 8 && \text{(from the second line)}\\ &= 12 - 1 \times (20 - 1 \times 12) && \text{(substituting from the first line)}\\ &= 2 \times 12 - 1 \times 20 && \text{(collecting terms)}. \end{align*}

Since 8=2×48 = 2 \times 4, multiply the whole identity through by 22:

8=4×12−2×20=48−40=8. ✓\begin{align*} 8 &= 4 \times 12 - 2 \times 20 \\ &= 48 - 40 \\ &= 8. \ \checkmark \end{align*}

Therefore, (x,y)=(4,−2)(x,y) = (4,-2) is a solution. The general strategy: solve for the gcd first, then scale.

Generating More Solutions#

We noted earlier that Bézout pairs are not unique; here is how to manufacture new solutions from an old one.

Example. Given that gcd⁡(403,286)=13=5×403−7×286\gcd(403,286) = 13 = 5 \times 403 - 7 \times 286, find a second pair (x,y)(x,y) with 403x+286y=13403x + 286y = 13.
The trick is that we can shift xx by −bd-\frac{b}{d} and yy by ad\frac{a}{d} (where d=gcd⁡(a,b)d = \gcd(a,b)) without changing the value, because the two changes cancel exactly:

a(x−bd)+b(y+ad)=ax+by−abd+abd=ax+by.\begin{align*} a\left(x - \tfrac{b}{d}\right) + b\left(y + \tfrac{a}{d}\right) &= ax + by - \tfrac{ab}{d} + \tfrac{ab}{d} \\ &= ax + by. \end{align*}

Here bd=28613=22\frac{b}{d} = \frac{286}{13} = 22 and ad=40313=31\frac{a}{d} = \frac{403}{13} = 31, so from (5,−7)(5,-7) we get

(x,y)=(5−22,−7+31)=(−17,24),\begin{align*} (x, y) &= (5 - 22, -7 + 31) \\ &= (-17, 24), \end{align*}

and indeed 403×(−17)+286×24=−6851+6864=13403 \times (-17) + 286 \times 24 = -6851 + 6864 = 13. ✓\checkmark

Therefore, (−17,24)(-17, 24) is a second solution, and repeating the shift in either direction generates infinitely many. Notice how the last column of the EEA table for 403403 and 286286 was (xi,yi)=(−22,31)(x_i, y_i) = (-22, 31); that "useless" column under ri=0r_i = 0 is exactly the shift (±bd,∓ad)\left(\pm\frac{b}{d}, \mp\frac{a}{d}\right), so the table hands you the recipe for new solutions for free.

A Preview of Modular Inverses#

Here is a look ahead at why the coprime criterion matters so much; this idea will carry most of the modular arithmetic later in the course.

An inverse of aa modulo nn is an integer xx such that axax leaves remainder 11 when divided by nn; i.e. ax−1=nkax - 1 = nk for some k∈Zk \in \mathbb{Z}. Rearranging,

ax+n(−k)=1,ax + n(-k) = 1,

which is precisely a statement that 11 is an integer linear combination of aa and nn. By the coprime criterion, such xx and kk exist if and only if gcd⁡(a,n)=1\gcd(a,n) = 1; and when the inverse exists, the extended Euclidean algorithm is exactly the tool that finds it.

Example. Find an integer xx such that 7x7x leaves remainder 11 when divided by 3030.
First check existence: run the Euclidean algorithm on 3030 and 77,

30=4×7+2,(1)7=3×2+1,(2)2=2×1+0,\begin{align*} 30 &= 4 \times 7 + 2, && (1)\\ 7 &= 3 \times 2 + 1, && (2)\\ 2 &= 2 \times 1 + 0, \end{align*}

so gcd⁡(30,7)=1\gcd(30,7) = 1 and an inverse exists. Now back-substitute:

1=7−3×2(from (2))=7−3×(30−4×7)(substituting from (1))=13×7−3×30(collecting terms).\begin{align*} 1 &= 7 - 3 \times 2 && \text{(from } (2))\\ &= 7 - 3 \times (30 - 4 \times 7) && \text{(substituting from } (1))\\ &= 13 \times 7 - 3 \times 30 && \text{(collecting terms)}. \end{align*}

So 7×13=1+3×307 \times 13 = 1 + 3 \times 30; i.e. 7×13=917 \times 13 = 91 leaves remainder 11 when divided by 3030. Therefore, the inverse of 77 modulo 3030 is x=13x = 13.

By contrast, 66 has no inverse modulo 3030: gcd⁡(6,30)=6≠1\gcd(6,30) = 6 \neq 1, so 6x+30(−k)=16x + 30(-k) = 1 would force 6∣16 \mid 1, which is impossible. This is the standard exam trap; always check the gcd before hunting for an inverse.