numtheory-05
numtheory-05
Common Divisor
From Chapter 5 of A Friendly Introduction to Number Theory, Fourth Edition. Joseph H. Silverman.
Copyright
c 2013 by Pearson Education, Inc. All rights reserved.
29
Divisibility and the Greatest
Common Divisor
The notions of divisibility and factorizations are important tools in number theory.
In this chapter we will look at these ideas more closely.
Suppose that m and n are integers with m = 0. We say that m divides n if n is
a multiple of m, that is, if there is an integer k such that n = mk. If m divides n,
we write m|n. Similarly, if m does not divide n, then we write m n. For example,
The greatest common divisor of two numbers a and b (not both zero)
is the largest number that divides both of them. It is denoted by
gcd(a, b). If gcd(a, b) = 1, we say that a and b are relatively prime.
30
Divisibility and the Greatest Common Divisor
Another example is
gcd(225, 120) = 15.
We can check that this answer is correct by factoring 225 = 32 · 52 and 120 =
23 · 3 · 5, but, in general, factoring a and b is not an efficient way to compute their
greatest common divisor.1
The most efficient method known for finding the greatest common divisors of
two numbers is called the Euclidean algorithm. It consists of doing a sequence of
divisions with remainder until the remainder is zero. We will illustrate with two
examples before describing the general method.
As our first example, we will compute gcd(36, 132). The first step is to di-
vide 132 by 36, which gives a quotient of 3 and a remainder of 24. We write this
as
132 = 3 × 36 + 24.
The next step is to take 36 and divide it by the remainder 24 from the previous step.
This gives
36 = 1 × 24 + 12.
Next we divide 24 by 12, and we find a remainder of 0,
24 = 2 × 12 + 0.
The Euclidean algorithm says that as soon as you get a remainder of 0, the re-
mainder from the previous step is the greatest common divisor of the original two
numbers. So in this case we find that gcd(132, 36) = 12.
Let’s do a larger example. We will compute
gcd(1160718174, 316258250).
Our reason for doing a large example like this is to help convince you that the
Euclidean algorithm gives a far more efficient way to compute gcd’s than factor-
ization. We begin by dividing 1160718174 by 316258250, which gives 3 with a
remainder of 211943424. Next we take 316258250 and divide it by 211943424.
This process continues until we get a remainder of 0. The calculations are given in
1
An even less efficient way to compute the greatest common divisor of a and b is the method
taught to my daughter by her fourth grade teacher, who recommended that the students make com-
plete lists of all the divisors of a and b and then pick out the largest number that appears on both
lists!
31
Divisibility and the Greatest Common Divisor
A = Q × B + R.
Then at the next step we replace our old A and B with the numbers B and R and
continue the process until we get a remainder of 0. At that point, the remainder R
from the previous step is the greatest common divisor of our original two numbers.
So the above calculation shows that
We can partly check our calculation (always a good idea) by verifying that 1078 is
indeed a common divisor. Thus
32
Divisibility and the Greatest Common Divisor
For example, suppose that A = 12345 and B = 417. Then A/B = 29.6043 . . . ,
so Q = 29 and R = 12345 − 417 · 29 = 252.
We’re now ready to analyze the Euclidean algorithm. The general method
looks like
a= q1 × b + r1
b = q2 × r1 + r2
r1 = q3 × r2 + r3
r2 = q4 × r3 + r4
..
.
rn−3 = qn−1 × rn−2 + rn−1
rn−2 = qn × rn−1 + rn ← gcd
rn−1 = qn+1 rn + 0
If we let r0 = b and r−1 = a, then every line looks like
rn−2 = qn × rn−1 + rn
shows that rn divides rn−2 , since it divides both rn−1 and rn . Now looking at the
line above that, we already know that rn divides both rn−1 and rn−2 , so we find
that rn also divides rn−3 . Moving up line by line, when we reach the second line we
will already know that rn divides r2 and r1 . Then the second line b = q2 × r1 + r2
tells us that rn divides b. Finally, we move up to the top line and use the fact
that rn divides both r1 and b to conclude that rn also divides a. This completes our
verification that the last nonzero remainder rn is a common divisor of a and b.
But why is rn the greatest common divisor of a and b? Suppose that d is any
common divisor of a and b. We will work our way back down the list of equations.
So from the first equation a = q1 × b + r1 and the fact that d divides both a and b,
we see that d also divides r1 . Then the second equation b = q2 r1 + r2 shows us
that d must divide r2 . Continuing down line by line, at each stage we will know
that d divides the previous two remainders ri−1 and ri , and then the current line
ri−1 = qi+1 × ri + ri+1 will tell us that d also divides the next remainder ri+1 .
Eventually, we reach the penultimate line rn−2 = qn × rn−1 + rn , at which point
we conclude that d divides rn . So we have shown that if d is any common divisor
of a and b then d will divide rn . Therefore, rn must be the greatest common divisor
of a and b.
33
Divisibility and the Greatest Common Divisor
This completes our verification that the Euclidean algorithm actually com-
putes the greatest common divisor, a fact of sufficient importance to be officially
recorded.
Theorem 1 (Euclidean Algorithm). To compute the greatest common divisor of
two numbers a and b, let r−1 = a, let r0 = b, and compute successive quotients
and remainders
ri−1 = qi+1 × ri + ri+1
for i = 0, 1, 2, . . . until some remainder rn+1 is 0. The last nonzero remainder rn
is then the greatest common divisor of a and b.
There remains the question of why the Euclidean algorithm always finishes. In
other words, we know that the last nonzero remainder will be the desired gcd, but
how do we know that we ever get a remainder that does equal 0? This is not a
silly question, since it is easy to give algorithms that do not terminate; and there
are even very simple algorithms for which it is not known whether or not they
always terminate. Fortunately, it is easy to see that the Euclidean algorithm always
terminates. The reason is simple. Each time we compute a quotient with remainder,
A = Q × B + R,
the remainder will be between 0 and B − 1. This is clear, since if R ≥ B, then we
can add one more onto the quotient Q and subtract B from R. So the successive
remainders in the Euclidean algorithm continually decrease:
b = r0 > r 1 > r 2 > r 3 > · · · .
But all the remainders are greater than or equal to 0, so we have a strictly decreasing
sequence of nonnegative integers. Eventually, we must reach a remainder that
equals 0; in fact, it is clear that we will reach a remainder of 0 in at most b steps.
Fortunately, the Euclidean algorithm is far more efficient than this. You will show
in the exercises that the number of steps in the Euclidean algorithm is at most seven
times the number of digits in b. So, on a computer, it is quite feasible to compute
gcd(a, b) when a and b have hundreds or even thousands of digits!
Exercises
1. Use the Euclidean algorithm to compute each of the following gcd’s.
(a) gcd(12345, 67890) (b) gcd(54321, 9876)
2. Write a program to compute the greatest common divisor gcd(a, b) of two inte-
gers a and b. Your program should work even if one of a or b is zero. Make sure that you
don’t go into an infinite loop if a and b are both zero!
34
Divisibility and the Greatest Common Divisor
5. The “3n + 1 algorithm” works as follows. Start with any number n. If n is even, divide
it by 2. If n is odd, replace it with 3n + 1. Repeat. So, for example, if we start with 5, we
get the list of numbers
5, 16, 8, 4, 2, 1, 4, 2, 1, 4, 2, 1, . . . ,
and if we start with 7, we get
7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1, . . . .
Notice that if we ever get to 1 the list just continues to repeat with 4, 2, 1’s. In general, one
of the following two possibilities will occur:2
(i) We may end up repeating some number a that appeared earlier in our list, in which
case the block of numbers between the two a’s will repeat indefinitely. In this case
we say that the algorithm terminates at the last nonrepeated value, and the number
of distinct entries in the list is called the length of the algorithm. For example, the
algorithm terminates at 1 for both 5 and 7. The length of the algorithm for 5 is 6,
and the length of the algorithm for 7 is 17.
(ii) We may never repeat the same number, in which case we say that the algorithm does
not terminate.
2
There is, of course, a third possibility. We may get tired of computing and just stop working, in
which case one might say that the algorithm terminates due to exhaustion of the computer!
35
Divisibility and the Greatest Common Divisor
(a) Find the length and terminating value of the 3n+1 algorithm for each of the following
starting values of n:
(b) Do some further experimentation and try to decide whether the 3n + 1 algorithm
always terminates and, if so, at what value(s) it terminates.
(c) Assuming that the algorithm terminates at 1, let L(n) be the length of the algorithm
for starting value n. For example, L(5) = 6 and L(7) = 17. Show that if n = 8k + 4
with k ≥ 1, then L(n) = L(n + 1). [Hint. What does the algorithm do to the starting
values 8k + 4 and 8k + 5?]
(d) Show that if n = 128k + 28 then L(n) = L(n + 1) = L(n + 2).
(e) Find some other conditions, similar to those in (c) and (d), for which consecutive
values of n have the same length. (It might be helpful to begin by using the next
exercise to accumulate some data.)
36