Skip to main content
Logo image

Section 4.3 Euclidean Algorithm

We turn the strategy from Example 4.2.7 into an algorithm for finding greatest common divisors. As in the example, we repeatedly apply Theorem 4.2.6 3. 4 to replace the computation of \(\gcd(a,b)\) with \(\gcd(a\fmod b, b)\text{.}\) At each step, the numbers become smaller. We stop when the remainder \(a\fmod b\) is zero.
The algorithm is named after the Greek mathematician Euclid. He first described it in Book 7 of his Elements (around 300 BC) [5]. To make the algorithm easier to present, we allow only natural numbers (positive integers) as inputs.
In the video in Figure 4.3.1 we introduce the algorithm and use it to find greatest common divisors.
Figure 4.3.1. Euclidean Algorithm by Matt Farmer and Stephen Steward
We now write the Euclidean Algorithm in our algorithm format.
In the algorithm, \(r\) becomes smaller in each iteration of the loop. As \(r\) is a non-negative integer, it has to become zero eventually. So after finitely many steps the algorithm returns a result.

Example 4.3.3. Greatest common divisor of \(612\) and \(56\).

We compute the greatest common divisor of \(612\) and \(56\) with Algorithm 4.3.2.
Input: \(a:=612\) and \(b:=56\)
    1. let \(r:=612\fmod 56=52\)
    2. let \(a:=56\)
    3. let \(b:=52\)
  1. As \(r=52\) the statement \(r=0\) is false. So we continue Item 1 with:
    1. let \(r:=56\fmod 52=4\)
    2. let \(a:=52\)
    3. let \(b:=4\)
  1. As \(r=4\) the statement \(r=0\) is false. So we continue Item 1 with:
    1. let \(r:=52\fmod 4=0\)
    2. let \(a:=4\)
    3. let \(b:=0\)
  1. As \(r=0\) the statement \(r=0\) is true. So we continue Item 3 with:
  2. We return the value of \(a\) which is \(4\text{.}\)
Output: \(4\)
We have found that the greatest common divisor of \(612\) and \(56\) is \(4\text{.}\)
Next we repeat the previous example. Instead of explicitly writing down what happens in every step, we write down the value of each variable at the end of step 1. This notation is more suitable for the computation of greatest common divisors by hand.

Example 4.3.4. Greatest common divisor of \(612\) and \(56\) compact.

We compute the greatest common divisor of \(612\) and \(56\) with Algorithm 4.3.2. In the table we give the values of the variables at the end of step 1 in each iteration of the loop.
step \(r\) \(a\) \(b\)
Input \(612\) \(56\)
(1) \(612\fmod 56=52\) \(56\) \(52\)
(1) \(56\fmod 52=4\) \(52\) \(4\)
(1) \(52\fmod 4=0\) \(4\) \(0\)
Output 4
So the output is \(\gcd(612,56)=4\text{.}\)
In the interactive algorithm in Example 4.3.5, click each step of the Euclidean algorithm to see how the variable values change.

Example 4.3.5. Euclidean algorithm interactive.

Now we use the Euclidean Algorithm (Algorithm 4.3.2) to prove that consecutive natural numbers are coprime.

Proof.

Let \(n\) be a natural number. We compute \(\gcd(n+1,n)\) using the Euclidean Algorithm. with Algorithm 4.3.2. In the table we give the values of the variables after step (1) in each iteration of the loop.
step \(r\) \(a\) \(b\)
Input \(n+1\) \(n\)
(1) \((n+1)\fmod n=1\) \(n\) \(1\)
(1) \(n\fmod 1=0\) \(1\) \(0\)
Output 1
So, \(\gcd(n+1,n) = 1\text{,}\) and we conclude that \(n+1\) and \(n\) are coprime.
We use a numerical example to show the proof of the theorem.

Example 4.3.7. \(238\) and \(237\) are coprime.

We compute the greatest common divisor of \(238\) and \(237\) with Algorithm 4.3.2. In the table we give the values of the variables after step (1) in each iteration of the loop.
step \(r\) \(a\) \(b\)
Input \(238\) \(237\)
(1) \(238\fmod 237=1\) \(237\) \(1\)
(1) \(237\fmod 1=0\) \(1\) \(0\)
Output \(1\)
So the output is \(\gcd(238,237)=1\text{.}\)