Bezout's Identity and the Extended Euclidean Algorithm
See why a greatest common divisor is always a combination of the two numbers, and compute the coefficients with a simple table method.
Key takeaways
- For integers a and b, not both zero, there are integers x and y with ax + by = gcd(a, b).
- The extended Euclidean algorithm tracks x and y alongside the remainders, so no back-tracking is required.
- The equation ax + by = c has integer solutions exactly when gcd(a, b) divides c, and one solution generates them all.
- A number has an inverse modulo m exactly when it shares no factor with m, and this method computes it.
Bezout's identity says that the greatest common divisor of two integers a and b, not both zero, can always be written as ax + by for some integers x and y. For 99 and 78, whose greatest common divisor is 3, one choice is 99 × (−11) + 78 × 14 = 3. The extended Euclidean algorithm is the procedure that finds x and y.
This small fact does heavy work. It tells you which linear equations have whole-number solutions, supplies division in modular arithmetic, and underlies the proof that prime factorization is unique.
A puzzle to begin
You have a 5-liter jug, a 3-liter jug, no markings and a tap. Can you measure 4 liters?
Net amounts you move are whole-number combinations of 5 and 3. Since 5 × 2 − 3 × 2 = 4, filling the large jug twice and emptying the small one twice, in the right order, does it: fill 5, pour into 3 (leaving 2), empty the 3, move the 2 across, fill 5 again, top up the 3 with 1 liter, and 4 liters remain.
With jugs of 6 and 4 liters, every combination 6x + 4y is even, so 3 liters can never appear. The greatest common divisor decides what is reachable.
Working backward through the divisions
Recall the Euclidean algorithm on 1071 and 462.
1071 = 2 × 462 + 147
462 = 3 × 147 + 21
147 = 7 × 21 + 0 Now rewrite the earlier lines to express each remainder, and substitute from the bottom up.
21 = 462 − 3 × 147 second line
147 = 1071 − 2 × 462 first line
21 = 462 − 3 × (1071 − 2 × 462) substitute
21 = 7 × 462 − 3 × 1071 collect terms
check: 3234 − 3213 = 21 So 1071 × (−3) + 462 × 7 = 21. Back substitution is easy to follow for short runs. For longer ones the bookkeeping gets messy, and a table does better.
The table method
Keep rows of the form (r, x, y), each satisfying r = ax + by. Start with the rows for a and b themselves. To make a new row, divide the previous two r values to get a quotient q, then subtract q times the last row from the one before it.
Here a = 99 and b = 78.
| q | r | x | y |
|---|---|---|---|
| start | 99 | 1 | 0 |
| start | 78 | 0 | 1 |
| 1 | 21 | 1 | −1 |
| 3 | 15 | −3 | 4 |
| 1 | 6 | 4 | −5 |
| 2 | 3 | −11 | 14 |
| 2 | 0 | 26 | −33 |
The last row with a nonzero r gives the answer: gcd(99, 78) = 3, with x = −11 and y = 14. Verify: 99 × (−11) = −1089 and 78 × 14 = 1092, and the sum is 3.
Every row stays true because subtracting a multiple of one correct equation from another gives a correct equation. That is the whole justification. The row ending in 0 is also a useful check: 99 × 26 = 2574 = 78 × 33.
A proof without computation
There is also a short argument that the coefficients must exist. Let S be the set of all positive numbers of the form ax + by. It is not empty, so it has a smallest member d.
Divide a by d, writing a = qd + r with 0 ≤ r < d. Since r = a − qd, it is again a combination of a and b. If r were positive, it would be a member of S smaller than d. So r = 0, meaning d divides a. The same holds for b.
Finally, any common divisor of a and b divides ax + by, hence divides d. So d is the greatest common divisor. The argument leans on the well-ordering principle: every nonempty set of positive whole numbers has a least element.
It also shows something further. The combinations ax + by are exactly the multiples of gcd(a, b), no more and no fewer.
Solving ax + by = c
The equation has integer solutions if and only if g = gcd(a, b) divides c. If it does, scale the Bezout coefficients by c ÷ g.
Solve 99x + 78y = 12.
99 × (−11) + 78 × 14 = 3
multiply by 4: x₀ = −44, y₀ = 56
check: −4356 + 4368 = 12 All other solutions come from trading between the two terms:
x = x₀ + k × (b ÷ g) = −44 + 26k
y = y₀ − k × (a ÷ g) = 56 − 33k k any integer Taking k = 2 gives x = 8 and y = −10, and 792 − 780 = 12. These formulas catch every solution because a ÷ g and b ÷ g share no factor. Geometrically, you are picking out the grid points on a straight line, the simplest case of Diophantine equations.
A word problem with nonnegative answers
Real problems often demand that x and y not be negative. Suppose tickets cost 7 dollars for adults and 5 dollars for children, and a group paid exactly 81 dollars. How many of each could there have been?
Solve 7x + 5y = 81. Since 7 × (−2) + 5 × 3 = 1, multiplying by 81 gives x = −162 and y = 243. All solutions are x = −162 + 5k and y = 243 − 7k.
Both counts must be at least 0. The first condition requires 5k ≥ 162, so k ≥ 33. The second requires 7k ≤ 243, so k ≤ 34. Two values survive. With k = 33 there are 3 adults and 12 children, and with k = 34 there are 8 adults and 5 children. Check them: 21 + 60 = 81 and 56 + 25 = 81.
The general solution did the searching for you. It shows that there are exactly two possibilities and that nothing has been overlooked. When the target is large compared with the two prices, nonnegative solutions always exist, and the question of the largest impossible total is the Frobenius coin problem.
Combining more than two numbers
Bezout's identity extends to any list. For 6, 10 and 15, no two are coprime, yet the three together have greatest common divisor 1. Indeed 6 + 10 − 15 = 1.
To find such coefficients in general, work in stages. First write g = gcd(a, b) as ax + by. Then write the greatest common divisor of g and c as gu + cv. Substituting for g gives a combination of all three.
For 6, 10 and 15: gcd(6, 10) = 2 = 6 × 2 + 10 × (−1). Then 1 = 2 × 8 + 15 × (−1). Substituting gives 1 = 6 × 16 + 10 × (−8) + 15 × (−1), which is 96 − 80 − 15. It differs from the quick answer above, a reminder that many choices of coefficients work.
Inverses in modular arithmetic
The inverse of a modulo m is a number x with ax ≡ 1 (mod m), meaning ax leaves remainder 1 on division by m. It plays the role of 1/a. It exists exactly when gcd(a, m) = 1, because then ax + my = 1 has a solution.
Find the inverse of 17 modulo 120.
120 = 7 × 17 + 1
1 = 120 − 7 × 17
so 17 × (−7) ≡ 1 (mod 120)
−7 ≡ 113
check: 17 × 113 = 1921 = 16 × 120 + 1 The inverse is 113. This is the step that turns a public key into a private one in the textbook version of RSA, shown in RSA with small primes. Those toy sizes are for learning only and protect nothing. The same inverses solve linear congruences in general.
Why primes behave as they do
Suppose a prime p divides ab but not a. Then gcd(p, a) = 1, so px + ay = 1. Multiply by b to get pbx + aby = b. Both terms on the left are divisible by p, so p divides b.
That is Euclid's lemma, the key to unique factorization. A slightly wider version holds too: if m divides ab and gcd(m, a) = 1, then m divides b.
Where the name comes from
Étienne Bezout was an eighteenth-century French mathematician who studied the corresponding statement for polynomials. The version for integers had already been described by Claude Gaspard Bachet de Méziriac in 1624. Names in mathematics often honor someone other than the first discoverer.
The polynomial version reads the same way: if f and g are polynomials with rational coefficients and no common factor of positive degree, then there are polynomials u and v with uf + vg = 1. The extended algorithm computes them using polynomial long division.
Common mistakes
- Expecting x and y to be unique. There are infinitely many pairs, spaced b ÷ g apart in x.
- Expecting both to be positive. When a and b are positive and the gcd is smaller than both, one coefficient is typically negative.
- Reading ax + by = d backward: finding that some combination equals d does not prove d is the gcd. It proves only that the gcd divides d. When d = 1, though, the gcd must be 1.
- Forgetting to scale both coefficients when c is a multiple of g.
- Swapping the columns after reordering a and b. Label them clearly.
Writing it as a program
In code, you need to keep only two rows of the table at any moment. Start with (a, 1, 0) and (b, 0, 1). While the newer r is not 0, compute the quotient q, form the new row as the older row minus q times the newer, and discard the older row. When the loop ends, the surviving row with nonzero r is your answer.
It is wise to test the result before using it, because checking is far cheaper than computing: multiply out ax + by and compare it with r. If you work with negative inputs, run the method on absolute values and flip the sign of the matching coefficient at the end. Languages differ in how they compute remainders of negative numbers, which is a common source of bugs.
What the identity does not cover
The statement belongs to systems where division with remainder is available. It fails for polynomials in two variables: x and y share no common factor of positive degree, yet no polynomials u and v make ux + vy equal to 1, since the left side is 0 when x and y are both 0. A similar failure occurs among polynomials with integer coefficients, using 2 and x. Recognizing where the identity holds is one of the first lessons of ring theory.
Try one
Find integers x and y with 35x + 22y = 1. The divisions are 35 = 1 × 22 + 13, 22 = 1 × 13 + 9, 13 = 1 × 9 + 4 and 9 = 2 × 4 + 1. Running the table gives x = −5 and y = 8, and indeed −175 + 176 = 1. So the inverse of 22 modulo 35 is 8.