# Euclidean Algorithm

## Contents

## Algorithm

The **Euclidean algorithm** is a method for finding the greatest common divisor (GCD) of two integers $a$ and $b$.

Let $a, b \in \Z$ and $a \ne 0 \lor b \ne 0$.

The steps are:

- $(1): \quad$ Start with $\tuple {a, b}$ such that $\size a \ge \size b$. If $b = 0$ then the task is complete and the GCD is $a$.
- $(2): \quad$ If $b \ne 0$ then you take the remainder $r$ of $\dfrac a b$.
- $(3): \quad$ Set $a \gets b, b \gets r$ (and thus $\size a \ge \size b$ again).
- $(4): \quad$ Repeat these steps until $b = 0$.

Thus the GCD of $a$ and $b$ is the value of the variable $a$ after the termination of the algorithm.

In the words of Euclid:

*Given two (natural) numbers not prime to one another, to find their greatest common measure.*

(*The Elements*: Book $\text{VII}$: Proposition $2$)

## Proof 1

Suppose $a, b \in \Z$ and $a \lor b \ne 0$.

From the Division Theorem, $a = q b + r$ where $0 \le r < \size b$.

From GCD with Remainder, the GCD of $a$ and $b$ is also the GCD of $b$ and $r$.

Therefore, we may search instead for $\gcd \set {b, r}$.

Since $\size r < \size b$ and $b \in \Z$, we will reach $r = 0$ after finitely many steps.

At this point, $\gcd \set {r, 0} = r$ from GCD with Zero.

$\blacksquare$

## Euclid's Proof

Let $AB, CD$ be the two given (natural) numbers which are not coprime.

We need to find the greatest common divisor of $AB$ and $CD$.

Without loss of generality that $CD \le AB$.

We have that $CD$ is a divisor of itself.

If $CD$ is a divisor of $AB$ then $CD$ is a common divisor of $CD$ and $AB$.

It is clearly the greatest, because no number greater than $CD$ can be a divisor of $CD$.

Now, suppose $CD$ does not divide $AB$.

Then the less of the numbers $AB, CD$ being continually subtracted from the greater, some number will be left which will be a divisor of the one before it.

From Coprimality Criterion, this number will not be a unit, otherwise $AB$ and $CD$ will be coprime.

So some number will be left which is a divisor of the one before it.

Now let $CD$, dividing $BE$, leave $EA$ less than itself.

Let $EA$, dividing $DF$, leave $FC$ less than itself.

Let $CF$ divide $AE$.

Since then $CF$ divides $AE$, and $AE$ divides $DF$, then $CF$ will also divide $DF$.

But it also divides itself.

Therefore it will also divide the whole $CD$.

But $CD$ divides $BE$, therefore $CF$ divides $BE$.

But it also divides $EA$, therefore it will also divide the whole $BA$.

So $CF$ is a common divisor of $CD$ and $AB$.

Suppose $CF$ is not the greatest common divisor of $CD$ and $AB$.

Let $G > CF$ also be a common divisor of $CD$ and $AB$.

Since $G$ divides $CD$, while $CD$ divides $BE$, it follows that $G$ divides $BE$.

But $G$ also divides the whole $BA$, and so it also divides the remainder $AE$.

But $AE$ divides $DF$.

Therefore $G$ divides $DF$.

But $G$ also divides the whole $DC$.

Therefore it will also divide the remainder $CF$.

But $G$ is greater than $CF$, which is impossible.

Therefore no number greater than $CF$ divides the numbers $AB$ and $CD$.

Therefore $CF$ is the greatest common divisor of $AB$ and $CD$.

$\blacksquare$

## Demonstration

Using the **Euclidean Algorithm**, we can investigate in detail what happens when we apply the Division Theorem repeatedly to $a$ and $b$.

\(\displaystyle a\) | \(=\) | \(\displaystyle q_1 b + r_1\) | |||||||||||

\(\displaystyle b\) | \(=\) | \(\displaystyle q_2 r_1 + r_2\) | |||||||||||

\(\displaystyle r_1\) | \(=\) | \(\displaystyle q_3 r_2 + r_3\) | |||||||||||

\(\displaystyle \cdots\) | \(\) | \(\displaystyle \) | |||||||||||

\(\displaystyle r_{n - 2}\) | \(=\) | \(\displaystyle q_n r_{n - 1} + r_n\) | |||||||||||

\(\displaystyle r_{n - 1}\) | \(=\) | \(\displaystyle q_{n + 1} r_n + 0\) |

From the Division Theorem, we know that the remainder is always strictly less than the divisor.

That is, in $a = q b + r$:

- $0 \le r < \size b$

So we know that:

- $b > r_1 > r_2 > \ldots > r_{n - 2} > r_{n - 1} > r_n > 0$

So the algorithm *has* to terminate.

$\blacksquare$

## Algorithmic Nature

It can be seen from the definition that the **Euclidean Algorithm** is indeed an algorithm:

- Finiteness: As has been seen, the algorithm always terminates after a finite number of steps.
- Definiteness: Each of the steps is precisely defined.
- The inputs are $a$ and $b$.
- The output is $\gcd \left\{{a, b}\right\}$.
- Effectiveness: Each operation is finite in extent and can be effectively performed.

## Formal Implementation

The **Euclidean Algorithm** can be implemented as a computational method $\left({Q, I, \Omega, f}\right)$ as follows:

Let $Q$ be the set of:

- all singletons $\left({n}\right)$
- all ordered pairs $\left({m, n}\right)$
- all ordered quadruples:
- $\left({m, n, r, 1}\right)$
- $\left({m, n, r, 2}\right)$
- $\left({m, n, p, 3}\right)$

where $m, n, p \in \Z_{> 0}$ and $r \in \Z_{\ge 0}$.

Let $I \subseteq Q$ be the set of all ordered pairs $\left({m, n}\right)$.

Let $\Omega$ be the set of all singletons $\left({n}\right)$.

Let $f$ be defined as follows:

- $f \left({\left({m, n}\right)}\right) = \left({m, n, 0, 1}\right)$
- $f \left({\left({n}\right)}\right) = \left({n}\right)$
- $f \left({\left({m, n, r, 1}\right)}\right) = \left({m, n, \operatorname{rem} \left({m / n}\right), 2}\right)$
- $f \left({\left({m, n, r, 2}\right)}\right) = \begin{cases}\left({n}\right) & : r = 0 \\ \left({m, n, r, 3}\right) & : r \ne 0 \end{cases}$
- $f \left({\left({m, n, p, 3}\right)}\right) = \left({n, p, p, 1}\right)$

where $\operatorname{rem} \left({m / n}\right)$ denotes the remainder of $m$ on division by $n$.

## Constructing an Integer Combination

Having determined the GCD of $a$ and $b$ using the Euclidean Algorithm, we are now in a position to find a solution to $\gcd \set {a, b} = x a + y b$ for $x$ and $y$.

By Bézout's Identity it is always possible to find such an $x, y \in \Z$.

Working back through the equations generated during the course of working the Euclidean Algorithm, we get:

\(\displaystyle \gcd \set {a, b}\) | \(=\) | \(\displaystyle r_n\) | |||||||||||

\(\displaystyle \) | \(=\) | \(\displaystyle r_{n - 2} - q_n r_{n - 1}\) | |||||||||||

\(\displaystyle \) | \(=\) | \(\displaystyle r_{n - 2} - q_n \paren {r_{n - 3} - q_{n - 1} r_{n - 2} }\) | |||||||||||

\(\displaystyle \) | \(=\) | \(\displaystyle \paren {1 + q_n q_{n - 1} } r_{n - 2} - q_n r_{n - 3}\) | |||||||||||

\(\displaystyle \) | \(=\) | \(\displaystyle \paren {1 + q_n q_{n - 1} } \paren {r_{n - 4} - q_{n - 2} r_{n - 3} } - q_n r_{n - 3}\) |

... and so on. The algebra gets messier the further up the tree you go, and is not immediately enlightening.

Thus eventually we arrive at $\gcd \set {a, b} = x a + y b$ where $x$ and $y$ are numbers made up from some algebraic cocktail of the coefficients of the terms involving the remainders from the various applications of the Division Theorem.

Note that $a \divides b \implies \gcd \set {a, b} = a$, from GCD of Integer and Divisor.

## Also known as

The **Euclidean algorithm** is also known as **Euclid's algorithm** or the **Euclidean division algorithm**.

## Examples

### GCD of $341$ and $527$

The GCD of $341$ and $527$ is found to be:

- $\gcd \set {341, 527} = 31$

#### Integer Combination

$31$ can be expressed as an integer combination of $341$ and $527$:

- $31 = 2 \times 527 - 3 \times 341$

### GCD of $2190$ and $465$

The GCD of $2190$ and $465$ is found to be:

- $\gcd \set {2190, 465} = 15$

Hence $15$ can be expressed as an integer combination of $2190$ and $465$:

- $15 = 33 \times 465 - 7 \times 2190$

### GCD of $9 n + 8$ and $6 n + 5$

The GCD of $9 n + 8$ and $6 n + 5$ is found to be:

- $\gcd \set {9 n + 8, 6 n + 5} = 1$

Hence:

- $2 \paren {9 n + 8} - 3 \paren {6 n + 5} = 1$

### Solution of $31 x \equiv 1 \pmod {56}$

Let $x \in \Z$ be an integer such that:

- $31 x \equiv 1 \pmod {56}$

Then by using the Euclidean Algorithm:

- $x = -9$

is one such $x$.

## Also see

- Rational Numbers and SFCFs are Equivalent for an application of the
**Euclidean algorithm**in a slightly different context.

## Historical Note

This theorem is Proposition $2$ of Book $\text{VII}$ of Euclid's *The Elements*.

## Source of Name

This entry was named for Euclid.

## Sources

- 1969: C.R.J. Clapham:
*Introduction to Abstract Algebra*... (previous) ... (next): $\S 3.11$ - 1971: George E. Andrews:
*Number Theory*... (previous) ... (next): $\text {2-2}$ Divisibility: Theorem $\text {2-2}$ - 1971: Allan Clark:
*Elements of Abstract Algebra*... (previous) ... (next): Chapter $1$: Properties of the Natural Numbers: $\S 23 \zeta$ - 1978: Thomas A. Whitelaw:
*An Introduction to Abstract Algebra*... (previous) ... (next): $\S 12$: Highest common factors and Euclid's algorithm - 1986: David Wells:
*Curious and Interesting Numbers*... (previous) ... (next): $5$ - 1989: Ephraim J. Borowski and Jonathan M. Borwein:
*Dictionary of Mathematics*... (previous) ... (next): Entry:**Euclidean algorithm** - 1989: Ephraim J. Borowski and Jonathan M. Borwein:
*Dictionary of Mathematics*... (previous) ... (next): Entry:**Euclid's algorithm**or**Euclidean algorithm** - 1996: John F. Humphreys:
*A Course in Group Theory*... (previous) ... (next): $\text{A}.1$: Number theory - 1997: Donald E. Knuth:
*The Art of Computer Programming: Volume 1: Fundamental Algorithms*(3rd ed.) ... (previous) ... (next): $\S 1.1$: Algorithms: Algorithm $\text{E}$ - 1997: David Wells:
*Curious and Interesting Numbers*(2nd ed.) ... (previous) ... (next): $5$ - 2008: David Nelson:
*The Penguin Dictionary of Mathematics*(4th ed.) ... (previous) ... (next): Entry:**Euclidean algorithm** - 2014: Christopher Clapham and James Nicholson:
*The Concise Oxford Dictionary of Mathematics*(5th ed.) ... (previous) ... (next): Entry:**Euclidean Algorithm**