# Ken Ward's Mathematics Pages

## Number Theory Euclid's Extended Algorithm

Number Theory Contents
Like most theorems in mathematics, Euclid's Extended Algorithm is important, not only for its conclusion, but also for what we can learn about computation from the way it works, and the short-cuts it reveals. Like Euclid's Algorithm, it is very important in Number Theory and other subjects.

The theorem tells us that if a and b are relatively prime, then we can find numbers, x and y, such that:
ax+by=1

As a consequence of this, if a and b are not relatively prime and have a greatest common divisor d, then we can find numbers, x and y such that:
ax+by=d

## Example 1

Suppose we have two numbers, 57 and 46, we seek two numbers x, and y such that:
57x+46y=d (where d is the greatest common divisor of 57 and 46).
First we seek the gcd, using Euclid's Algorithm.

k rk rk+1 qk rk+2 Table 1: gcd(57,46) 57= 46· 1+ 11 46= 11· 4+ 2 11= 2· 5+ 1 We now have the gcd, which is 1, because 57 and 46 are relatively prime. We now have our d=1
Next, we figure out the x and y values.

Considering the equation when k=3, and we already have a "1". We can arrange the equation:
11=2·5+1, so:
1=112·5                 [1.1]

Progressively we can substitute the remainders, beginning with 2. When k=2, we have:
46=11·4+2, so:
2=4611·4. Substituting this value for 2 in 1.1:
1=11−5·(4611·4)
1=11·21−5·46         [1.2]

When k=1, we have:
57=46·1+11, which gives:
11=57−46·1, and substituting for 11 in Equation 1.2:
1=(57−46·1)·21−5·46
1=57·21−26·46, which is our equation          [1.3]

Presenting the results in a table, we have:
k rk rk+1 xk yk Table 2: Solve 57x+46y=1 11 2 1 -5 5 46 11 -5 21 4 57 46 21 26 1

We have therefore found that x=21 and y=26, so:
57·21-26·46=1

## Proof

This is really a formalisation of our example. We will prove that if gcd(a,b)=d, then there exist numbers x and y such that:
ax+by=d. Assuming both a, b ≠0, and a>b.

Our argument is that we can express the gcd as a linear combination of the kind:
rn-2-qn-2·rn-1=rn
where rn=d, n is the number of steps required in Euclid's Algorithm to find the gcd. By Euclid's Algorithm, we know that n is finite and the algorithm always gives us the gcd. So working backwards from this linear combination, substituting for the r's until we have r1=a and r2=b, and we have the sought for linear combination.

First we will use Euclid's Algorithm to find gcd.

a=b·q1+r1
b=r1·q2+r2
....
rn-k+1=qn-k+1·rn-k+2+rn-k+3
rn-k=qn-k·rn-k+1+rn-k+2
...
rn-5=qn-5·rn-4+rn-3     [n-5]
rn-4=qn-4·rn-3+rn-2     [n-4]
rn-3=qn-3·rn-2+rn-1         [n-3]
rn-2=qn-2·rn-1+rn       [n-2]

Assuming that rn is equal to d, so we can express [n-2] as:
d=rn-2 − rn-1·qn-2   [2.1]

As in the arithmetic example, we can substitute rn-1 from Equation n-1 into 2.1:
d=rn-2 − (rn-3−qn-3·rn-2)·qn-2   which gives:

d=rn-2·(1+qn-3·qn-2) − rn-3·qn-2 which is an expression of the r's in as a linear combination, equal to d. We will write this equation as follows to maintain consistency:
d= −rn-3·qn-2+rn-2·(1+qn-3·qn-2)  [2.2],
which shows the r's in the order rn-k, rn-k+1, of a, b)

Naturally, we can continue... We can substitute in Equation 2.2, the value of rn-2 from Equation n-4:
d= −rn-3·qn-2+(rn-4−qn-4·rn-3)·(1+qn-3·qn-2), and by collecting like terms, showing the linear combination of the r's:
d= −rn-3·(qn-2+qn-4·(1+qn-3·qn-2))+rn-4·(1+qn-3·qn-2), and bringing the r's into the order a, b:
d= rn-4·(1+qn-3·qn-2)−rn-3·(qn-2+qn-4·(1+qn-3·qn-2))   [2.3]

Substituting the value for rn-3 from [n-5] in Equation 2.3, we get:
d= rn-4·(1+qn-3·qn-2)−(rn-5−qn-5·rn-4)·(qn-2+qn-4·(1+qn-3·qn-2)), and again collecting the r's together:
d= rn-4·(1+qn-3·qn-2+qn-5·[qn-2+qn-4·(1+qn-3·qn-2)])−rn-5·(qn-2+qn-4·(1+qn-3·qn-2)), and we need to begin with rn-5 because n-5<n-4, and this preserves our order:
d= −rn-5·(qn-2+qn-4·(1+qn-3·qn-2))+rn-4·(1+qn-3·qn-2+qn-5·[qn-2+qn-4·(1+qn-3·qn-2)])

Eventually, we will obtain an expression involving a linear combination of a and b. "Eventually", because we know that we will reach this point, because Euclid's Algorithm begins with a and b and reaches at zero in a finite number of steps, which we have proved under Euclid's Algorithm.
Therefore, we can continue as far as r1 and r2, which are a and b, expressed as a linear combination of the form:
ax+by=d

## Arithmetic Example

We first use Euclid's Algorithm to compute gcd(57,46)
k rk rk+1 qk rk+2 Table 3: gcd(57,46) 57= 46· 1+ 11 46= 11· 4+ 2 11= 2· 5+ 1 We now have the gcd, which is 1, because 57 and 46 are relatively prime. We now have our d=1 4 2= 1· 2+ 0 5 1= 0· 0+ 1

We can use the above to compute x and y such that:
57x+46y=1
From the above table 3, we can fill in the first 3 rows and the sixth row (qk) of Table 4 (There is a great deal of redundancy in this information, and normally we wouldn't write these columns. They are written only to enable the calculation of rk·xk+rk+1·yk which is 1 in each case).
We start at k=5, in the above table and fill in Table 4 below, as we did before.

k rk rk+1 xk yk Table 4: Solve 57x+46y=1 1 0 1 0 - 2 1 0 1 2 11 2 1 -5 5 46 11 -5 21 4 57 46 21 -26 1

The significant parts of the table for this section are the last three columns, xk, yk, and qk.

Clearly, we see that xk=yk-1. Also, all the information in xk is contained in yk. That is, except for the first and last numbers.

Also, the table begins with x, and y values 1, 0 and the next row is 0, 1.

What isn't obvious, but what we will later prove is:
xk=xk-2-qk-1·xk-1, and
yk=yk-2-yk-1·qk

That is, in computing the x's and y's, we need only to go back two terms. And we can compute the extended algorithm using the q's from the computation of the gcd

## Determining the Coefficients in the Extended Algorithm prior to term n-2

In the previous example, we noted that the first coefficients in the extended algorithm were 1, 0 and on the next row, 0, 1. The question is, is this fortuitous or is it true in general.
The following are some continued terms of Euclid's algorithm, with rn=d=gcd, where this is the (n-2) term from the beginning.

We note that rn=d, the gcd(r1,r2).
In term [n-1], rn+1=0, because it is the remainder after the gcd. (Because d divides all remainders, then the remainder when divided into rn-1 is zero).

Because rn+1 is zero, then rn+2=rn=d.

We begin the extended algorithm:
[3.1]
Because the coefficient of rn is 1, and qn=0 (because there are zero rn+1), we have our starting values 1, 0.

Substituting rn+1 from [n-1]

which tells us that:

Also that:

Rearranging:
[3.2]
which gives us our second row, with coefficients 0, 1

## Proving the Rules

The following table illustrates the Extended Euclidean algorithm using algebra:

k xk yk Table 5: Algebraic Illustration n 1 0 n-1 0 1 1 -qn-2 -qn-2 1+qn-3·qn-2 1+qn-3·qn-2 -(qn-2+qn-4·(1+qn-3·qn-2) ) -(qn-2+qn-4·(1+qn-3·qn-2) ) 1+qn-3·qn-2+qn-5·[qn-2+qn-4·(1+qn-3·qn-2)]
The above table is present only to demonstrate the algebra, and does not take part in the proof.

We prove the rules in the following way:
Imagine that the following is the kth term of Euclid's Extended Algorithm:
[4.1]
where d is gcd(r1,r2) and xk and yk are kth values in our calculation of x1 and y1;  n-2 ≤ k ≤ 1; and d is the gcd, rn. Naturally, all the numbers are integers.

The next stage in the algorithm is to substitute:
(from Euclid's Algorithm) [4.2]
in 4.1:
[4.3]
which is the k-1 th term of the algorithm.

Therefore:
[4.4]
which tells us that an x value is equal to the previous y value, as we saw in the table.

Also,
[4.5]
By using 4.4, we can express the y's in terms of other y's (and the q!)

or
[4.6]
That is, the new y value equals the y value 2 rows up less the current q times the previous y.

Similarly, we can express the x's in terms of the x's (and the q):
[4.7]
That is the new x value equals the x value two rows down less the previous x value times the previous q value.

### Arithmetic Demonstration of the Rules

The table below is a previous table, computed using the rules this time, that is:

k xk yk qk Table 6: Solve 57x+46y=1 (Using the rules) 1 0 - 0 1 2 1 -5 5 x3=1−5·0=1y3=0−5·1=-5 -5 21 4 x2=1−5·1=-5y2=1−4·(-5)=21 21 -26 1 x1=1−(-5)·4=21y1=(−5)·1−21=−26

Therefore a solution to
57x+46y=1, is:
57(21)+46(-26)=1

## The solution is not unique

One solution to 57x+46y=1 is:
57(21)+46(-26)=1

Other solutions are:
57(67)+46(-83)=1
57(113)+46(-140)=1

There are, in fact, an infinite number of such solutions. So the solution given above is not unique.

Ken Ward's Mathematics Pages

# Faster Arithmetic - by Ken Ward

Ken's book is packed with examples and explanations that enable you to discover more than 150 techniques to speed up your arithmetic and increase your understanding of numbers. Paperback and Kindle: