• No results found

The Greatest Common Divisor (GCD) and the Euclidean Algorithm

BASICS OF NUMBER THEORY

Theorem 2.22 If a and b are relatively prime, and if a divides kb for some some integer k, then a must divide k

2.6 The Greatest Common Divisor (GCD) and the Euclidean Algorithm

LAUNCH

Find the greatest common divisor of 20 and 35. What method did you use to find the answer? Now find the greatest common divisor of 16, 807 and 14, 406. If you were able to find it, did you use the same method you used in the first problem? Why or why not?

We imagine that you probably had a lot of difficulty finding the greatest common divisor of the large numbers presented in the launch question. You might have used a “factoring tree” quite easily with the first pair of small numbers, but it is much more difficult to do with the pair of large numbers. The purpose of this section is to introduce you to an algorithm that will help you find the GCD rather easily and will give you other insights about numbers and their greatest common divisors.

One of the fundamental topics stressed throughout the middle and secondary school curricu-lum is the greatest common divisor or greatest common factor of two numbers a and b. We denote this by gcd (a, b). This is the largest number that divides both a and b. So, for example, gcd (6, 8) is 2 and gcd (10, 15) = 5.

The greatest common divisor is not only useful in mathematics. It has found uses in developing secure codes that even the National Security agency can’t break, and hence is useful for our own national security. It has been used in developing certain musical rhythms and also in neutron accelerators as well as in computer design and so on. [An interesting article detailing some of this is

“The Euclidean Algorithm Generates Traditional Musical Rhythms”, by Godfried Toussaint (2005).]

In the book Number Theory in Science and Communication, by M.R. Shroeder (1988), we find the following: “An interesting and most surprising application of the greatest common divisor occurs in human perception of pitch: the brain, when confronted with harmonically related frequencies, will perceive the GCD [greatest common divisor] of these frequencies as the pitch.” (page 5)

When the greatest common divisor of two numbers a and b is 1, then a and b have no prime factors in common and so they are relatively prime. Thus, another way to define the expression

“a and b are relatively prime” is to say that gcd (a, b) = 1. So, 8 and 15 are relatively prime since gcd (8, 15) = 1 as are the numbers 14 and 17 since gcd (14, 17) = 1. Of course, gcd (a, b) = gcd (b, a).

We also observe that, if a is positive, gcd (a, a) = a

gcd (a, 1) = 1 and gcd (a, 0) = a.

Make sure you can explain why each of these statements is true.

Finding the greatest common divisor of two numbers when the two numbers are factored into primes is simple. For each common prime in the factorizations, we take the lowest power of the prime we see and multiply the results. Thus, if we wanted to find the greatest common divisor of the numbers M = 26· 39· 7 and N = 28· 34· 11, we would notice that the common primes in the factorizations are 2 and 3, and that the lowest power of 2 we see is 26, while the lowest power of 3 we see is 34. Thus, gcd (M, N) = 26· 34. We use this idea in algebra when we factor expressions.

Thus, if we have 3a3b2 and 6ab3 and we want to find the greatest common factor of these two expressions (which is synonymous with greatest common divisor), we treat the a and b as if they are primes. Thus, gcd (3a3b2, 6ab3) = 3ab2, where we have factored out the smallest power of each common “prime.” We tell our students that, in any factoring problem, we always factor out the greatest common divisor of the terms first. Thus, if we have to factor 3a3b2 + 6ab3, we factor out 3ab2and we get 3ab2(a2+ 2b). In a similar manner, if we want to factor x4− 9x2, we factor out the gcd first, which is x2 and we get x2(x2− 9) = x2(x− 3)(x + 3).

Finding the gcd of numbers by factoring into primes is easy when the numbers are small or are already in factored form. For example, if we wanted to find the gcd (24, 18) we get 6 very quickly.

Imagine though trying to find the gcd of 4562 and 2460 or numbers much larger than these.

Factoring would be cumbersome and time consuming. In practice, it is not done this way since in practical applications the numbers are usually extremely large, making it inefficient and difficult to factor even with the help of computers. Instead, there is a better method known as the Euclidean Algorithm to find the gcd of two numbers. It shows us how to transform the gcd of two numbers into the gcd of two numbers which are at best, smaller. Continued application of this yields the gcd of the two numbers.

Theorem 2.26 (Version 1 of Euclidean Algorithm) If a and b are integers, then gcd (a, b) = gcd (b, a− b).

Proof. We will show that the set of divisors of a and b coincides with the set of divisors of b and a− b. Thus, the largest number which divides a and b is also the largest number that divides b and a− b. That is, gcd(a, b) = gcd (b, a − b). For example, gcd (15, 6) = gcd (6, 9) = 3.

Now let h be any divisor of a and b. Then, since h is a divisor of a and b, it divides a − b by Theorem 2.7 . Thus any divisor of a and b is a divisor of b and a− b.

Now we show the reverse. Suppose that h is any divisor of b and a− b. Then h certainly divides b, and by Theorem 2.7, h divides the sum (a− b) + b or just a. That is, any divisor of b and a − b divides both a and b. Thus h is a divisor of a and b.

We have shown in the bold statements that each divisor, h, of a and b is a divisor of b and a− b and conversely. Thus, the divisors of aand b and b and a − b are the same. So, the greatest common divisor of a and b is the greatest common divisor of b and a− b. 

This algorithm is very easy to program in a computer and is very quick and efficient. Here is an algebraic example of how to use this theorem.

Example 2.27 Show that for any integer n, gcd (n + 1, n) = 1. (This is Example 2.8 redone differently.)

Solution. gcd (n + 1, n) = gcd (n, n + 1 − n) = gcd (n, 1) = 1. Observe the factoring method is useless here. In a similar manner we can show that gcd (2n + 1, n) = 1 for any integer n.

Here is another version of the Euclidean Algorithm which one sees more frequently.

Theorem 2.28 (Euclidean Algorithm version 2). Suppose that a> b and that, when we divide a by b, we get a remainder of r. Then gcd (a, b) = gcd (b, r ).

Proof. The proof is almost the same as the proof of Theorem 2.26. We know that a = bq + r for some q. Looking at the right side of this equation, we see that any divisor of b and r must divide the left side, a (Theorem 2.7). Thus, the divisors of b and r are divisors of a (and b). From the relationship a− bq = r , we observe by looking at the left side of this equation that any divisor of a and b divides the right side, r and of course, b (again by Theorem 2.7). Thus, the divisors of a and b are divisors of r and b. Two bolded statements together show us that the divisors of a and b are the same as those of b and r.

We have shown that the divisors of a and b are the same as those of b and r. It follows that gcd (a, b) = gcd (b, r ). 

You might think of Theorem 2.28 as the “new and improved” version of Theorem 2.26.

This new Algorithm is very efficient, and computers always employ it when finding gcd (a, b) by repeatedly applying this theorem. In fact, when books refer to “the” Euclidean Algorithm, they mean repeated application of version 2 of the Euclidean Algorithm, and we will refer to it likewise.

Notice, we always find the greatest common divisor of the smaller number and the remainder when the larger number is divided by the smaller number. We repeat this over and over until we finally get to gcd (g, 0) at which point we know that g is the greatest common divisor of a and b. A way to express this mathematically, calling the remainders at each stage, r1, r2, and so on is, gcd (a, b) = gcd (b, r1) = gcd(r1, r2) = . . . gcd (g, 0) = g. To clarify how to use this version of the Euclidean Algorithm, let us revisit some examples we mentioned earlier.

Example 2.29 Find (a) gcd (24, 18) and then find (b) gcd (4562, 2460).

Solution. (a) 24 and 18 are both easy to factor. 24 = 23· 3 and 18 = 2 · 32, so by our factoring method of taking the lowest power of each common factor and multiplying them together, we

get gcd (24, 18) = 2· 3 = 6. Had we done this using version 2 of the Euclidean Algorithm, the steps would have been gcd (24, 18) = gcd (18, 6) (since the remainder when we divide 24 by 18 is 6) and gcd (18, 6) = gcd (6, 0) since the remainder when 18 is divided by 6 is 0. But now we are done since we know that gcd (6, 0) = 6.

Let us show, in Figure 2.5, how this would look by long division.

18 18 24 1

6

18 18 3 6

previous 0 remainder

previous divisor

when this is zero we are done divisor

remainder

Figure 2.5

In our first step, we divide the larger number 24 by the smaller number 18. We look only at the remainder, 6. That becomes our new divisor, and we try to divide the most recent divisor, 18, by the remainder 6. The method is always, “Divide the most recent divisor by the remainder until you get a remainder of 0 in which case your latest divisor is the gcd.”

(b) This is more difficult to do by the factoring method. Let us do this by version 2 of the Euclidean Algorithm: gcd (4562, 2460) = gcd (2460, 2102) = gcd(2102, 358) = gcd (358, 312) = gcd (312, 46) = gcd (46, 36) = gcd (36, 10) = gcd (10, 6) = gcd (6, 4) = gcd (4, 2) = gcd (2, 0) = 2.

There is a useful result that follows from Theorem 2.7, which is not obvious, and which we will use later on when we study Diophantine equations. We will also use its corollary, which gives us neater proofs of some theorems. They really are quite important in the study of number theory.

Theorem 2.30 If g is the greatest common divisor of a and b, then g = ma + nb for some integers m