Download Computing the Greatest Common Divisor

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

List of prime numbers wikipedia , lookup

Approximations of π wikipedia , lookup

History of logarithms wikipedia , lookup

Factorization wikipedia , lookup

Arithmetic wikipedia , lookup

Algorithm wikipedia , lookup

Elementary mathematics wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Location arithmetic wikipedia , lookup

Factorization of polynomials over finite fields wikipedia , lookup

Transcript
Math 470
Communication and Cryptography
Fall 2005
Computing the Greatest Common Divisor
Let a and b be fixed positive integers. We recall that the greatest common divisor of a and b is the
largest integer d dividing both a and b, and is denoted in the text by gcd(a, b).
We discuss three important ways to compute the greatest common divisor. One way is by
prime factorization. The second is the Euclidean algorithm. The third is by a tally version of
the generalized Euclidean algorithm. It is important that you understand and can use all three
methods. This discussion references materials in the Trappe & Washington text on pp. 66-68
(Greatest Common Divisor) and pp. 69-70 (Solving ax + by = d).
Computing gcd(a, b) by prime factorization.
Reference T&W, p. 66. Computing gcd(a, b) by prime factorization is easy in theory. It is also
easy in practice provided that you can readily find the prime factors of a and b. However as we
will see later in the course, it is commonly not the case that we cannot easily find the prime factors
of a given number.
The method is this. Factor a and b into primes. Ignore prime factors that appear in a or in b
but not both. For each prime factor that appears in both factorizations, choose the smaller power
of that prime. Multiply these prime factors together to obtain gcd(a, b). Here are some examples:
Examples.
(a) Compute gcd(22 · 35 · 53 , 34 · 56 · 7). The prime 2 divides the first number but not the second,
and the prime 7 divides the second number but not the first. Neither number will be a factor
of the gcd. The common prime factors are 3 and 5. The prime 3 appears as 35 and 34 , and we
choose the lower power 34 . Similarly we choose the lower power 53 . The gcd is 34 · 53 .
(b) Compute gcd(22 · 35 , 56 · 7). The two numbers share no prime factors, so the gcd is 1.
Computing gcd(a, b) by the Euclidean algorithm.
Reference T&W, p. 67. The Euclidean algorithm for calculating greatest common divisors is very
simple and fast. It is a descent algorithm in the sense that we will do a sequence of simple operations that produce a decreasing sequence of posititve integers. When that sequence terminates in a
finite number of steps as it must (why?), then the last number is the gcd. Let us illustate.
The general idea is to divide the smaller number into the larger and keep only the remainder, if
any. Replace the larger number by the smaller, and replace the smaller number by the remainder.
Repeat. At every step, we produce a remainder which must be smaller than all previous remainders.
Greatest Common Divisor
c 2005 by Jon T. Pitts
Copyright Page 1 of 6
Math 470
Communication and Cryptography
Fall 2005
At some point there is no remainder (or equivalently the remainder is zero) at which time the last
remainder that you calculated is the gcd.
Example. Find gcd(1180, 482) by the Euclidean algorithm. Proceed as follows.
(1) Divide 1180 by 482, drop the quotient, and keep the remainder. The remainder is 216.
(2) Replace 1180 by 482, and replace 482 by 216. Divide 482 by 216 and keep only the remainder.
The remainder is 50.
(3) Continuing, replace 482 by 216, and replace 216 by 50. Divide 216 by 50. The remainder is
16.
(4) Divide 50 by 16. The remainder is 2.
(5) Divide 16 by 2. There is no remainder. Stop here. The last nonzero remainder is 2.
(6) Conclude that gcd(1180, 482) = 2.
Computing gcd(a, b) by the tally method.
Reference T&W, pp. 66, 69-70. We present a version of what the text calls the extended Euclidean
algorithm. We call this version the tally method be cause it reduces the calculation of gcd(a, b) to
constructing a simple table. This table has significant advantages:
• The table is easy to compute.
• The table automatically calculates number x and y such that ax + by = gcd(a, b). This is an
extremely useful fact.
• The table similarly displays all intermediate results.
Example. We illustrate the tally method by recalculating the example gcd(a, b), T&W, pp. 66,
69 where a = 1180 and b = 482. These are the same numbers that we just used to illustrate
the Euclidean algorithm. However we are going to extract much more information with not much
more work.
NOTE: In order to avoid notational confusion, notice that we’ve reversed the values of a and
b from what is in the text on page 69. This does not change the answer, but we will always wish
to begin the table with the larger number and by convention we’ll write that number first. As a
result, if you are comparing the calculations here with the example in the text, please notice that
the values of numbers xi , yi are swapped compared to the text.
The tally method builds a table. The table will have six columns as follows.
Greatest Common Divisor
c 2005 by Jon T. Pitts
Copyright Page 2 of 6
Math 470
Communication and Cryptography
Fall 2005
Column 1 We are going to number the lines in our table i = 0, 1, 2 . . .. The index i will be entered
in column 1.
Column 2 Corresponding to each i, we calculate numbers mi to be entered into column 2. m0
is always the larger number which we call a. In this example, m0 = a = 1180. m1 is the
smaller number which we call b. In this example m1 = b = 482. When we compute row
number i+2, we will begin by calculating mi+2 from rows i and i+1. This will be explained
below.
Columns 3 and 4 For each i, we will be computing mi = xi a + yib. Column 3 contains x1 and
column 4 contains y1 . Obviously x0 = 1, y0 = 0 and x1 = 0, y1 = 1.
Column 5 and 6 Columns 5 and 6 are blank in row 0. For row i+1, we will divide mi by mi+1 and
compute mi = qi+1 mi+1 + ri+1 . The quotient qi+1 is entered in column 5 and the remainder
ri+1 is entered into column 6.
Now we begin the calculations.
Row i=0.
i
0
mi xi
1180 1
yi qi
0 -
ri
-
Check that 1180 = m0 = 1180x0 + 482y0 = 1180(1) + 482(0).
Row i=1. We calculate this row in two steps. First we can easily fill in the first four entries.
i
0
1
mi xi
1180 1
482 0
yi qi
0 1
ri
-
To fill in q1 and r1 , we divide m0 = 1180 by m1 = 482 obtaining a quotient q1 = 2 and remainder
r1 = 216.
i
0
1
mi xi
1180 1
482 0
yi qi
ri
0 1 2 216
Check that 482 = m1 = 1180x1 + 482y1 = 1180(0) + 482(1).
Greatest Common Divisor
c 2005 by Jon T. Pitts
Copyright Page 3 of 6
Math 470
Communication and Cryptography
Fall 2005
Row i=2. We calculate this row from rows i = 0 and i = 1. First, the number m2 is simply the
remainder r1 from the previous row. In fact, in general mi+2 = ri+1 at every stage.
i
0
1
2
mi xi
1180 1
482 0
216
yi qi
ri
0 1 2 216
Filling in x2 and y2 are key. The idea is to compute x2 by multiplying x1 by q1 and subtracting it
from x0 . In equation form, x2 = x0 − x1 q1 = 1 − 0(2) = 1. The value of y2 is computed similarly
as y2 = y0 − y1 q1 = 0 − 1(2) = −2.
i
0
1
2
mi xi
1180 1
482 0
216 1
yi qi
ri
0 1 2 216
-2
Finally we complete row 2 by dividing m1 = 482 by m2 = 216 obtaining a quotient q2 = 2 and
remainder r2 = 50.
i
0
1
2
mi xi
1180 1
482 0
216 1
yi qi
ri
0 1 2 216
-2 2 50
Check that 216 = m2 = 1180x2 + 482y2 = 1180(1) + 482(−2).
Row i=3. We calculate this row from rows i = 1 and i = 2. The number m3 is simply the remainder r2 = 50 from the previous row. Analogously to what we did in row 2, x3 = x1 − x2 q2 =
0 − 2(1) = −2 and y3 = y1 − y2 q2 = 1 − (−2)(2) = 5. Finally, we divide m2 = 216 by m3 = 50
obtaining a quotient q3 = 4 and remainder r3 = 16.
i
0
1
2
3
mi xi
1180 1
482 0
216 1
50 -2
yi qi
ri
0 1 2 216
-2 2 50
5 4 16
Check that 50 = m3 = 1180x3 + 482y3 = 1180(−2) + 482(5).
Greatest Common Divisor
c 2005 by Jon T. Pitts
Copyright Page 4 of 6
Math 470
Communication and Cryptography
Fall 2005
Row i=4. We repeat as for row 3.
i
0
1
2
3
4
mi xi
1180 1
482 0
216 1
50 -2
16 9
yi qi
ri
0 1 2 216
-2 2 50
5 4 16
-22 3
2
Check that 16 = m4 = 1180x4 + 482y4 = 1180(9) + 482(−22).
Row i=5. We repeat as for row 4.
i
0
1
2
3
4
5
mi
xi
1180
1
482
0
216
1
50 -2
16
9
2 -29
yi qi
ri
0 1 2 216
-2 2 50
5 4 16
-22 3
2
71 8
0
We notice that something unusual has occurred. At this step, for this first (and last) time, our remainder r5 = 0. We are almost at the stopping point and we are ready to read off gcd(1180, 482).
At this step, when the remainder ri vanishes, the number mi will be our greatest common
divisor. In other words, m5 = 2 = gcd(482, 1180). Furthermore we obtain the dividend that we
can find 2 = gcd(482, 1180) as a linear combination of a = 482 and b = 1180. In fact, we check
that 2 = m5 = 1180x5 + 482y5 = 1180(−29) + 482(71).
Row i=6. Since the remainder r5 vanished, this means that we can go only one more step. We
bring down r5 = 0 = m6 and calculate as much as we can.
i
0
1
2
3
4
5
6
mi
xi
1180
1
482
0
216
1
50
-2
16
9
2 -29
0 241
yi qi
0 1 2
-2 2
5 4
-22 3
71 8
-590 -
Greatest Common Divisor
ri
216
50
16
2
0
c 2005 by Jon T. Pitts
Copyright Page 5 of 6
Math 470
Communication and Cryptography
Fall 2005
We check that 0 = m6 = 1180x6 + 482y6 = 1180(241) + 482(−590), an unexpected dividend.
Important conclusions.
Two very important results follow from these tally table calculations. We will use these results
throughout the course. Let a and b be positive integers.
(a) There exist integers x and y such that ax + by = gcd(a, b). In other words, gcd(a, b) may
always be written as an integral linear combination of a and b.
(b) If gcd(a, b) = 1, then a and b are said to be relatively prime because they have no common
prime factors. If a and b are relatively prime, then there exists integers x and y such that
ax + by = 1.
Exercises
1. Compute d = gcd(330, 286).
(a) Use the prime factorization method to find d. (Answer: d = 22.)
(b) Use the tally method to find d. Find numbers x and y such that 330x + 286y = d. From
the last line of the tally table, read numbers u, v such that 330u + 286v = 0. (Answer:
x = −6 and y = 7. u = 13 and v = −15.)
2. Find numbers x and y such that 77x + 64y = 1. (Hint: Notice that gcd(77, 64) = 1.)
Greatest Common Divisor
c 2005 by Jon T. Pitts
Copyright Page 6 of 6