The Euclidean Algorithm, Bezout Coefficients and Modular Inverses

1. The key identity

For integers aa and b>0b > 0, divide with remainder: a=qb+ra = qb + r with 0≤r<b0 \le r < b. Then


gcd⁡(a,b)=gcd⁡(b,r).\gcd(a, b) = \gcd(b, r).


Why. If dd divides both aa and bb, it divides r=a−qbr = a - qb. Conversely, if dd divides bb and rr, it divides a=qb+ra = qb + r. The two pairs have exactly the same common divisors, so they have the same greatest common divisor.


Notice which pair survives: the old divisor and the remainder. The pair (dividend, remainder) is not guaranteed to have the same gcd. For example 30=3⋅8+630 = 3 \cdot 8 + 6, and gcd⁡(30,8)=2\gcd(30, 8) = 2, but gcd⁡(30,6)=6\gcd(30, 6) = 6.

2. The algorithm

Repeat the division step until the remainder is 0. The remainders strictly decrease, so the process ends, and the last nonzero remainder is the gcd.


Worked example: gcd⁡(1071,462)\gcd(1071, 462).


Division

Pair passed on

1071=2⋅462+1471071 = 2 \cdot 462 + 147

(462, 147)

462=3⋅147+21462 = 3 \cdot 147 + 21

(147, 21)

147=7⋅21+0147 = 7 \cdot 21 + 0

stop


The last nonzero remainder is 21, so gcd⁡(1071,462)=21\gcd(1071, 462) = 21. Check: 1071=21⋅511071 = 21 \cdot 51 and 462=21⋅22462 = 21 \cdot 22, and gcd⁡(51,22)=1\gcd(51, 22) = 1.


The algorithm is fast. Every two steps at least halve the larger number, so the number of steps grows only with the number of digits, not with the size of the numbers themselves.

3. The extended algorithm and Bezout's identity

Bezout's identity: for integers a,ba, b not both zero, there are integers x,yx, y with ax+by=gcd⁡(a,b)ax + by = \gcd(a, b). The extended algorithm finds them.


Method 1: back-substitution. Rewrite each division as "remainder = dividend - quotient × divisor" and substitute upward, starting from the gcd:


21=462−3⋅147,147=1071−2⋅46221 = 462 - 3 \cdot 147, \qquad 147 = 1071 - 2 \cdot 462


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


So 1071⋅(−3)+462⋅7=211071 \cdot (-3) + 462 \cdot 7 = 21. Check: −3213+3234=21-3213 + 3234 = 21.


Method 2: the running table. Keep rows (ri,si,ti)(r_i, s_i, t_i) with the invariant ri=asi+btir_i = a s_i + b t_i. Start with (a,1,0)(a, 1, 0) and (b,0,1)(b, 0, 1). For each quotient qq, the next row is (previous-but-one row) − q×-\, q \times (previous row). The row whose rr is the gcd gives the coefficients. The table avoids errors in long back-substitutions because every row can be checked on its own.


Coefficients are not unique. If (x,y)(x, y) works, so does (x+bgk, y−agk)\left(x + \tfrac{b}{g}k,\ y - \tfrac{a}{g}k\right) for any integer kk, where g=gcd⁡(a,b)g = \gcd(a, b). The extended algorithm returns one particular pair, typically with ∣x∣≤b2g|x| \le \tfrac{b}{2g} and ∣y∣≤a2g|y| \le \tfrac{a}{2g}. When a problem asks for "the" coefficients, it must specify a range.

4. Modular inverses

An integer uu is an inverse of aa modulo mm when au≡1(modm)au \equiv 1 \pmod m. Such a uu exists exactly when gcd⁡(a,m)=1\gcd(a, m) = 1:


  • If gcd⁡(a,m)=1\gcd(a, m) = 1, Bezout gives ax+my=1ax + my = 1, so ax≡1(modm)ax \equiv 1 \pmod m, and xx reduced into {0,1,…,m−1}\{0, 1, \dots, m-1\} is the inverse.
  • If g=gcd⁡(a,m)>1g = \gcd(a, m) > 1, then gg divides au−kmau - km for all integers u,ku, k, so au−kmau - km can never equal 1.


Worked example: the inverse of 9 modulo 50. Divisions: 50=5⋅9+550 = 5 \cdot 9 + 5, 9=1⋅5+49 = 1 \cdot 5 + 4, 5=1⋅4+15 = 1 \cdot 4 + 1. Back-substitute:


1=5−4=5−(9−5)=2⋅5−9=2(50−5⋅9)−9=2⋅50−11⋅9.1 = 5 - 4 = 5 - (9 - 5) = 2 \cdot 5 - 9 = 2(50 - 5 \cdot 9) - 9 = 2 \cdot 50 - 11 \cdot 9.


The coefficient of 9 is −11-11. It is a valid inverse, but the standard representative lies in 00 to 4949: −11+50=39-11 + 50 = 39. Check: 9⋅39=351=7⋅50+19 \cdot 39 = 351 = 7 \cdot 50 + 1.



The Euclidean algorithm is applied to find gcd⁡(1517,413)\gcd(1517, 413). After the first division step, which pair of numbers is carried forward to the next step?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Use the Euclidean algorithm to find gcd⁡(10857,4389)\gcd(10857, 4389).

Give your answer as a number.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Let g=gcd⁡(1914,899)g = \gcd(1914, 899). There is exactly one pair of integers (x,y)(x, y) with

1914x+899y=gand∣x∣≤15.1914x + 899y = g \quad\text{and}\quad |x| \le 15.

Use the extended Euclidean algorithm to find this pair. Report the value of yy.

Give your answer as a number.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors