Monday Math: The Euclidean Algorithm

My number theory class has moved on from Pythagorean triples. Lately we've been talking about the Euclidean algorithm. Specifically, it's an algorithm for finding the greatest common divisor (gcd) of two numbers.

Of course, there are lots of ways finding the gcd. You could simply list all of the divisors of the first number, all of the divisors of the second number, and then compare the two lists. Let me suggest, though, that this method gets tedious in a hurry. Even for a computer it's hopelessly inefficient.

A somewhat better way involves finding the prime factorizations of the two numbers. The gcd can then be found by looking at the intersection of the two factorizations. But this too is rather inefficient when you are dealing with large numbers.

Which brings us to the big algorithm. It is based on the idea that if A and B are the two numbers, with A larger, then we can divide A by B to get a quotient and a remainder:


\[
A=qB+r
\]

 

Though I won't stop to do a formal proof, it is now the case that


\[
\gcd(A,B)=\gcd(B,r),
\]

 

which is pretty cool, since r is smaller than B. The basic idea is that in our previous equation, any divisor of two of the three terms must also divide the third term as well.

Perhaps now the strategy is clear. We just keep dividing, so that our problem involves finding the gcd of smaller and smaller numbers.

(For the TeX hounds in the audience, I should point out that my TeX renderer does not seem to recognize the align environment. Hence the somewhat awkward layout.)

As an example, let us take


\[
A=147 \phantom{xxx} \textrm{and} \phantom{xxx} B=114.
\]

 

Then we can compute:


\[
147=(1)(114)+33
\]

 


\[
114=(3)(33)+15
\]

 


\[
33=(2)(15)+3
\]

 


\[
15=(3)(5)
\]

 

We now conclude that the gcd is 3, the logic being that


\[
\gcd(147,114)=\gcd(114,33)=\gcd(33,15)=\gcd(15,3).
\]

 

Not a bad trick! And extremely efficient, as it happens.

But I know what you're thinking. You're thinking: Gosh! It sure is cool that you can find the gcd so easily. But now suppose that A and B are two integers with gcd equal to d. Can I use the Euclidean algorithm to find (possibly negative) integers x and y that solve the equation:


\[
Ax+By=d?
\]

 

Indeed you can. The trick is to work the algorithm backward. For example, if I want to find a solution to


\[
147x+114y=3,
\]

 

I can reason like this:


\[
3=33-(2)(15)
\]

 


\[
3=33-(2)(114-(3)(33))= -2(114)+7(33)
\]

 


\[
3= -2(114)+7(147-(1)(114))=147(7)+114(-9)
\]

 

which certainly does the trick. Now, I grant you that little computation might seem like it came out of left field. It turns out though, that it is just what we will need later to solve certain problems involving modular arithmetic. Don't know what that is? Well, just stay tuned!

More like this

The big number theory class has moved on to prime factorizations and the fundamental theorem of arithmetic. As it happens, though, I've already done a post on that subject. Looking back at what I wrote then I see that I left out one important detail. I asserted without proof (though I did provide…
Time to finish what we started last week. We saw that if a, b, c was a primitive Pythagorean triple, then at least one of a and b is even and one is odd. Let us declare, then, that we will use a to denote the odd length and b to denote the even one. By rearranging the Pythagorean equation and…
I know what you're thinking. You're thinking, “Gosh, it sure is neat that we can generate all Pythagorean triples from one simple formula, but what happens if we try an exponent bigger than two? That is, can you find nontrivial integer solutions to the equation \[ x^n+y^n=z^n \]   when n is a…
The big number theory course has moved on to modular arithmetic, which means we have been discussing Fermat's Little Theorem. Personally I've always thought that name is just adorable. As it happens, I already did a post on this topic. But since that post is close to a year old, and since I did…

All real texers know to use \gcd to get the right font. gcd, like sin, cos, etc. should always appear in the roman font.

By Donald Knooth (not verified) on 07 Feb 2011 #permalink

Something to amuse: Jason hasn't explicitly stated it here, but the gcd algorithm in pretty much every book out there that mentions it requires that the remainder be positive. A variant of the algorithm allows you to choose the reminder negative if it makes the magnitude smaller. For example, given 423 and 107, the classic algorithm would use 423 = 3 * 107 + 102 so gcd(423,107)=gcd(107,102). The alternative is 423 = 4 * 107 - 5 so gcd(423,107)=gcd(107,5).

It's been proven that this approach will sometimes take the same number of steps to find the gcd, will usually take less steps, but never takes more. I have no idea why the insistence that the remainder be positive...

âthe gcd algorithm in pretty much every book out there that mentions it requires that the remainder be positive. A variant of the algorithm allows you to choose the reminder negativeâ

Interesting. I didn't know it's computationally more efficient but I do remember being led to discover it by a problem in a textbook's (Niven, Zuckerman, Montgomery) section on divisibility and the Euclidean algorithm:

âLet a and b be positive integers such that (1 + ab)|(a² + b²). Show that the integer (a² + b²)/(1 + ab) must be a perfect square.â

Something else to amuse. :)

phayes, that's cruel.

(That problem was Q6 in the 1988 International Mathematical Olympiad, and is still notorious for having been very difficult by the already rather tough standards of the IMO.)

Stephen --

Thanks for the comment. I knew you had done some work on the Euclidean algorithm, but I didn't remember specifically what you had looked at.

@g Notoriously difficult eh? :-D I suppose so and I expect that's why I remembered it.

phayes, an interesting problem...

A little numerical exploration inspired the following solution using an infinite descent proof:

a=b clearly implies a=b=1.

Take some general solution a 1)

If b = ka, clearly k = a^2 (and b = a^3). This is a solution for all values of a.

b >= ka+1 is impossible because b^2 >= b(1+ka) = b + kab > k + kab

Otherwise, b < ka
Note that k + b^2 < k + kab = a^2 + b^2
and so k < a^2

We can descend to a smaller solution (c,a) with the same divisor k by setting c=ka-b:

c^2 + a^2
= (ka-b)^2 + a^2
= (ka)^2 - 2kab + b^2 + a^2
= ka.ka - 2kab + k + kab
= ka.ka - kab + k
= ka(ka-b) + k
= k(1 + ac)
as requred. Also note that
a^2 + b^2 = k + kab
a^2 - k = kab - b^2
a^2 > b(ka - b)
therefore since b>a, c=(ka-b)

By Jon Wharf (not verified) on 17 Feb 2011 #permalink

Ha ha, curse you html!

Reposted with suitable protection and of course preview followed by re-pasting:

phayes, an interesting problem...

A little numerical exploration inspired the following solution using an

infinite descent proof:

a=b clearly implies a=b=1.

Take some general solution a<b with divisor k.
a^2 + b^2 = k(1 + ab) = k + kab

(Note that ab < b^2 therefore k > 1)

If b = ka, clearly k = a^2 (and b = a^3). This is a solution for all values

of a.

b >= ka+1 is impossible because b^2 >= b(1+ka) = b + kab > k + kab

Otherwise, b < ka
Note that k + b^2 < k + kab = a^2 + b^2
and so k < a^2

We can descend to a smaller solution (c,a) with the same divisor k by setting

c=ka-b:

c^2 + a^2
= (ka-b)^2 + a^2
= (ka)^2 - 2kab + b^2 + a^2
= ka.ka - 2kab + k + kab
= ka.ka - kab + k
= ka(ka-b) + k
= k(1 + ac)
as requred. Also note that
a^2 + b^2 = k + kab
a^2 - k = kab - b^2
a^2 > b(ka - b)
therefore since b>a, c=(ka-b)<a

Only those solutions which reduce via descent to the form b=ka can exist.

Therefore k is always a square.

By Jon Wharf (not verified) on 17 Feb 2011 #permalink