# The Extended Euclidean Algorithm

• The Extended Euclidean Algorithm finds integers a and b such that .
• The backward recurrence is an implementation of the Extended Euclidean Algorithm. This implementation is well suited for hand computation.

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.

Example. Compute .

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 how 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.

Terminology. If a and b are things, a linear combination of a and b is something of the form , where s and t are numbers. (The kind of "number" depends on the context.)

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. 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. I'll call this algorithm the Extended Euclidean Algorithm.

Example. In this example, I'll show how you can use the algorithm in the proof to obtain a linear combination. I'll arrange the computations in the form of a table; the table is simply 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 and remainders, 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 at each step.

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

Contact information

Copyright 2008 by Bruce Ikenaga