# The Extended Euclidean Algorithm

The Extended Euclidean Algorithm finds a linear combination of m and n equal to . I'll begin by reviewing the Euclidean algorithm, on which the extended algorithm is based.

The Euclidean algorithm is an efficient way of computing the greatest common divisor of two numbers. It also provides a way of finding numbers a, b, such that

The Euclidean Algorithm. Take . Define

Then recursively define using the Division Algorithm:

The inequality shows that the 's form a decreasing sequence of nonnegative integers. It follows that the algorithm must terminate.

For instance, to cmpute :

We usually write this in tabular form:

The greatest common divisor is the last nonzero remainder: .

According to an earlier result, the greatest common divisor 29 must be a linear combination . Here's a tedious way to find integers a and b which work. Simply work backwards through the equations above, treating the 's as if they were variables.

Substituting for 87 in the first equation,

But

Substituting for 116, I find that

I've written the greatest common divisor 29 as a linear combination of the original numbers 1914 and 899.

While you can use this back-substitution approach to write the greatest common divisor as a linear combination of the original numbers, it's rather tedious. Here's a better way. I'll write it more formally, since the steps are a little complicated.

I proved the next result earlier, but the proof below will actually give an algorithm which constructs a linear combination. It is called a backward recurrence, and is due to S. P. Glasby [1]. It will look a little complicated, but you'll see that it's really easy to use in practice.

Theorem. ( Extended Euclidean Algorithm) is a linear combination of a and b: For some integers s and t,

Warning: s and t are not unique.

Proof. is only defined if at least one of a, b is nonzero. If , and . This proves the result if one of the numbers is 0, so I may as well assume both are nonzero. Moreover, since , I can assume both numbers are positive.

Suppose . Apply the Euclidean Algorithm to and , and suppose that is the last nonzero remainder:

I'm going to define a sequence of numbers , , ... , . They will be constructed recursively, starting with , and working downward to . (This is why this is called a backward recurrence.)

Define and . Then define

Now I claim that

I will prove this by downward induction, starting with and working downward to .

For , I have

The result holds for .

Next, suppose . Suppose the result holds for , i.e.

I want to prove the result for k. Substitute in the preceding equation and simplify:

This proves the result for k, so the result holds for , by downward induction.

In particular, for , the result says

Since , I've expressed as a linear combination of and .

There are many algorithms (like the one in the proof) which produce a linear combination, but I'll call this one the Extended Euclidean Algorithm.

Example. Compute and express it as a linear combination of 187 and 102.

I'll arrange this computation in the form of a table; it's just an extension of the table I used for the Euclidean algorithm.

Here's how you start:

(You can save a step by putting the larger number first.)

The a and q columns are filled in using the Euclidean algorithm, i.e. by successive division: Divide the next-to-the-last a by the last a. The quotient goes into the q-column, and the remainder goes into the a-column.

When the division comes out evenly, you stop. In this case, 85 divided by 17 is 5, with remainder 0.

The last entry in the a-column is the greatest common divisor. Thus, .

The y-column is filled in from bottom to top. Always start with 0 for the last y and 1 for the next-to-the-last y.

Then, working from bottom to top, fill in the y's using the rule

It's probably easier to show than it is to explain:

To get the linear combination, form the products diagonally and subtract one from the other:

Thus,

How do you know the order for the subtraction? The proof gives a formula, but the easiest thing is to pick one of the two ways, then fix it if it isn't right. If you subtract "the wrong way", you'll get a negative number. For example,

Since I know the greatest common divisor should be 17 --- it's the last number in the a-column --- I just multiply this equation by -1:

This way, you don't need to memorize the exact formula.

Example. Compute and express it as a linear combination of 246 and 194.

Thus,

I think this algorithm is the best for hand computation. For implementation on a computer, it has a drawback: You need to store all the Euclidean algorithm quotients, because you need to work your way backward up the table. There is another version of this algorithm which only requires that you save a couple of table lines at a time; it is not as good for hand computation, since you need two helper variables x and y and two multiplications at each step.

1. S. P. Glasby, Extended Euclid's algorithm via backward recurrence relations, Mathematics Magazine, 72(3)(1999), 228--230.

Contact information