Difference between revisions of "Euclidean algorithm"

m (proofreading)
Line 1: Line 1:
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 <math>a,b</math> with <math>a\ge b</math> and <math>b=0</math>, then the greatest common divisor is <math>\displaystyle{a}</math>. If <math>a\ge b>0</math>, then the set of common divisors of <math>\displaystyle{a}</math> and <math>b</math> is the same as the set of common divisors of <math>b</math> and <math>r</math> where <math>r</math> is the [[remainder]] of division of <math>\displaystyle{a}</math> by <math>b</math>. Indeed, we have <math>a=mb+r</math> with some integer<math>m</math>, so, if <math>\displaystyle{d}</math> divides both <math>\displaystyle{a}</math> and <math>b</math>, it must divide both <math>\displaystyle{a}</math> and <math>mb</math> and, thereby, their difference <math>r</math>. Similarly, if <math>\displaystyle{d}</math> divides both <math>b</math> and <math>r</math>, it should divide <math>\displaystyle{a}</math> as well. Thus, the greatest common divisors of <math>\displaystyle{a}</math> and <math>b</math> and of <math>b</math> and <math>r</math> coincide: <math>GCD(a,b)=GCD(b,r)</math>. But the pair <math>(b,r)</math> consists of smaller numbers than the pair <math>(a,b)</math>! So, we reduced our task to a simpler one. And we can do this reduction again and again until the smaller number becomes <math>0</math>.  
+
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 <math>a,b</math> with <math>a\ge b</math> and <math>b=0</math>, then the greatest common divisor is <math>\displaystyle{a}</math>. If <math>a\ge b>0</math>, then the set of common divisors of <math>\displaystyle{a}</math> and <math>b</math> is the same as the set of common divisors of <math>b</math> and <math>r</math>, where <math>r</math> is the [[remainder]] of division of <math>\displaystyle{a}</math> by <math>b</math>. Indeed, we have <math>a=mb+r</math> with some integer <math>m</math>, so, if <math>\displaystyle{d}</math> divides both <math>\displaystyle{a}</math> and <math>b</math>, it must divide both <math>\displaystyle{a}</math> and <math>mb</math> and, thereby, their difference <math>r</math>. Similarly, if <math>\displaystyle{d}</math> divides both <math>b</math> and <math>r</math>, it should divide <math>\displaystyle{a}</math> as well. Thus, the greatest common divisors of <math>\displaystyle{a}</math> and <math>b</math> and of <math>b</math> and <math>r</math> coincide: <math>GCD(a,b)=GCD(b,r)</math>. The pair <math>(b,r)</math> consists of smaller numbers than the pair <math>(a,b)</math>! So, we reduced our task to a simpler one. We can do this reduction again and again until the smaller number becomes <math>0</math>.  
 
<br>
 
<br>
 
To see how it works, just take an example <math>\displaystyle a=112</math>, <math>b=42</math>. We have <math>112=2\cdot 42+ 28</math>, so the pair <math>(112,42)</math> can be replaced by <math>(42,28)</math>. Similarly, <math>42=1\cdot 28+14</math>, so we get the pair <math>(28,14)</math>. Now, <math>28=2\cdot 14+0</math>, so we get the pair <math>(14,0)</math>, which has the greatest common divisor <math>14</math>. Thus <math>GCD(112,42)=14</math>.
 
To see how it works, just take an example <math>\displaystyle a=112</math>, <math>b=42</math>. We have <math>112=2\cdot 42+ 28</math>, so the pair <math>(112,42)</math> can be replaced by <math>(42,28)</math>. Similarly, <math>42=1\cdot 28+14</math>, so we get the pair <math>(28,14)</math>. Now, <math>28=2\cdot 14+0</math>, so we get the pair <math>(14,0)</math>, which has the greatest common divisor <math>14</math>. Thus <math>GCD(112,42)=14</math>.
Line 10: Line 10:
 
<math>28=2\cdot 14+0</math>
 
<math>28=2\cdot 14+0</math>
 
<br>
 
<br>
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:
+
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 one:
 
<br>
 
<br>
 
<math>14=42-1\cdot 28=42-1\cdot(112-2\cdot 42)=3\cdot 42-1\cdot 112</math>
 
<math>14=42-1\cdot 28=42-1\cdot(112-2\cdot 42)=3\cdot 42-1\cdot 112</math>

Revision as of 12:47, 19 June 2006

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)$. The pair $(b,r)$ consists of smaller numbers than the pair $(a,b)$! So, we reduced our task to a simpler one. 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 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).

Practice Problems

  • AIME 1985 #13 Let $f(n)$ be the greatest common divisor of $100 + n^{2}$ and $100 + (n+1)^{2}$ for $n = 1, 2, 3, \ldots$ . What is the maximum value of $f(n)$?