Sub topic 10
The Extended Euclidean Algorithm and Bézout's Identity
This chapter assumes you can trace Euclid's algorithm by hand and that you understand why it terminates. You should also be comfortable reading Python tuple assignment. In the last chapter, the remainders were the only values that mattered. Here we keep extra bookkeeping alongside them so that the algorithm also tells us how the GCD is built from the inputs.
Bézout's Identity
Bézout's identity says that for any integers a and b, not both zero, there exist integers x and y such that
a·x + b·y = gcd(a, b).
The numbers x and y are called Bézout coefficients. The identity also implies that gcd(a, b) is the smallest positive value of the form a·x + b·y. Every such combination is divisible by the GCD, and the GCD itself can be reached.
For example, gcd(240, 46) = 2, and 240·(−9) + 46·47 = −2160 + 2162 = 2.
The coefficients are not unique. If (x, y) works, then so does (x + k·b/g, y − k·a/g) for any integer k, where g is the GCD. With k = 1 on the example above you get (14, −73), and 240·14 − 46·73 = 3360 − 3358 = 2.
The Key Invariant
Every remainder in Euclid's algorithm is a combination of a and b. The two starting values are trivially combinations:
- a = a·1 + b·0
- b = a·0 + b·1
Each new remainder is computed as rnew = rold − q·r. If both rold and r are combinations of a and b, then rnew is too, and its coefficients come from the same formula: snew = sold − q·s and tnew = told − q·t.
So we carry triples (r, s, t) that always satisfy r = a·s + b·t. When the remainder reaches zero, the previous row holds the GCD and its coefficients.
Tracing 240 and 46
Each row satisfies r = 240·s + 46·t. You can check any row by hand.
| q | r | s | t | Check |
|---|---|---|---|---|
| 240 | 1 | 0 | 240 | |
| 46 | 0 | 1 | 46 | |
| 5 | 10 | 1 | −5 | 240 − 230 = 10 |
| 4 | 6 | −4 | 21 | −960 + 966 = 6 |
| 1 | 4 | 5 | −26 | 1200 − 1196 = 4 |
| 1 | 2 | −9 | 47 | −2160 + 2162 = 2 |
| 2 | 0 | 23 | −120 | 5520 − 5520 = 0 |
The last nonzero remainder is 2, with coefficients (−9, 47). The final row (23, −120) equals (b/g, −a/g), which is a useful sanity check.
Implementation
def extended_gcd(a, b):
# Returns (g, x, y) with a*x + b*y == g == gcd(a, b), for a, b >= 0
old_r, r = a, b
old_s, s = 1, 0
old_t, t = 0, 1
while r != 0:
q = old_r // r
old_r, r = r, old_r - q * r
old_s, s = s, old_s - q * s
old_t, t = t, old_t - q * t
return old_r, old_s, old_t
g, x, y = extended_gcd(240, 46)
print(g, x, y) # 2 -9 47
assert 240 * x + 46 * y == gThe loop does exactly the same number of iterations as plain Euclid, so it still needs O(log min(a, b)) steps. The only extra cost is two multiplications and two subtractions per step.
Common Errors
- Using the wrong row. The answer is in the
old_*variables when the loop exits. The current row holds remainder 0. - Expecting positive coefficients. One coefficient is usually negative. That is normal. Shift by multiples of b/g and a/g if you need a particular sign.
- Negative inputs. This version assumes a, b ≥ 0. For negative inputs, run it on |a| and |b|, then flip the sign of x if a < 0 and the sign of y if b < 0.
- Testing only the GCD. Always assert
a*x + b*y == g. A bug in the coefficient updates leaves g unaffected, so checking g alone will not catch it.
The next chapter uses the coefficient x to compute modular inverses.
Exercise
Trace extended_gcd(99, 78) by hand. What triple (g, x, y) does it return, and does it satisfy 99x + 78y = g?
Show answer
The quotients are 1, 3, 1, 2, 2. The rows (r, s, t) are (99, 1, 0), (78, 0, 1), (21, 1, -1), (15, -3, 4), (6, 4, -5), (3, -11, 14), (0, 26, -33). The function returns (3, -11, 14), and 99·(-11) + 78·14 = -1089 + 1092 = 3.
Exercise
Given that 240·(-9) + 46·47 = 2, find a pair of Bézout coefficients for 240 and 46 in which x is positive and as small as possible.
Show answer
Add b/g = 23 to x and subtract a/g = 120 from y. That gives x = 14 and y = -73, and 240·14 + 46·(-73) = 3360 - 3358 = 2. Since -9 is negative and 14 - 23 < 0, x = 14 is the smallest positive choice.