# The Euclidean algorithm

The Euclidean algorithm

The Euclidean algorithm is a way to find the greatest common divisor of two positive integers, a and b. First let me show the computations for a=210 and b=45.

• Divide 210 by 45, and get the result 4 with remainder 30, so 210=4·45+30.
• Divide 45 by 30, and get the result 1 with remainder 15, so 45=1·30+15.
• Divide 30 by 15, and get the result 2 with remainder 0, so 30=2·15+0.
• The greatest common divisor of 210 and 45 is 15.

Let’s see. Several questions occur immediately..

1. What’s going on here?
2. Why does the algorithm stop?
3. Why is the answer correct?

O.k. I will answer first question by giving a formal description of the algorithm, which supposedly finds the greatest common divisor (GCD) of two integers. This “greatest common divisor” must exist, since positive integer divisors of integers can’t be any larger than the integers:

 Formal description of the Euclidean algorithm Input Two positive integers, a and b. Output The greatest common divisor, g, of a and b. Internal computation If a

Here is one link which describes the algorithm. Here’s another link, with more information. If you want to “do” some further examples, Paul Garrett of the University of Minnesota has a Visible Euclidean algorithm page where you can specify further examples and see the algorithm work..

 Why does the algorithm stop?At each step, the remainder, r, decreases by at least 1. Therefore r must eventually be 0. A formal proof would use mathematical induction.
 Why is the answer actually equal to the GCD?In the final stage, when r=0, we see that the “final b” must divide the final a. Go backwards along the string of equations (a=q·b+r), and you will see that at each step, the “final b” divides each of the parts of the equation. Therefore the “final b” must be a divisor of both of the initial a and initial b. Now look at the first equation. In the equation a=q·b+r, the GCD divides both a and b and therefore must divide r as well (because a-q·r is a multiple of the GCD). Now carry this observation through all of the equations, forward. The GCD must divide two of the three pieces in all of the equations, and thus must divide the third. Therefore, the GCD also divides the “final b”. So the “final b” divides both a and b, and is itself a multiple of the GCD. Well, the GCD is the greatest such divisor, and therefore the “final b” must be equal to the GCD of the initial values.

The extended Euclidean algorithm

Here’s a true statement:

 If a and b are positive integers, then there are always integers m and n so that the GCD of a and b equals m·a+n·b.

The extended Euclidean algorithm (described, for example, here, allows the computation of multiplicative inverses mod P. First let’s see an example. Since the GCD of 210 and 45 is 15, we should be able to write 15 as a sum of multiples of 210 and 45. Here’s how to do it. We look carefully at the steps above and change them each.

• Divide 210 by 45, and get the result 4 with remainder 30, so 210=4·45+30.
30=1·210-4·45.
• Divide 45 by 30, and get the result 1 with remainder 15, so 45=1·30+15.
15=45-1·30=45-1·(1·210-4·45)=-1·210+5·45

The greatest common divisor of 210 and 45 is 15, and we have written 15 as a sum of integer multiples of 210 and 45.

The extended Euclidean algorithm has a very important use: finding multiplicative inverses mod P. Choose a prime, P: how about 97. I know 97 is prime, because 2 and 3 and 5 and 7 and even 11 aren’t factors of 97, and I only need to check division by primes up to the square root of 97.

Now let me take a fairly random integer, say 20. Since 20 is less than 97, and 97 is prime, the GCD of 20 and 97 should be 1. (Remember, since 97 is prime, its only divisors are itself and 1.) I will verify this by “running” the Euclidean algorithm:

• 97=4·20+17
• 20=1·17+3
• 17=5·3+2
• 3=1·2+1

The extended Euclidean algorithm allows us to write 1 as a sum of 97 and 20. Here we go:

• 17=1·97-4·20
• 20-1·17=3 so 3=1·20-1·17=1·20-(1·97-4·20)=-1·97+5·20
• 17=5·3+2 so 2=17-5·3=(1·97-4·20)-5(-1·97+5·20)=6·97-29·20
• 1=3-2=(-1·97+5·20)(6·97-29·20)=-7·97+34·20

The final equation tells us that 1=-7·97+34·20, which means that the product of 34 and 20 is equal to 1 plus a multiple of 97. But in mod 97, we ignore multiples of 97. Therefore 34 is the multiplicative inverse of 20 mod 97.

Exercise
Find the multiplicative inverse of 60 mod 97 by hand. As I mentioned in class, doing just one of these computations “by hand” is good enough. Here’s a link to the answer.

The extended Euclidean algorithm is easy to implement on a computer and the amount of memory needed is not large. The algorithm runs very fast. For example, I am currently running a copy ofMaple in another window on a PC which is neither very fast nor has much memory. I just selected a 100 decimal digit prime, P, and found the multiplicative inverse of a 50 decimal digit number mod P. The system returned 0.00 as the amount of time used. That is, the computation used less than .01 seconds!