The Euclidean Algorithm
The Euclidean Algorithm, Bezout Coefficients and Modular Inverses
1. The key identity
For integers a and b>0, divide with remainder: a=qb+r with 0≤r<b. Then
Why. If d divides both a and b, it divides r=a−qb. Conversely, if d divides b and r, it divides a=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+6, and gcd(30,8)=2, but 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).
Division | Pair passed on |
|---|---|
1071=2⋅462+147 | (462, 147) |
462=3⋅147+21 | (147, 21) |
147=7⋅21+0 | stop |
The last nonzero remainder is 21, so gcd(1071,462)=21. Check: 1071=21⋅51 and 462=21⋅22, and 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,b not both zero, there are integers x,y with 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:
So 1071⋅(−3)+462⋅7=21. Check: −3213+3234=21.
Method 2: the running table. Keep rows (ri,si,ti) with the invariant ri=asi+bti. Start with (a,1,0) and (b,0,1). For each quotient q, the next row is (previous-but-one row) −q× (previous row). The row whose r 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) works, so does (x+gbk, y−gak) for any integer k, where g=gcd(a,b). The extended algorithm returns one particular pair, typically with ∣x∣≤2gb and ∣y∣≤2ga. When a problem asks for "the" coefficients, it must specify a range.
4. Modular inverses
An integer u is an inverse of a modulo m when au≡1(modm). Such a u exists exactly when gcd(a,m)=1:
- If gcd(a,m)=1, Bezout gives ax+my=1, so ax≡1(modm), and x reduced into {0,1,…,m−1} is the inverse.
- If g=gcd(a,m)>1, then g divides au−km for all integers u,k, so au−km can never equal 1.
Worked example: the inverse of 9 modulo 50. Divisions: 50=5⋅9+5, 9=1⋅5+4, 5=1⋅4+1. Back-substitute:
The coefficient of 9 is −11. It is a valid inverse, but the standard representative lies in 0 to 49: −11+50=39. Check: 9⋅39=351=7⋅50+1.
Use the Euclidean algorithm to find gcd(10857,4389).
Give your answer as a number.
Let g=gcd(1914,899). There is exactly one pair of integers (x,y) with
Use the extended Euclidean algorithm to find this pair. Report the value of y.
Give your answer as a number.