Computing the Greatest Common Divisor
Total Page:16
File Type:pdf, Size:1020Kb
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,andwe 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 opera- tions 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 Copyright c 2005 by Jon T. Pitts 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.Divide482 by 216 and keep only the remainder. The remainder is 50. (3) Continuing, replace 482 by 216, and replace 216 by 50.Divide216 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).Thisisan 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 Copyright c 2005 by Jon T. Pitts 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. Columns3and4 For each i, we will be computing mi = xia + yib. Column 3 contains x1 and column 4 contains y1. Obviously x0 =1,y0 =0and x1 =0,y1 =1. Column 5 and 6 Columns 5 and 6 are blank in row 0.Forrowi+1, we will divide mi by mi+1 and compute mi = qi+1mi+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 mi xi yi qi ri 0 1180 1 0 - - 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 mi xi yi qi ri 0 1180 1 0 - - 1 482 0 1 To fill in q1 and r1,wedividem0 = 1180 by m1 = 482 obtaining a quotient q1 =2and remainder r1 = 216. i mi xi yi qi ri 0 1180 1 0 - - 1 482 0 1 2 216 Check that 482 = m1 = 1180x1 + 482y1 = 1180(0) + 482(1). Greatest Common Divisor Copyright c 2005 by Jon T. Pitts Page 3 of 6 Math 470 Communication and Cryptography Fall 2005 Row i=2. We calculate this row from rows i =0and 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 mi xi yi qi ri 0 1180 1 0 - - 1 482 0 1 2 216 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 − x1q1 =1−0(2) = 1.Thevalueofy2is computed similarly as y2 = y0 − y1q1 =0−1(2) = −2. i mi xi yi qi ri 0 1180 1 0 - - 1 482 0 1 2 216 2 216 1 -2 Finally we complete row 2 by dividing m1 = 482 by m2 = 216 obtaining a quotient q2 =2and remainder r2 =50. i mi xi yi qi ri 0 1180 1 0 - - 1 482 0 1 2 216 2 216 1 -2 2 50 Check that 216 = m2 = 1180x2 + 482y2 = 1180(1) + 482(−2). Row i=3. We calculate this row from rows i =1and i =2. The number m3 is simply the re- mainder r2 =50from the previous row. Analogously to what we did in row 2, x3 = x1 − x2q2 = 0 − 2(1) = −2 and y3 = y1 − y2q2 =1−(−2)(2) = 5. Finally, we divide m2 = 216 by m3 =50 obtaining a quotient q3 =4and remainder r3 =16. i mi xi yi qi ri 0 1180 1 0 - - 1 482 0 1 2 216 2 216 1 -2 2 50 3 50 -2 5 4 16 Check that 50 = m3 = 1180x3 + 482y3 = 1180(−2) + 482(5). Greatest Common Divisor Copyright c 2005 by Jon T. Pitts Page 4 of 6 Math 470 Communication and Cryptography Fall 2005 Row i=4. We repeat as for row 3. i mi xi yi qi ri 0 1180 1 0 - - 1 482 0 1 2 216 2 216 1 -2 2 50 3 50 -2 5 4 16 4 16 9 -22 3 2 Check that 16 = m4 = 1180x4 + 482y4 = 1180(9) + 482(−22). Row i=5. We repeat as for row 4. i mi xi yi qi ri 0 1180 1 0 - - 1 482 0 1 2 216 2 216 1 -2 2 50 3 50 -2 5 4 16 4 16 9 -22 3 2 5 2 -29 71 8 0 We notice that something unusual has occurred. At this step, for this first (and last) time, our re- mainder r5 =0. We are almost at the stopping point and we are ready to read off gcd(1180; 482).