Difference between revisions of "Euclidean algorithm"
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>. 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>. | + | 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>. |
+ | <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>. | ||
Usually the Euclidean algorithm is written down just as a chain of divisions with remainder: | Usually the Euclidean algorithm is written down just as a chain of divisions with remainder: | ||
<br> | <br> |
Revision as of 23:40, 18 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 with and , then the greatest common divisor is . If , then the set of common divisors of and is the same as the set of common divisors of and where is the remainder of division of by . Indeed, we have with some integer, so, if divides both and , it must divide both and and, thereby, their difference . Similarly, if divides both and , it should divide as well. Thus, the greatest common divisors of and and of and coincide: . But the pair consists of smaller numbers than the pair ! So, we reduced our task to a simpler one. And we can do this reduction again and again until the smaller number becomes .
To see how it works, just take an example , . We have , so the pair can be replaced by . Similarly, , so we get the pair . Now, , so we get the pair , which has the greatest common divisor . Thus .
Usually the Euclidean algorithm is written down just as a chain of divisions with remainder:
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:
i.e., is expressed as a sum of and with integer coefficients.
How fast is the Euclidean algorithm? One can prove that if the larger of two numbers does not exceed the -th Fibonacci number, then the required number of steps does not exceed , 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 be the greatest common divisor of and for . What is the maximum value of ?