# Euclidean algorithm

Euclid of Alexandria was a Greek mathematician. He was called the “Father of Geometry”. He wrote one of the most influential works in the history of mathematics – Elements.

Although Elements are mostly about geometry, Elements also include number theory. In this lesson we’ll give a little insight of this great mathematicians mind and his influence in the number theory.

Image credit: Wikimedia Commons

The Euclidean Algorithm is a method for finding the greatest common divisor of two integers. Before showing the exact algorithm first we should set few rules and notations.

If a|b and a|c, we say that a is the common divisor of numbers b and c.

If at least one of numbers b and c is different from zero, then there exist only finitely many common divisors of b and c. The greatest of them is called the greatest common divisor of b and c. GCD is denoted as (b, c).

If we have two numbers whose greatest common divisor is one, we say that those two numbers are relatively prime. For example 40 and 13 are relatively prime. We denote this as:

and are relatively prime numbers

You also don’t necessarily have to observe only two numbers. More than two numbers can also be relatively prime. If we have numbers whose only common divisor is 1, we say that those numbers are relatively prime. Also if every pair of those numbers is relatively prime we say that there are pairwise relatively prime.

Lemma 1. If , , , then .

Put in words, this lemma tells us that if we have a number b, that is divisible with number a with remainder r, then the greatest common divisor from a and b will be the same as the greatest common divisor of and .

As an example we can take 6 and 4. We know that . We can see now that

Proof. Let’s say that , .

| .This means that d1 is the common divisor of a and r.

Since we got that and that we can conclude that .

. This means that is the common divisor of and .

From here we got that and that which means that .

We now got that and that which can only mean that .

## Algorithm

Let’s assume that with repeated application of the remainder theorem we got the following string of equalities:

,
,

,

,

The procedure ends when we get remainder equal to zero. The procedure has to end in finally many steps.

Lemma 1 implies that the greatest common divisor of a and b is equal to rn because:

This means that is equal to the last non trivial remainder in the upper procedure.

This process for finding GCD is called the Euclidean algorithm.

Let’s see how this algorithm works in practice.

Example Find GCD of and .

First we’ll divide with and write it down as a sum of a product and remainder.

Since 4 is the last non trivial remainder this means that