Books Number Theory

The Euclidean Algorithm

The following is Euclidean algorithm. Given two positive integers 𝑎𝑎 and 𝑏𝑏, find gcd⁡(𝑎,𝑏)\gcd (𝑎,𝑏).

Algorithm 3.1

Euclidean algorithm

Input
𝑎,𝑏∈Z+𝑎,𝑏\in {\mathbb{Z}}^{+}
Output
gcd⁡(𝑎,𝑏)\gcd (𝑎,𝑏)
1 while 𝑏>0𝑏>0 do
2 𝑟←𝑎mod⁡𝑏𝑟←𝑎\operatorname{mod}𝑏 // 0≤𝑟<𝑏0\leq 𝑟<𝑏
3 𝑎←𝑏𝑎←𝑏
4 𝑏←𝑟𝑏←𝑟
5 return 𝑎𝑎
Proof

The variables 𝑎𝑎 and 𝑏𝑏 change. Write 𝑎0{𝑎}_{0} and 𝑏0{𝑏}_{0} for the two inputs. At the start of each iteration, just before the guard on line 1 is tested, let 𝑃𝑃 be the assertion that 𝑎>0𝑎>0, 𝑏≥0𝑏\geq 0, and gcd⁡(𝑎,𝑏)=gcd⁡(𝑎0,𝑏0)\gcd (𝑎,𝑏)=\gcd ({𝑎}_{0},{𝑏}_{0}).

Initialization. Before the first iteration, 𝑎=𝑎0𝑎={𝑎}_{0} and 𝑏=𝑏0𝑏={𝑏}_{0}, both positive. Thus 𝑃𝑃 holds.

Maintenance. Assume 𝑃𝑃 holds before an iteration and the guard holds, so 𝑏>0𝑏>0. By Theorem 1.5 there is an integer 𝑞𝑞 with 𝑎=𝑞𝑏+𝑟𝑎=𝑞𝑏+𝑟 and 0≤𝑟<𝑏0\leq 𝑟<𝑏. Line 2 sets 𝑟𝑟 to that remainder. By Theorem 3.2, gcd⁡(𝑏,𝑟)=gcd⁡(𝑎,𝑏)\gcd (𝑏,𝑟)=\gcd (𝑎,𝑏). Lines 3 and 4 then replace (𝑎,𝑏)(𝑎,𝑏) by (𝑏,𝑟)(𝑏,𝑟), in that order, so 𝑎𝑎 receives the old value of 𝑏𝑏 before 𝑏𝑏 receives 𝑟𝑟. The new 𝑎𝑎 is positive and the new 𝑏𝑏 is nonnegative, and 𝑃𝑃 holds at the next test of the guard.

Termination. Whenever the guard holds, line 2 produces 0≤𝑟<𝑏0\leq 𝑟<𝑏, and line 4 sets 𝑏𝑏 equal to 𝑟𝑟. If the guard holds again, this new 𝑏𝑏 is a strictly smaller positive integer. The successive values of 𝑏𝑏 at the guard are therefore a strictly decreasing sequence of positive integers. If the sequence were infinite, its set of values would be a nonempty subset of Z+{\mathbb{Z}}^{+} with no least element, contradicting the well-ordering principle. So the guard fails after finitely many iterations.

Exit. The guard has failed and 𝑃𝑃 still holds, so 𝑏≥0𝑏\geq 0 forces 𝑏=0𝑏=0, and 𝑎>0𝑎>0. Thus 𝑎𝑎 divides both 𝑎𝑎 and 00, and no larger positive integer divides 𝑎𝑎, hence gcd⁡(𝑎,0)=𝑎\gcd (𝑎,0)=𝑎. By 𝑃𝑃, line 5 returns gcd⁡(𝑎0,𝑏0)\gcd ({𝑎}_{0},{𝑏}_{0}).

The Euclidean algorithm can take quite a few steps. Given any positive integer 𝑛𝑛, there are integers 𝑎𝑎 and 𝑏𝑏 for which it runs exactly 𝑛𝑛 steps to find gcd⁡(𝑎,𝑏)\gcd (𝑎,𝑏). And 𝑛𝑛 can be large, so the algorithm can feel slow.

Consecutive Fibonacci numbers are such examples.

Example 3.2

Apply the Euclidean algorithm to 𝑓10=55{𝑓}_{10}=55 and 𝑓9=34{𝑓}_{9}=34:

55=34⋅1+21,34=21⋅1+13,21=13⋅1+8,13=8⋅1+5,8=5⋅1+3,5=3⋅1+2,3=2⋅1+1,2=1⋅2+0.\begin{aligned}55 & =34\cdot 1+21, \\ 34 & =21\cdot 1+13, \\ 21 & =13\cdot 1+8, \\ 13 & =8\cdot 1+5, \\ 8 & =5\cdot 1+3, \\ 5 & =3\cdot 1+2, \\ 3 & =2\cdot 1+1, \\ 2 & =1\cdot 2+0.\end{aligned}

That is 88 steps, and the value returned is 11, so gcd⁡(𝑓9,𝑓10)=1\gcd ({𝑓}_{9},{𝑓}_{10})=1.

We have the general result:

Theorem 3.7
Consider the Fibonacci sequence {𝑓𝑛}\{{𝑓}_{𝑛}\} with 𝑓0=0{𝑓}_{0}=0 and 𝑓1=1{𝑓}_{1}=1. For every integer 𝑛≥2𝑛\geq 2, the Euclidean algorithm takes exactly 𝑛𝑛 steps to show that gcd⁡(𝑓𝑛+1,𝑓𝑛+2)=1\gcd ({𝑓}_{𝑛+1},{𝑓}_{𝑛+2})=1.

Note that this theorem does not only illustrate our point, that for every such 𝑛𝑛 we can find an example on which the Euclidean algorithm runs for exactly 𝑛𝑛 steps. Along the way it also proves that the consecutive Fibonacci numbers 𝑓𝑛+1{𝑓}_{𝑛+1} and 𝑓𝑛+2{𝑓}_{𝑛+2} are coprime: their greatest common divisor is 11.

From Example 3.2 we already have the idea of the proof. At every step except the last, the quotient is 11. At the last step the quotient is 22, and the remainder is 00.

Proof

By the recurrence of the Fibonacci sequence, for every integer 𝑘≥2𝑘\geq 2,

𝑓𝑘+2=𝑓𝑘+1+𝑓𝑘.{𝑓}_{𝑘+2}={𝑓}_{𝑘+1}+{𝑓}_{𝑘}.

Note that 0<𝑓𝑘<𝑓𝑘+10<{𝑓}_{𝑘}<{𝑓}_{𝑘+1}. This is the crucial point: it shows that 𝑓𝑘+2{𝑓}_{𝑘+2} divided by 𝑓𝑘+1{𝑓}_{𝑘+1} has quotient 11 and remainder 𝑓𝑘{𝑓}_{𝑘}. By Theorem 1.5, that quotient and remainder are unique.

So the Euclidean algorithm gives

𝑓𝑛+2=𝑓𝑛+1⋅1+𝑓𝑛,𝑓𝑛+1=𝑓𝑛⋅1+𝑓𝑛−1,⋮𝑓4=𝑓3⋅1+𝑓2,𝑓3=𝑓2⋅2+0.\begin{aligned}{𝑓}_{𝑛+2} & ={𝑓}_{𝑛+1}\cdot 1+{𝑓}_{𝑛}, \\ {𝑓}_{𝑛+1} & ={𝑓}_{𝑛}\cdot 1+{𝑓}_{𝑛-1}, \\ & ⋮ \\ {𝑓}_{4} & ={𝑓}_{3}\cdot 1+{𝑓}_{2}, \\ {𝑓}_{3} & ={𝑓}_{2}\cdot 2+0.\end{aligned}

The algorithm returns 𝑓2=1{𝑓}_{2}=1, which means gcd⁡(𝑓𝑛+1,𝑓𝑛+2)=1\gcd ({𝑓}_{𝑛+1},{𝑓}_{𝑛+2})=1. And it takes 𝑛𝑛 steps.

The Complexity of the Euclidean Algorithm

We can prove a theorem of Gabriel Lamé: an upper bound on the number of steps the Euclidean algorithm takes.

Theorem 3.8Lamé's theorem

The number of divisions needed to find the greatest common divisor of two positive integers by the Euclidean algorithm does not exceed five times the number of decimal digits in the smaller of the two integers.

Mathematically, suppose 𝑎,𝑏∈Z+𝑎,𝑏\in {\mathbb{Z}}^{+} with 𝑎>𝑏𝑎>𝑏, and suppose it takes 𝑛𝑛 steps to find gcd⁡(𝑎,𝑏)\gcd (𝑎,𝑏). Then 𝑛≤5𝑚𝑛\leq 5𝑚, where 𝑚𝑚 is the number of decimal digits of 𝑏𝑏.

The result is a nice one. We finally have an upper bound on the worst case of the Euclidean algorithm. At the same time the conclusion is baffling. What do decimal digits have to do with it, and where does the coefficient 55 come from?

And you’d be surprised that the proof uses the Fibonacci sequence, and a property of it that we introduced, (1.8).

Proof

Suppose it takes 𝑛𝑛 steps. Let 𝑟0=𝑎{𝑟}_{0}=𝑎 and 𝑟1=𝑏{𝑟}_{1}=𝑏. The Euclidean algorithm gives

𝑟0=𝑟1⋅𝑞1+𝑟2,𝑟1=𝑟2⋅𝑞2+𝑟3,⋮𝑟𝑛−2=𝑟𝑛−1⋅𝑞𝑛−1+𝑟𝑛,𝑟𝑛−1=𝑟𝑛⋅𝑞𝑛+0.\begin{aligned}{𝑟}_{0} & ={𝑟}_{1}\cdot {𝑞}_{1}+{𝑟}_{2}, \\ {𝑟}_{1} & ={𝑟}_{2}\cdot {𝑞}_{2}+{𝑟}_{3}, \\ & ⋮ \\ {𝑟}_{𝑛-2} & ={𝑟}_{𝑛-1}\cdot {𝑞}_{𝑛-1}+{𝑟}_{𝑛}, \\ {𝑟}_{𝑛-1} & ={𝑟}_{𝑛}\cdot {𝑞}_{𝑛}+0.\end{aligned}

Since 𝑞𝑘≥1{𝑞}_{𝑘}\geq 1,

𝑟𝑘−1≥𝑟𝑘+𝑟𝑘+1,∀𝑘=1,…,𝑛−1.{𝑟}_{𝑘-1}\geq {𝑟}_{𝑘}+{𝑟}_{𝑘+1},\quad \forall 𝑘=1,\ldots ,𝑛-1.
(3.6)
Note

From (3.6) we should at once think of the recurrence for the Fibonacci sequence:

𝑓𝑛=𝑓𝑛−1+𝑓𝑛−2.{𝑓}_{𝑛}={𝑓}_{𝑛-1}+{𝑓}_{𝑛-2}.

This is where the Fibonacci sequence first shows itself.

Now 𝑟𝑛≥1=𝑓2{𝑟}_{𝑛}\geq 1={𝑓}_{2}, and 𝑟𝑛−1>𝑟𝑛{𝑟}_{𝑛-1}>{𝑟}_{𝑛}, so 𝑟𝑛−1≥2=𝑓3{𝑟}_{𝑛-1}\geq 2={𝑓}_{3}. It then follows that

𝑟𝑛−2≥𝑟𝑛−1+𝑟𝑛≥𝑓3+𝑓2=𝑓4,𝑟𝑛−3≥𝑟𝑛−2+𝑟𝑛−1≥𝑓4+𝑓3=𝑓5,⋮𝑟1≥𝑟2+𝑟3≥𝑓𝑛+𝑓𝑛−1=𝑓𝑛+1.\begin{aligned}{𝑟}_{𝑛-2} & \geq {𝑟}_{𝑛-1}+{𝑟}_{𝑛}\geq {𝑓}_{3}+{𝑓}_{2}={𝑓}_{4}, \\ {𝑟}_{𝑛-3} & \geq {𝑟}_{𝑛-2}+{𝑟}_{𝑛-1}\geq {𝑓}_{4}+{𝑓}_{3}={𝑓}_{5}, \\ & ⋮ \\ {𝑟}_{1} & \geq {𝑟}_{2}+{𝑟}_{3}\geq {𝑓}_{𝑛}+{𝑓}_{𝑛-1}={𝑓}_{𝑛+1}.\end{aligned}

In summary, we have an estimate for 𝑏𝑏 by a Fibonacci number indexed from the number of steps taken:

𝑏=𝑟1≥𝑓𝑛+1.𝑏={𝑟}_{1}\geq {𝑓}_{𝑛+1}.

Using (1.8), we have

𝑏≥𝑓𝑛+1>φ𝑛−1.𝑏\geq {𝑓}_{𝑛+1}>{\varphi }^{𝑛-1}.

At this point we already have the inequality that relates 𝑛𝑛. Taking the logarithm base 1010, we move 𝑛−1𝑛-1 from the exponent down into a product:

(𝑛−1)log⁡10φ<log⁡10𝑏.(𝑛-1){\log }_{10}\varphi <{\log }_{10}𝑏.

Suppose 𝑏𝑏 has 𝑚∈Z+𝑚\in {\mathbb{Z}}^{+} decimal digits. Then 𝑏<10𝑚𝑏<{10}^{𝑚}, and hence log⁡10𝑏<𝑚{\log }_{10}𝑏<𝑚. Therefore

𝑛<𝑚log⁡10φ+1.𝑛<\frac{𝑚}{{\log }_{10}\varphi }+1.

We can show that

log⁡10φ>15,{\log }_{10}\varphi >\frac{1}{5},

and then obtain

𝑛<𝑚log⁡10φ+1<𝑚15+1=5𝑚+1.𝑛<\frac{𝑚}{{\log }_{10}\varphi }+1<\frac{𝑚}{\frac{1}{5}}+1=5𝑚+1.

That is where the 55 comes from. Since 𝑛𝑛 is an integer, 𝑛≤5𝑚𝑛\leq 5𝑚.

Note

We could, of course, use a calculator to check log⁡10φ>15{\log }_{10}\varphi >\frac{1}{5} and be done. But I find the estimate itself interesting.

The idea is to show φ5>10{\varphi }^{5}>10. Recall that we have

φ2=φ+1.{\varphi }^{2}=\varphi +1.

So

φ5=(φ2)2⋅φ=(φ+1)2⋅φ=(φ2+2φ+1)⋅φ=(3φ+2)⋅φ=5φ+3.\begin{aligned}{\varphi }^{5} & ={({\varphi }^{2})}^{2}\cdot \varphi \\ & ={(\varphi +1)}^{2}\cdot \varphi \\ & =({\varphi }^{2}+2\varphi +1)\cdot \varphi \\ & =(3\varphi +2)\cdot \varphi \\ & =5\varphi +3.\end{aligned}

And

φ=1+52>1+42=32,\varphi =\frac{1+\sqrt{5}}{2}>\frac{1+\sqrt{4}}{2}=\frac{3}{2},

so

φ5=5φ+3>212=10.5>10.{\varphi }^{5}=5\varphi +3>\frac{21}{2}=10.5>10.

Thus we obtain the estimate log⁡10φ>15{\log }_{10}\varphi >\frac{1}{5}.

Expressing Greatest Common Divisors as Linear Combinations

Question: how do we express the gcd, 𝑑𝑑, as a linear combination of 𝑎𝑎 and 𝑏𝑏?

We can trace the Euclidean algorithm backward, as it is described in the original textbook. The divisions are

𝑟0=𝑟1⋅𝑞1+𝑟2,𝑟1=𝑟2⋅𝑞2+𝑟3,⋮𝑟𝑛−3=𝑟𝑛−2⋅𝑞𝑛−2+𝑟𝑛−1,𝑟𝑛−2=𝑟𝑛−1⋅𝑞𝑛−1+𝑟𝑛,𝑟𝑛−1=𝑟𝑛⋅𝑞𝑛+0,\begin{aligned}{𝑟}_{0} & ={𝑟}_{1}\cdot {𝑞}_{1}+{𝑟}_{2}, \\ {𝑟}_{1} & ={𝑟}_{2}\cdot {𝑞}_{2}+{𝑟}_{3}, \\ & ⋮ \\ {𝑟}_{𝑛-3} & ={𝑟}_{𝑛-2}\cdot {𝑞}_{𝑛-2}+{𝑟}_{𝑛-1}, \\ {𝑟}_{𝑛-2} & ={𝑟}_{𝑛-1}\cdot {𝑞}_{𝑛-1}+{𝑟}_{𝑛}, \\ {𝑟}_{𝑛-1} & ={𝑟}_{𝑛}\cdot {𝑞}_{𝑛}+0,\end{aligned}

where 𝑟𝑛=𝑑=gcd⁡(𝑎,𝑏){𝑟}_{𝑛}=𝑑=\gcd (𝑎,𝑏).

Every step but the last leaves a remainder, and that remainder can be moved to the other side of the equation. The second-to-last step is the one whose remainder is 𝑑𝑑 itself. You see, we have

𝑑=𝑟𝑛=𝑟𝑛−2−𝑟𝑛−1⋅𝑞𝑛−1,𝑑={𝑟}_{𝑛}={𝑟}_{𝑛-2}-{𝑟}_{𝑛-1}\cdot {𝑞}_{𝑛-1},
(3.7)

and so 𝑑𝑑 is already a linear combination of 𝑟𝑛−2{𝑟}_{𝑛-2} and 𝑟𝑛−1{𝑟}_{𝑛-1}.

The step before that does the same for 𝑟𝑛−1{𝑟}_{𝑛-1}:

𝑟𝑛−1=𝑟𝑛−3−𝑟𝑛−2⋅𝑞𝑛−2.{𝑟}_{𝑛-1}={𝑟}_{𝑛-3}-{𝑟}_{𝑛-2}\cdot {𝑞}_{𝑛-2}.

Thus 𝑟𝑛−1{𝑟}_{𝑛-1} is a linear combination of the two terms that precede it, 𝑟𝑛−3{𝑟}_{𝑛-3} and 𝑟𝑛−2{𝑟}_{𝑛-2}. Substitute this expression into (3.7). What was a combination of 𝑟𝑛−2{𝑟}_{𝑛-2} and 𝑟𝑛−1{𝑟}_{𝑛-1} becomes a combination of 𝑟𝑛−3{𝑟}_{𝑛-3} and 𝑟𝑛−2{𝑟}_{𝑛-2}:

𝑑=−𝑟𝑛−3𝑞𝑛−1+(1+𝑞𝑛−2𝑞𝑛−1)𝑟𝑛−2.𝑑=-{𝑟}_{𝑛-3}{𝑞}_{𝑛-1}+(1+{𝑞}_{𝑛-2}{𝑞}_{𝑛-1}){𝑟}_{𝑛-2}.

And so on. Each time, the latest remainder still standing in the expression is rewritten, by the division that produced it, as a combination of two earlier ones. We keep working backward until the only numbers left are 𝑟0=𝑎{𝑟}_{0}=𝑎 and 𝑟1=𝑏{𝑟}_{1}=𝑏. By then we are expressing 𝑑𝑑 as a linear combination of 𝑎𝑎 and 𝑏𝑏.

The problem seems solved. But if you think about writing a program to find the coefficients 𝑠𝑠 and 𝑡𝑡 such that

gcd⁡(𝑎,𝑏)=𝑠𝑎+𝑡𝑏,\gcd (𝑎,𝑏)=𝑠𝑎+𝑡𝑏,

then you will realize that this argument does not hand you 𝑠𝑠 and 𝑡𝑡 on the way down. The quotients appear only as the divisions are performed, and the backward steps need them in reverse: 𝑞1,𝑞2,…,𝑞𝑛−1{𝑞}_{1},{𝑞}_{2},\ldots ,{𝑞}_{𝑛-1}. One pass runs the Euclidean algorithm and stores those quotients. Another pass starts from 𝑑𝑑 and walks back to 𝑎𝑎 and 𝑏𝑏, building the coefficients as it goes. Two passes, and a list that has to sit there until the second one is finished. An uncomfortable feeling of wasting both time and space.

There is another way to look at the same divisions, a forward view.

𝑟0=𝑟1⋅𝑞1+𝑟2⟺𝑟2=𝑟0−𝑟1⋅𝑞1,𝑟1=𝑟2⋅𝑞2+𝑟3⟺𝑟3=𝑟1−𝑟2⋅𝑞2,⋮⋮𝑟𝑛−3=𝑟𝑛−2⋅𝑞𝑛−2+𝑟𝑛−1⟺𝑟𝑛−1=𝑟𝑛−3−𝑟𝑛−2⋅𝑞𝑛−2,𝑟𝑛−2=𝑟𝑛−1⋅𝑞𝑛−1+𝑟𝑛⟺𝑟𝑛=𝑟𝑛−2−𝑟𝑛−1⋅𝑞𝑛−1,𝑟𝑛−1=𝑟𝑛⋅𝑞𝑛+0.\begin{aligned}{𝑟}_{0} & ={𝑟}_{1}\cdot {𝑞}_{1}+{𝑟}_{2} & \quad ⟺\quad & & {𝑟}_{2} & ={𝑟}_{0}-{𝑟}_{1}\cdot {𝑞}_{1}, \\ {𝑟}_{1} & ={𝑟}_{2}\cdot {𝑞}_{2}+{𝑟}_{3} & \quad ⟺\quad & & {𝑟}_{3} & ={𝑟}_{1}-{𝑟}_{2}\cdot {𝑞}_{2}, \\ & ⋮ & & & & ⋮ \\ {𝑟}_{𝑛-3} & ={𝑟}_{𝑛-2}\cdot {𝑞}_{𝑛-2}+{𝑟}_{𝑛-1} & \quad ⟺\quad & & {𝑟}_{𝑛-1} & ={𝑟}_{𝑛-3}-{𝑟}_{𝑛-2}\cdot {𝑞}_{𝑛-2}, \\ {𝑟}_{𝑛-2} & ={𝑟}_{𝑛-1}\cdot {𝑞}_{𝑛-1}+{𝑟}_{𝑛} & \quad ⟺\quad & & {𝑟}_{𝑛} & ={𝑟}_{𝑛-2}-{𝑟}_{𝑛-1}\cdot {𝑞}_{𝑛-1}, \\ {𝑟}_{𝑛-1} & ={𝑟}_{𝑛}\cdot {𝑞}_{𝑛}+0. & & & & \end{aligned}
(3.8)

Take a minute and think. Do you get what I mean?

Yes. Think this way. The right-hand column is the same division, solved for the new remainder. At the first step, 𝑟2{𝑟}_{2} is a linear combination of 𝑟0=𝑎{𝑟}_{0}=𝑎 and 𝑟1=𝑏{𝑟}_{1}=𝑏. At the second step, 𝑟3{𝑟}_{3} is a linear combination of 𝑟1{𝑟}_{1} and 𝑟2{𝑟}_{2}. We can already write 𝑟2{𝑟}_{2} in terms of 𝑎𝑎 and 𝑏𝑏, and 𝑟1{𝑟}_{1} is 𝑏𝑏, so replacing 𝑟2{𝑟}_{2} writes 𝑟3{𝑟}_{3} in terms of 𝑎𝑎 and 𝑏𝑏 as well. Each new remainder is a combination of the two that precede it, and those two are already combinations of 𝑎𝑎 and 𝑏𝑏, so the new one is too. Keep going. It is the same replacement every time, and at the end 𝑑=𝑟𝑛𝑑={𝑟}_{𝑛} is a linear combination of 𝑎𝑎 and 𝑏𝑏.

Nothing has to be saved for a later pass. The quotient is used when it appears, to rewrite the new remainder, and then we do not need it again. One pass through the Euclidean algorithm, and no list of 𝑞𝑞’s. This is, basically, the idea of the extended Euclidean algorithm.

Call the coefficients 𝑠𝑗{𝑠}_{𝑗} and 𝑡𝑗{𝑡}_{𝑗}, so that

𝑟𝑗=𝑠𝑗𝑎+𝑡𝑗𝑏.{𝑟}_{𝑗}={𝑠}_{𝑗}𝑎+{𝑡}_{𝑗}𝑏.

From (3.8) we can read off the first two:

𝑟2=𝑟0−𝑟1⋅𝑞1=𝑎−𝑞1𝑏⟹𝑠2=1,𝑡2=−𝑞1,𝑟3=𝑟1−𝑟2⋅𝑞2=−𝑞2𝑎+(1+𝑞1𝑞2)𝑏⟹𝑠3=−𝑞2,𝑡3=1+𝑞1𝑞2.\begin{aligned}{𝑟}_{2} & ={𝑟}_{0}-{𝑟}_{1}\cdot {𝑞}_{1}=𝑎-{𝑞}_{1}𝑏 & ⟹\quad & & {𝑠}_{2}=1,\quad {𝑡}_{2}=-{𝑞}_{1}, \\ {𝑟}_{3} & ={𝑟}_{1}-{𝑟}_{2}\cdot {𝑞}_{2}=-{𝑞}_{2}𝑎+(1+{𝑞}_{1}{𝑞}_{2})𝑏 & ⟹\quad & & {𝑠}_{3}=-{𝑞}_{2},\quad {𝑡}_{3}=1+{𝑞}_{1}{𝑞}_{2}.\end{aligned}

We can fill in the definitions for 𝑗=0𝑗=0 and 𝑗=1𝑗=1. We have

𝑟0=1⋅𝑟0+0⋅𝑟1,𝑟1=0⋅𝑟0+1⋅𝑟1,\begin{aligned}{𝑟}_{0} & =1\cdot {𝑟}_{0}+0\cdot {𝑟}_{1}, \\ {𝑟}_{1} & =0\cdot {𝑟}_{0}+1\cdot {𝑟}_{1},\end{aligned}

so

𝑠0=1,𝑠1=0,𝑡0=0,𝑡1=1.{𝑠}_{0}=1,\quad {𝑠}_{1}=0,\quad {𝑡}_{0}=0,\quad {𝑡}_{1}=1.

With 𝑟𝑗=𝑠𝑗𝑎+𝑡𝑗𝑏{𝑟}_{𝑗}={𝑠}_{𝑗}𝑎+{𝑡}_{𝑗}𝑏 in hand, the recurrence for {𝑠𝑗}\{{𝑠}_{𝑗}\} and {𝑡𝑗}\{{𝑡}_{𝑗}\} is easy to derive. Suppose

𝑟𝑗−2=𝑠𝑗−2𝑎+𝑡𝑗−2𝑏,𝑟𝑗−1=𝑠𝑗−1𝑎+𝑡𝑗−1𝑏.\begin{aligned}{𝑟}_{𝑗-2} & ={𝑠}_{𝑗-2}𝑎+{𝑡}_{𝑗-2}𝑏, \\ {𝑟}_{𝑗-1} & ={𝑠}_{𝑗-1}𝑎+{𝑡}_{𝑗-1}𝑏.\end{aligned}

The next step is to express 𝑟𝑗{𝑟}_{𝑗} in 𝑎𝑎 and 𝑏𝑏. From the Euclidean algorithm,

𝑟𝑗=𝑟𝑗−2−𝑟𝑗−1𝑞𝑗−1.{𝑟}_{𝑗}={𝑟}_{𝑗-2}-{𝑟}_{𝑗-1}{𝑞}_{𝑗-1}.

Substitute 𝑟𝑗−2{𝑟}_{𝑗-2} and 𝑟𝑗−1{𝑟}_{𝑗-1}, and we obtain

𝑟𝑗=(𝑠𝑗−2−𝑞𝑗−1𝑠𝑗−1)𝑎+(𝑡𝑗−2−𝑞𝑗−1𝑡𝑗−1)𝑏.{𝑟}_{𝑗}=({𝑠}_{𝑗-2}-{𝑞}_{𝑗-1}{𝑠}_{𝑗-1})𝑎+({𝑡}_{𝑗-2}-{𝑞}_{𝑗-1}{𝑡}_{𝑗-1})𝑏.

Matching coefficients gives

𝑠𝑗=𝑠𝑗−2−𝑞𝑗−1𝑠𝑗−1,𝑡𝑗=𝑡𝑗−2−𝑞𝑗−1𝑡𝑗−1,\begin{aligned}{𝑠}_{𝑗} & ={𝑠}_{𝑗-2}-{𝑞}_{𝑗-1}{𝑠}_{𝑗-1}, \\ {𝑡}_{𝑗} & ={𝑡}_{𝑗-2}-{𝑞}_{𝑗-1}{𝑡}_{𝑗-1},\end{aligned}

for 2≤𝑗≤𝑛2\leq 𝑗\leq 𝑛. The cases 𝑗=2𝑗=2 and 𝑗=3𝑗=3 hold under the definitions just made for 𝑗=0𝑗=0 and 𝑗=1𝑗=1.

Theorem 3.9

Let 𝑎,𝑏∈Z+𝑎,𝑏\in {\mathbb{Z}}^{+} with 𝑎>𝑏𝑎>𝑏, and let 𝑛𝑛 be the number of steps the Euclidean algorithm takes on 𝑎𝑎 and 𝑏𝑏. Let 𝑞𝑗{𝑞}_{𝑗} be the quotients in that algorithm, and define 𝑠𝑗{𝑠}_{𝑗} and 𝑡𝑗{𝑡}_{𝑗} by

𝑠0=1,𝑡0=0,𝑠1=0,𝑡1=1,\begin{gathered}{𝑠}_{0}=1,\quad {𝑡}_{0}=0, \\ {𝑠}_{1}=0,\quad {𝑡}_{1}=1,\end{gathered}

and, for 2≤𝑗≤𝑛2\leq 𝑗\leq 𝑛,

𝑠𝑗=𝑠𝑗−2−𝑞𝑗−1𝑠𝑗−1,𝑡𝑗=𝑡𝑗−2−𝑞𝑗−1𝑡𝑗−1.\begin{aligned}{𝑠}_{𝑗} & ={𝑠}_{𝑗-2}-{𝑞}_{𝑗-1}{𝑠}_{𝑗-1}, \\ {𝑡}_{𝑗} & ={𝑡}_{𝑗-2}-{𝑞}_{𝑗-1}{𝑡}_{𝑗-1}.\end{aligned}

Then

gcd⁡(𝑎,𝑏)=𝑠𝑛𝑎+𝑡𝑛𝑏.\gcd (𝑎,𝑏)={𝑠}_{𝑛}𝑎+{𝑡}_{𝑛}𝑏.
Proof

Let 𝑟0=𝑎{𝑟}_{0}=𝑎, 𝑟1=𝑏{𝑟}_{1}=𝑏, and let 𝑟2,…,𝑟𝑛{𝑟}_{2},\ldots ,{𝑟}_{𝑛} be the later remainders, so 𝑟𝑛=gcd⁡(𝑎,𝑏){𝑟}_{𝑛}=\gcd (𝑎,𝑏) and, by (3.8),

𝑟𝑗=𝑟𝑗−2−𝑟𝑗−1𝑞𝑗−1{𝑟}_{𝑗}={𝑟}_{𝑗-2}-{𝑟}_{𝑗-1}{𝑞}_{𝑗-1}

for 2≤𝑗≤𝑛2\leq 𝑗\leq 𝑛. Let 𝑃(𝑗)𝑃(𝑗) be the assertion that 𝑟𝑗=𝑠𝑗𝑎+𝑡𝑗𝑏{𝑟}_{𝑗}={𝑠}_{𝑗}𝑎+{𝑡}_{𝑗}𝑏.

Base case. The values 𝑠0=1{𝑠}_{0}=1, 𝑡0=0{𝑡}_{0}=0, 𝑠1=0{𝑠}_{1}=0, and 𝑡1=1{𝑡}_{1}=1 give 𝑟0=1⋅𝑎+0⋅𝑏{𝑟}_{0}=1\cdot 𝑎+0\cdot 𝑏 and 𝑟1=0⋅𝑎+1⋅𝑏{𝑟}_{1}=0\cdot 𝑎+1\cdot 𝑏. Thus 𝑃(0)𝑃(0) and 𝑃(1)𝑃(1) hold. There are two base cases because the recurrence looks back two steps.

Inductive step. Let 2≤𝑗≤𝑛2\leq 𝑗\leq 𝑛, and assume 𝑃(𝑗−2)𝑃(𝑗-2) and 𝑃(𝑗−1)𝑃(𝑗-1). Then

𝑟𝑗=𝑟𝑗−2−𝑟𝑗−1𝑞𝑗−1=(𝑠𝑗−2𝑎+𝑡𝑗−2𝑏)−𝑞𝑗−1(𝑠𝑗−1𝑎+𝑡𝑗−1𝑏)=(𝑠𝑗−2−𝑞𝑗−1𝑠𝑗−1)𝑎+(𝑡𝑗−2−𝑞𝑗−1𝑡𝑗−1)𝑏=𝑠𝑗𝑎+𝑡𝑗𝑏,\begin{aligned}{𝑟}_{𝑗} & ={𝑟}_{𝑗-2}-{𝑟}_{𝑗-1}{𝑞}_{𝑗-1} \\ & =({𝑠}_{𝑗-2}𝑎+{𝑡}_{𝑗-2}𝑏)-{𝑞}_{𝑗-1}({𝑠}_{𝑗-1}𝑎+{𝑡}_{𝑗-1}𝑏) \\ & =({𝑠}_{𝑗-2}-{𝑞}_{𝑗-1}{𝑠}_{𝑗-1})𝑎+({𝑡}_{𝑗-2}-{𝑞}_{𝑗-1}{𝑡}_{𝑗-1})𝑏 \\ & ={𝑠}_{𝑗}𝑎+{𝑡}_{𝑗}𝑏,\end{aligned}

where the last step is the recurrence. Thus 𝑃(𝑗)𝑃(𝑗) holds.

By induction, 𝑃(𝑗)𝑃(𝑗) holds for every 𝑗𝑗 with 0≤𝑗≤𝑛0\leq 𝑗\leq 𝑛. In particular, 𝑃(𝑛)𝑃(𝑛) is gcd⁡(𝑎,𝑏)=𝑠𝑛𝑎+𝑡𝑛𝑏\gcd (𝑎,𝑏)={𝑠}_{𝑛}𝑎+{𝑡}_{𝑛}𝑏.

In a program, we can use the following, the so-called extended Euclidean algorithm, to express gcd⁡(𝑎,𝑏)\gcd (𝑎,𝑏) as a linear combination of 𝑎𝑎 and 𝑏𝑏.

Algorithm 3.2

Extended Euclidean algorithm

Input
𝑎,𝑏∈Z+𝑎,𝑏\in {\mathbb{Z}}^{+}
Output
gcd⁡(𝑎,𝑏)\gcd (𝑎,𝑏), 𝑠𝑠, 𝑡𝑡
1 (𝑠0,𝑠1)←(1,0)({𝑠}_{0},{𝑠}_{1})←(1,0)
2 (𝑡0,𝑡1)←(0,1)({𝑡}_{0},{𝑡}_{1})←(0,1)
3 while 𝑏>0𝑏>0 do
4 𝑞,𝑟←divmod⁡(𝑎,𝑏)𝑞,𝑟←\operatorname{divmod}(𝑎,𝑏)
5 𝑎←𝑏𝑎←𝑏
6 𝑏←𝑟𝑏←𝑟
7 (𝑠0,𝑠1)←(𝑠1,𝑠0−𝑞𝑠1)({𝑠}_{0},{𝑠}_{1})←({𝑠}_{1},{𝑠}_{0}-𝑞{𝑠}_{1})
8 (𝑡0,𝑡1)←(𝑡1,𝑡0−𝑞𝑡1)({𝑡}_{0},{𝑡}_{1})←({𝑡}_{1},{𝑡}_{0}-𝑞{𝑡}_{1})
9 return 𝑎𝑎, 𝑠0{𝑠}_{0}, 𝑡0{𝑡}_{0}