0% found this document useful (0 votes)
3 views8 pages

numtheory-05

This document discusses the concepts of divisibility and the greatest common divisor (gcd) in number theory. It introduces the Euclidean algorithm as an efficient method for computing the gcd of two integers through a series of divisions until a remainder of zero is reached. The document also includes examples and exercises to reinforce understanding of these concepts.

Uploaded by

Jola Haziri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

numtheory-05

This document discusses the concepts of divisibility and the greatest common divisor (gcd) in number theory. It introduces the Euclidean algorithm as an efficient method for computing the gcd of two integers through a series of divisions until a remainder of zero is reached. The document also includes examples and exercises to reinforce understanding of these concepts.

Uploaded by

Jola Haziri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Divisibility and the Greatest

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,

3|6 and 12|132, since 6=3·2 and 132 = 12 · 11.

The divisors of 6 are 1, 2, 3, and 6. On the other hand, 5  7, since no integer


multiple of 5 is equal to 7. A number that divides n is called a divisor of n.
If we are given two numbers, we can look for common divisors, that is, num-
bers that divide both of them. For example, 4 is a common divisor of 12 and 20,
since 4|12 and 4|20. Notice that 4 is the largest common divisor of 12 and 20.
Similarly, 3 is a common divisor of 18 and 30, but it is not the largest, since 6
is also a common divisor. The largest common divisor of two numbers is an ex-
tremely important quantity that will frequently appear during our number theoretic
excursions.

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.

Two examples that we mentioned above are

gcd(12, 20) = 4 and gcd(18, 30) = 6.

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

the following table:


1160718174 = 3 × 316258250 + 211943424
316258250 = 1 × 211943424 + 104314826
211943424 = 2 × 104314826 + 3313772
104314826 = 31 × 3313772 + 1587894
3313772 = 2 × 1587894 + 137984
1587894 = 11 × 137984 + 70070
137984 = 1 × 70070 + 67914
70070 = 1 × 67914 + 2156
67914 = 31 × 2156 + 1078 ← gcd
2156 = 2 × 1078 + 0
Notice how at each step we divide a number A by a number B to get a quotient Q
and a remainder R. In other words,

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

gcd(1160718174, 316258250) = 1078.

We can partly check our calculation (always a good idea) by verifying that 1078 is
indeed a common divisor. Thus

1160718174 = 1078 × 1076733 and 316258250 = 1078 × 293375.

There is one more practical matter to be mentioned before we undertake a


theoretical analysis of the Euclidean algorithm. If we are given A and B, how can
we find the quotient Q and the remainder R? Of course, you can always use long
division, but that can be time consuming and subject to arithmetic errors if A and B
are large. A pleasant alternative is to find a calculator or computer program that will
automatically compute Q and R for you. However, even if you are only equipped
with an inexpensive calculator, there is an easy three-step method to find Q and R.

Method to Compute Q and R on a Calculator So That A = B × Q + R


1. Use the calculator to divide A by B. You get a number with decimals.
2. Discard all the digits to the right of the decimal point. This gives Q.
3. To find R, use the formula R = A − B × Q.

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

ri−1 = qi+1 × ri + ri+1 .

Why is the last nonzero remainder rn a common divisor of a and b? We start


from the bottom and work our way up. The last line rn−1 = qn+1 rn shows that rn
divides rn−1 . Then the previous line

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

3. Let b = r0 , r1 , r2 , . . . be the successive remainders in the Euclidean algorithm applied


to a and b. Show that after every two steps, the remainder is reduced by at least one half.
In other words, verify that
1
ri+2 < ri for every i = 0, 1, 2, . . . .
2
Conclude that the Euclidean algorithm terminates in at most 2 log2 (b) steps, where log2 is
the logarithm to the base 2. In particular, show that the number of steps is at most seven
times the number of digits in b. [Hint. What is the value of log2 (10)?]

4. A number L is called a common multiple of m and n if both m and n divide L.


The smallest such L is called the least common multiple of m and n and is denoted by
LCM(m, n). For example, LCM(3, 7) = 21 and LCM(12, 66) = 132.
(a) Find the following least common multiples.
(i) LCM(8, 12) (ii) LCM(20, 30) (iii) LCM(51, 68) (iv) LCM(23, 18).
(b) For each of the LCMs that you computed in (a), compare the value of LCM(m, n)
to the values of m, n, and gcd(m, n). Try to find a relationship.
(c) Give an argument proving that the relationship you found is correct for all m and n.
(d) Use your result in (b) to compute LCM(301337, 307829).
(e) Suppose that gcd(m, n) = 18 and LCM(m, n) = 720. Find m and n. Is there more
than one possibility? If so, find all of them.

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:

(i) n = 21 (ii) n = 13 (iii) n = 31

(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.)

6. Write a program to implement the 3n + 1 algorithm described in the previous


exercise. The user will input n and your program should return the length L(n) and the
terminating value T (n) of the 3n + 1 algorithm. Use your program to create a table giving
the length and terminating value for all starting values 1 ≤ n ≤ 100.

36

You might also like