Euclidean Algorithm

Algorithm
The Euclidean Algorithm (or Euclid's Algorithm / Euclidean Division 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 $\left({a, b}\right)$ such that $\left|{a}\right| \ge \left|{b}\right|$. If $b = 0$ then you are done and the GCD is $a$.
 * $(2): \quad$ If $b \neq 0$ then you take the remainder $r$ of $\dfrac a b$.
 * $(3): \quad$ Set $a \gets b, b \gets r$ (and thus $\left|{a}\right| \ge \left|{b}\right|$ again).
 * $(4): \quad$ Repeat these steps until $b=0$.

Thus the GCD of $a$ and $b$ is the value of the variable $a$ at the end of the algorithm.

It can be seen from the definition that the Euclidean algorithm is indeed an algorithm in the strict mathematical sense.


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

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

From the Division Theorem, $a = q b + r$ where $0 \le r < \left|{b}\right|$.

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 \left\{{b, r}\right\}$.

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

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

Demonstration
We can investigate in more detail what happens when we apply the Division Theorem repeatedly to $a$ and $b$.

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 < \left|{b}\right|$.

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.

Algorithmic Nature

 * 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.

Constructing an Integer Combination
Having done this, we are now in a position to find a solution to $\gcd \left\{{a, b}\right\} = 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 above, we get:

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

(However, see Rational Numbers and SFCFs are Equivalent‎ for an application of the Euclidean algorithm in a slightly different context.)

Thus eventually we arrive at $\gcd \left\{{a, b}\right\} = 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 \backslash b \implies \gcd \left\{{a, b}\right\} = a$, from GCD of Integer and Divisor.