Docsity

The Extended Euclidean Algorithm: Finding Linear Combinations of Two Numbers, Exercises of Grammar and Composition

Toggle sidebar
bg1
1-21-2019
The Extended Euclidean Algorithm
The Extended Euclidean Algorithm finds a linear combination of mand nequal to (m, n). 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
(x, y) = ax +by.
The Euclidean Algorithm. Take m, n > 0. Define
r0=m, r1=n.
Then recursively define rkusing the Division Algorithm:
rk−2=qrk−1+rk,where 0 ≤rk< rk−1.
The inequality 0 ≤rk< rk−1shows that the rk’s form a decreasing sequence of nonnegative integers.
It follows that the algorithm must terminate.
For instance, to cmpute (1914,899):
1914 = 2 ·899 + 116
899 = 7 ·116 + 87
116 = 1 ·87 + 29
87 = 3 ·29 + 0
We usually write this in tabular form:
1914 -
899 2
116 7
87 1
29 3
The greatest common divisor is the last nonzero remainder: (1914,899) = 29.
According to an earlier result, the greatest common divisor 29 must be a linear combination a·1914 +
b·899. Here’s a tedious way to find integers aand bwhich work. Simply work backwards through the
equations above, treating the rk’s as if they were variables.
29 = 116 + (−1) ·87 and 87 = 899 + (−7) ·116.
Substituting for 87 in the first equation,
29 = 116 + (−1) ·[899 + (−7) ·116] = (−1) ·899 + 8 ·116.
But
116 = 1914 + (−2) ·899.
Substituting for 116, I find that
29 = (−1) ·899 + 8 ·[1914 + (−2) ·899] = 8 ·1914 + (−17) ·899.
1
pf3
pf4
pf5

Partial preview of the text

Download The Extended Euclidean Algorithm: Finding Linear Combinations of Two Numbers and more Exercises Grammar and Composition in PDF only on Docsity!

The Extended Euclidean Algorithm

The Extended Euclidean Algorithm finds a linear combination of m and n equal to (m, n). 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

(x, y) = ax + by.

The Euclidean Algorithm. Take m, n > 0. Define

r 0 = m, r 1 = n.

Then recursively define rk using the Division Algorithm:

rk− 2 = qrk− 1 + rk, where 0 ≤ rk < rk− 1.

The inequality 0 ≤ rk < rk− 1 shows that the rk’s form a decreasing sequence of nonnegative integers. It follows that the algorithm must terminate. For instance, to cmpute (1914, 899):

1914 = 2 · 899 + 116 899 = 7 · 116 + 87 116 = 1 · 87 + 29 87 = 3 · 29 + 0

We usually write this in tabular form:

The greatest common divisor is the last nonzero remainder: (1914, 899) = 29. According to an earlier result, the greatest common divisor 29 must be a linear combination a · 1914 + b · 899. Here’s a tedious way to find integers a and b which work. Simply work backwards through the equations above, treating the rk’s as if they were variables.

29 = 116 + (−1) · 87 and 87 = 899 + (−7) · 116.

Substituting for 87 in the first equation,

29 = 116 + (−1) · [899 + (−7) · 116] = (−1) · 899 + 8 · 116.

But 116 = 1914 + (−2) · 899.

Substituting for 116, I find that

29 = (−1) · 899 + 8 · [1914 + (−2) · 899] = 8 · 1914 + (−17) · 899.

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

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) (a, b) is a linear combination of a and b: For some integers s and t, (a, b) = sa + tb.

Warning: s and t are not unique.

Proof. (a, b) is only defined if at least one of a, b is nonzero. If a 6 = 0, (a, 0) = a and a = 1 · a + 0 · 0. This proves the result if one of the numbers is 0, so I may as well assume both are nonzero. Moreover, since (a, b) = (|a|, |b|), I can assume both numbers are positive. Suppose a ≥ b. Apply the Euclidean Algorithm to a 0 = a and a 1 = b, and suppose that an is the last nonzero remainder: a 0 = a 1 q 1 + a 2 , where 0 ≤ a 2 < a 1 a 1 = a 2 q 2 + a 3 , where 0 ≤ a 3 < a 2

ak = ak+1qk+1 + ak+2, where 0 ≤ ak+2 < ak+ .. . an− 1 = anqn + 0. I’m going to define a sequence of numbers yn, yn− 1 ,... y 1 , y 0. They will be constructed recursively, starting with yn, yn− 1 and working downward to y 0. (This is why this is called a backward recurrence.) Define yn = 0 and yn− 1 = 1. Then define

yk− 1 = qkyk + yk+1 for k = n − 2 ,... , 2 , 1.

Now I claim that

(−1)n+k+1ak− 1 yk + (−1)n+kakyk− 1 = an for 1 ≤ k ≤ n.

I will prove this by downward induction, starting with k = n and working downward to k = 1. For k = n, I have

(−1)^2 n+1an− 1 yn + (−1)^2 nanyn− 1 = −an− 1 yn + anyn− 1 = −an− 1 · 0 + an · 1 = an.

The result holds for k = n. Next, suppose 1 < k < n. Suppose the result holds for k + 1, i.e.

(−1)n+k+2akyk+1 + (−1)n+k+1ak+1yk = an.

I want to prove the result for k. Substitute yk+1 = yk− 1 − qkyk in the preceding equation and simplify:

an = (−1)n+k+2akyk+1 + (−1)n+k+1ak+1yk = (−1)n+k+2ak(yk− 1 − qkyk) + (−1)n+k+1ak+1yk =

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

a q y

187 -

102 1

85 1

17 5

The last entry in the a-column is the greatest common divisor. Thus, (187, 102) = 17. 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.

a q y

187 -

102 1

85 1 1

17 5 0

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

(next y) = (last q) · (last y) + (next-to-last y). It’s probably easier to show than it is to explain:

a q y

187 -

102 1 1

85 1 1

17 5 0

a q y

187 - 2

102 1 1

85 1 1

17 5 0

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

a (^) q y

187 - 2

102 1 1

85 1 1

17 5 0

Thus, 17 = (187, 102) = (2)(102) − (1)(187).

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, (1)(187) − (2)(102) = − 17.

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: (−1)(187) + (2)(102) = 17. This way, you don’t need to memorize the exact formula.

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

Thus, 2 = (246, 194) = 52 · 194 − 41 · 246.

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.

©^ c2019 by Bruce Ikenaga 5