Bezouts Identity and the Extended Euclidean Algorithm

The Euclidean Algorithm in Reverse

Recall that the Euclidean algorithm finds by repeatedly applying the division theorem until the remainder hits ; the last non-zero remainder is the gcd. Running it on and :

so .

Notice how each line lets us rewrite its remainder in terms of the two numbers sitting above it; i.e. line says . If we start at the penultimate line and keep substituting upwards, we can eventually write the gcd using only and :

Therefore, we have written in the form , specifically with and . An expression of the form , where , is called an integer linear combination of and ; 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 into ); 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 and 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 and ; this generalisation is the main theorem of this lecture.

Note

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

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 and , then work backwards through the lines, substituting for each remainder until only and are left.

It is important to note that the solution pair is not unique. We found , but it is also true that

(check: ). We will see how to produce all such solutions in Topic 4, but a preview is given below.

Example. Find integers and such that .
First, run the Euclidean algorithm on and :

so . Now back-substitute, starting from the penultimate line:

Therefore, ; i.e. and . (Quick sanity check: .)

Example. Find integers and such that .
This one is an edge case; running the Euclidean algorithm,

the remainder is already on the very first line, since . That's genuinely the entire run, so there is no penultimate line to back-substitute from. When , the gcd is just itself, and Bézout's identity is immediate:

Therefore, . 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 does not mean that . For instance, , but is certainly not . However, the following weaker statement is true.

Note

Theorem
Given for some integers , we have that

Proof. Let . Then and , so by Lemma 1.3 from Divisibility and Primes, divides any integer linear combination of and ;

Basically, every integer linear combination of and is a multiple of ; 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 showed up above: is a multiple of .

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

Note

Corollary
Integers and satisfy if and only if there exist integers and such that

Proof. If , then Bézout's identity directly provides such and . Conversely, if then by the previous theorem ; since the gcd is positive, .

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

Example. Show that any two consecutive integers are coprime.
Let and consider and . Notice that

so we have written as an integer linear combination of and (with and ). Therefore, by the corollary, for every integer ; 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 and are integers and is a prime integer. If , then or .

Proof. Suppose that . If , we are done; so suppose instead that . Since is prime, its only divisors are and , and because , the only common divisors of and are ; hence . By Bézout's identity, there exist integers and such that

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

Now, trivially, and since by assumption; therefore divides their sum,

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 and . Notice that we only ever care about the quotient and the remainder in each line. Writing for the quotient and for the remainder found in line , and seeding the remainder row with the original numbers and , the whole algorithm compresses to:

Each remainder is obtained from the two before it via

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

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

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

and filling it out column by column gives:

The magic of this table is that in every column,

You can check this on any column you like; e.g. the column gives . In particular, the second-last column (the one where ) tells us that , exactly matching the back-substitution answer from earlier.

Note

Algorithm (Extended Euclidean Algorithm)
Given integers and , to find integers and such that :

  • Start with the unfilled table: seed the row with and , the row with and the row with .
  • Fill the top two rows by applying the Euclidean algorithm to and as usual, recording each quotient and remainder (with practice this can be done directly in the table).
  • Fill the bottom two rows using the recursive formulae and .
  • In every column, ; so the required and are the and entries in the second-last column, where .

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

Example. Find integers and such that , using the EEA.
We already know the quotients from the earlier run (), so we fill out the table:

For instance, the entry under is , and the entry below it is . Reading off the second-last column (where ):

Therefore, and , 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 can be solved for other values of .

Note

Theorem
Given integers , the equation has integer solutions if and only if

Proof. If a solution exists, then is an integer linear combination of and , so by the weaker theorem above. Conversely, if for some , take a Bézout pair with and multiply through by ; then , is a solution.

Basically, the reachable values of 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 and such that , or explain why none exist.
Running the Euclidean algorithm on and :

so . Since , no integer solutions exist; every integer linear combination of and is a multiple of , and is not. Do not waste time back-substituting when the divisibility check already fails.

Example. Find integers and such that .
This time and , so solutions exist. First find a Bézout pair for the gcd by back-substituting the run above:

Since , multiply the whole identity through by :

Therefore, 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 , find a second pair with .
The trick is that we can shift by and by (where ) without changing the value, because the two changes cancel exactly:

Here and , so from we get

and indeed .

Therefore, is a second solution, and repeating the shift in either direction generates infinitely many. Notice how the last column of the EEA table for and was ; that "useless" column under is exactly the shift , 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 modulo is an integer such that leaves remainder when divided by ; i.e. for some . Rearranging,

which is precisely a statement that is an integer linear combination of and . By the coprime criterion, such and exist if and only if ; and when the inverse exists, the extended Euclidean algorithm is exactly the tool that finds it.

Example. Find an integer such that leaves remainder when divided by .
First check existence: run the Euclidean algorithm on and ,

so and an inverse exists. Now back-substitute:

So ; i.e. leaves remainder when divided by . Therefore, the inverse of modulo is .

By contrast, has no inverse modulo : , so would force , which is impossible. This is the standard exam trap; always check the gcd before hunting for an inverse.