Euclidean algorithm

Revision as of 23:33, 18 June 2006 by Fedja (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The Euclidean algorithm allows to find the greatest common divisor of any two non-negative integers. The main idea is very simple: if we have two non-negative integers $a,b$ with $a\ge b$ and $b=0$, then the greatest common divisor is $\displaystyle{a}$. If $a\ge b>0$, then the set of common divisors of $\displaystyle{a}$ and $b$ is the same as the set of common divisors of $b$ and $r$ where $r$ is the remainder of division of $\displaystyle{a}$ by $b$. Indeed, we have $a=mb+r$ with some integer$m$, so, if $\displaystyle{d}$ divides both $\displaystyle{a}$ and $b$, it must divide both $\displaystyle{a}$ and $mb$ and, thereby, their difference $r$. Similarly, if $\displaystyle{d}$ divides both $b$ and $r$, it should divide $\displaystyle{a}$ as well. Thus, the greatest common divisors of $\displaystyle{a}$ and $b$ and of $b$ and $r$ coincide: $GCD(a,b)=GCD(b,r)$. But the pair $(b,r)$ consists of smaller numbers than the pair $(a,b)$! So, we reduced our task to a simpler one. And we can do this reduction again and again until the smaller number becomes $0$. To see how it works, just take an example $\displaystyle a=112$, $b=42$. We have $112=2\cdot 42+ 28$, so the pair $(112,42)$ can be replaced by $(42,28)$. Similarly, $42=1\cdot 28+14$, so we get the pair $(28,14)$. Now, $28=2\cdot 14+0$, so we get the pair $(14,0)$, which has the greatest common divisor $14$. Thus $GCD(112,42)=14$. Usually the Euclidean algorithm is written down just as a chain of divisions with remainder:
$112=2\cdot 42+ 28$
$42=1\cdot 28+14$
$28=2\cdot 14+0$
The Euclidean algorithm has one nice bonus: the so called linear representation of the greatest common divisor. To see how it works, let's use our equations backwards starting with the last but one:
$14=42-1\cdot 28=42-1\cdot(112-2\cdot 42)=3\cdot 42-1\cdot 112$
i.e., $14$ is expressed as a sum of $112$ and $42$ with integer coefficients.
How fast is the Euclidean algorithm? One can prove that if the larger of two numbers does not exceed the $n$-th Fibonacci number, then the required number of steps does not exceed $n$, so the number of steps is logarithmic in the size of the initial numbers (i.e., the number of steps is not much more than the number of digits in the largest of the two numbers).