MTH4105 Introduction to Mathematical Computing Chapter 9 supplement School of Mathematical Sciences © 2015 by Dr Francis J. Wright Division and divisibility For some non-examinable optional extra intellectual stimulation, this supplement explores the division operation and how it leads to the important relation of divisibility. Division and divisibility: numer, denom In general, the result of dividing two integers or two polynomials is a fraction, which 2 2 x consists of a numerator divided by a denominator, e.g. or . Interactively, you x C1 3 can select the numerator or denominator of a fraction in the same way that you can select any other text, but Maple also provides functions for this purpose, called numer and denom, which work like this: 2 2 2 x x 2 = 2, denom = 3, numer = x2, denom = x C1. numer 3 x C1 x C1 3 Division becomes much more interesting if we require that the result must be in the original set, i.e. the result of dividing two integers must be an integer and the result of dividing two polynomials must be a polynomial. In general, this division will not be possible unless we allow a remainder to be left over. In Maple, divisibility functions that apply only to integers have names that begin with "i" to distinguish them from those that apply to polynomials. We have already seen this with ifactor and factor and we will see some more examples below. Integer division: iquo, irem Integer division is a straightforward generalization of the division of natural numbers considered in Mathematical Structures. Let a, b be integers. We say that b divides a, or a is divisible by b, which we write symbolically as b a, if there is an integer q such that a = b q. We call a, b, q respectively the divisor, dividend and quotient. Note that this divisibility test is a relation and the value of the expression b a is either true or false. It is clear from the definition that b 0 for all b because 0 = b$0, but 0 § a unless a = 0 because a s 0$q for any finite q. In general, the relation a = b q does not hold, but provided b s 0 it is always true that a = b q Cr for some integer r, which is what would be left over if we were to divide a by b, so we call r the remainder. We can impose conditions on r that make q and r unique. Page 1 of 9 28-Nov-2015 Integer quotient and remainder are implemented in Maple as the functions iquo and irem such that q = iquo a, b and r = irem a, b satisfy the equation a = b q Cr and the conditions that r ! b and the sign of r is the same as the sign of a make the values of q and r unique. For example: > a, b d 7, 3 a, b := 7, 3 (1.2.1) > q, r d iquo a, b , irem a, b q, r := 2, 1 > evalb a = b q Cr , evalb r ! b , evalb sign r = sign a true, true, true > a, b dK7, 3 > q, r d iquo a, b , irem a, b > evalb a = b q Cr , evalb (1.2.2) (1.2.3) a, b := K7, 3 (1.2.4) q, r := K2, K1 (1.2.5) r ! b , evalb sign r = sign a true, true, true (1.2.6) There appears to be no way to use the vertical bar notation actively for divisibility, but we could implement an infix integer divisibility predicate and test it like this: > `&divides` d b, a /is irem a, b = 0 &divides := b, a /is irem a, b = 0 (1.2.7) > 3 &divides 7, 3 &divides 6 false, true (1.2.8) Quiz Given two integers a, b, which of the following evaluates to true if and only if a is an integer multiple of b? is iquo b, a = 0 No. iquo b, a is the integer quotient of b divided by a. This is the wrong division function and the division is the wrong way around! > iquo 3, 9 , iquo 3, 10 0, 0 (1.2.1.1.1) is iquo a, b = 0 No. iquo a, b is the integer quotient of a divided by b. This is the wrong division function. > iquo 9, 3 , iquo 10, 3 3, 3 (1.2.1.2.1) is irem b, a = 0 No. irem b, a is the integer remainder of b divided by a. This division is the wrong way around! > irem 3, 9 , irem 3, 10 3, 3 (1.2.1.3.1) is irem a, b = 0 Yes. irem a, b is the integer remainder of a divided by b. If this is zero then b divides a. > irem 9, 3 , irem 10, 3 0, 1 (1.2.1.4.1) Page 2 of 9 28-Nov-2015 Exercises Assign the number 12345 to the variable n. It is obvious that 5 divides n. (Why?) Use irem to confirm that 5 is a divisor of n and then use iquo to assign the quotient of n divided by 5 back to the variable n. It is now obvious (even more so than before) that 3 divides n. (Why?) Use irem to confirm that 3 is a divisor of n and then use iquo to assign the quotient of n divided by 3 back to the variable n. Show that n is now prime and hence write down the prime factorization of 12345. > Solution > n d 12345 n := 12345 It is obvious that 5 divides n because the last digit of n is 5. > irem n, 5 0 (1.2.2.1.1) (1.2.2.1.2) > n d iquo n, 5 n := 2469 (1.2.2.1.3) It is obvious that 3 divides n because the sum of the digits, which is equal to 6 C6 C9, is divisible by 3. > irem n, 3 0 (1.2.2.1.4) > n d iquo n, 3 n := 823 (1.2.2.1.5) true (1.2.2.1.6) > isprime n Hence the prime factorization of 12345 is 3 # 5 # 823. Check: > ifactor 12345 3 5 823 Suppose that the variable f evaluates to a vulgar fraction, e.g. f = (1.2.2.1.7) 7 . Write an 3 1 , except that the nearest 3 1 one can get in Maple output is an expression sequence such as 2, . Generate 3 this expression sequence as the integer quotient of the numerator and denominator followed by the integer remainder of the numerator and denominator divided by the denominator to give a proper fraction. Define a function called mixed that performs the conversion of a vulgar fraction to a mixed fraction represented as an expression sequence, and test it for a few different vulgar fractions. expression that converts f to mixed fraction form, e.g. 2 > Solution > fd 7 3 f := 7 3 Page 3 of 9 (1.2.2.2.1) 28-Nov-2015 The integer part is give by > iquo numer f , denom f 2 (1.2.2.2.2) 1 3 (1.2.2.2.3) The proper fraction part is give by irem numer f , denom f > denom f This is the obvious definition of the function mixed: > mixed d f/ iquo numer f , denom f , irem numer f , denom f denom f mixed := f/ iquo numer f , denom f , (1.2.2.2.4) irem numer f , denom f denom f This is a more succinct definition that applies an expression as a mapping: irem > mixed d f/ iquo, numer f , denom f denom f irem mixed := f/ iquo, numer f , denom f (1.2.2.2.5) denom f > mixed > mixed 7 3 2, 1 3 (1.2.2.2.6) 3, 1 2 (1.2.2.2.7) 7 2 Integer greatest common divisor and least common multiple: igcd, ilcm The greatest common divisor (or highest common factor) of two integers a and b, normally written in mathematics as gcd a, b , is the unique nonnegative integer d such that • d a and d b; • if e is any integer such that e a and e b, then e d. Although it is not immediately obvious from this definition, gcd a, b is the largest integer that divides both a and b unless a = b = 0. Integer gcd is implemented in Maple as the function igcd, which works like this: > igcd K15, 9 3 (1.3.1) Note that > igcd 0, 0 0 (1.3.2) Page 4 of 9 28-Nov-2015 It is clear from the definition that we can replace integers by their absolute values when computing gcds. Euclid’s Algorithm computes gcd a, b for two nonnegative integers a and b as follows: a) if b = 0 then gcd a, b = a; b) if b s 0, divide a by b to find q and r such that a = b q Cr and 0 % r ! b. Then calculate gcd b, r ; the result is equal to gcd a, b . This is a recursive algorithm or function definition because it refers back to itself: it computes a gcd in terms of a simpler gcd. We will consider recursive functions in more detail next week. But this particular algorithm just involves repeatedly computing remainders and returning the last non-zero remainder. For example, to compute gcd K15, 9 first replace both numbers by their absolute values to give gcd 15, 9 . Then compute > irem 15, 9 6 (1.3.3) > irem 9, 6 3 (1.3.4) 0 (1.3.5) > irem 6, 3 and return 3 as the result. A concept related to the greatest common divisor is the least common multiple or lcm of two integers, which is the smallest nonnegative integer that is a multiple of both of them. It is used primarily to put a sum of fractions over a common denominator, which should be the lcm of the two denominators; any other common denominator would be bigger than necessary. Integer lcm is implemented in Maple as the function ilcm and works like this: > ilcm K15, 9 45 (1.3.6) ab Note that, generally, lcm a, b = , e.g. gcd a, b ab > ilcm a, b = igcd a, b a =K15, b = 9 21 = 21 (1.3.7) The gcd and lcm of more than two integers are defined in the obvious way and the Maple functions igcd and ilcm accept any number of arguments. You can also use the Maple functions gcd and lcm with integer arguments, but they only accept two inputs. They are intended primarily for use with polynomials (see the Maple help for details) and any arguments after the second are not used for input. Quiz You are given the following chain of computations: > irem 49, 15 4 (1.3.1.1) > irem 15, 4 3 (1.3.1.2) > irem 4, 3 Page 5 of 9 (1.3.1.3) 28-Nov-2015 1 (1.3.1.3) > irem 3, 1 0 (1.3.1.4) What is the greatest common divisor of 49 and K15, i.e. the value of gcd 49,K15 ? Is it G4,G3,G1, 0? It is C1 because it is the last non-zero value in the remainder sequence, starting from the absolute values. > igcd 49,K15 1 (1.3.1.1.1) Exercise Use successive remainder computation (Euclid's Algorithm) to show that 1234 and 115 are coprime, i.e. that their gcd is 1, and check this directly using the Maple igcd function. > Solution > irem 1234, 115 84 (1.3.2.1.1) 31 (1.3.2.1.2) 22 (1.3.2.1.3) 9 (1.3.2.1.4) 4 (1.3.2.1.5) 1 (1.3.2.1.6) 1 (1.3.2.1.7) > irem 115, 84 > irem 84, 31 > irem 31, 22 > irem 22, 9 > irem 9, 4 > igcd 1234, 115 Synoptic exercises Attempt the following in the boxes provided or in a new Maple worksheet (or document). Question 1 Compute q = iquo a, b , r = irem a, b and verify that a = b q Cr, r ! b and r has the same sign as a for a few random pairs of integers a, b. The neatest way to do this is to use rand and a loop to run through a few tests. Review Chapter 7 as necessary. Take care to avoid the case b = 0 because division by 0 is an error. Your answers > My answers > R d rand K9 ..9 : to 3 do Page 6 of 9 28-Nov-2015 a, b d R , R ; if b = 0 then next end if; # this is one fairly elegant way to avoid division by 0 q, r d iquo a, b , irem a, b ; is a = b q Cr , is r ! b , is r a R 0 end do a, b := 5, 3 q, r := 1, 2 true, true, true a, b := K4, K8 q, r := 0, K4 true, true, true a, b := 2, K6 q, r := 0, 2 true, true, true (1.4.1.1.1) Question 2 Implement Euclid's Algorithm to compute the gcd of the values of the variables a and b using a loop as follows. Review Chapter 7 as necessary. Assign the values 1234 and 115 to the variables a and b. Then in a loop, while b is nonzero, replace a by b and replace b by the remainder in the division of a by b. Use parallel assignment or introduce temporary variables as necessary. When the loop terminates, display the value of a, which should be the gcd required. Modify your code so that it always returns a nonnegative gcd, and test it with some negative input values. Your answers > My answers > a, b d 1234, 115; while b s 0 do a, b d b, irem a, b end do: gcd = abs a a, b := 1234, 115 gcd = 1 (1.4.2.1.1) Question 3 This question is about prime factorization of an integer using a loop. Review Chapter 7 as necessary. Assign the integer 123 to the variable n and use ifactor to find the complete prime factorization of n. Display the result neatly. Assign 0 to the variable p. Write a loop that executes while p % n. In the loop, increment p to the next larger prime and if p divides n then print out an appropriate message. Do all of the above in the same execution group. In this way, you should be able to compute the prime factorization of 123. Copy the code you have written to a new execution group and modify it so that it Page 7 of 9 28-Nov-2015 constructs a sequence of the prime factors and displays this sequence at the end of the loop, without displaying any intermediate values. Copy the code you have written to a new execution group, replace 123 by 10!, and execute the code. Note that the code runs for a long time and does not find the powers of the prime factors, only the primes themselves. Copy the code you have written to a new execution group and modify it so that, instead of testing whether p divides n, while p divides n it collects each p that divides n into a sequence of factors and then replaces n by the quotient of n divided by p. Note that this code runs very much faster. Optionally, instead of collecting multiple copies of the same factor, compute its power and display the full prime factorization elegantly. Your answers > My answers > n d 123 : n = ifactor n ; pd0: while p % n do p d nextprime p ; if irem n, p = 0 then print p, `is a factor of`, n end if end do: 123 = 3 41 3, is a factor of, 123 41, is a factor of, 123 (1.4.3.1.1) > n d 123 : n = ifactor n ; p d 0 : S d NULL : while p % n do p d nextprime p ; if irem n, p = 0 then S d S, p end if end do: `The prime factors of`, n, `are`, S 123 = 3 41 The prime factors of, 123, are, 3, 41 > n d 10!: n = ifactor n ; p d 0 : S d NULL : while p % n do p d nextprime p ; if irem n, p = 0 then S d S, p end if end do: `The prime factors of`, n, `are`, S 3628800 = 2 8 3 4 5 2 7 The prime factors of, 3628800, are, 2, 3, 5, 7 (1.4.3.1.2) (1.4.3.1.3) > n d 10!: n = ifactor n ; p d 0 : S d NULL : while p % n do Page 8 of 9 28-Nov-2015 p d nextprime p ; while irem n, p = 0 do S d S, p; n d iquo n, p end do end do: `The prime factors are`, S 8 4 2 3628800 = 2 3 5 7 The prime factors are, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 5, 5, 7 (1.4.3.1.4) > n d 10!: n = ifactor n ; p d 0 : S d NULL : while p % n do p d nextprime p ; for pow from 0 while irem n, p = 0 do n d iquo n, p end do; if pow O 0 then S d S, p, pow end if; # don't include zero powers end do: `The prime factors are`, S; mul `` s1 s 2 , s in S 3628800 = 2 8 3 4 5 2 7 The prime factors are, 2, 8 , 3, 4 , 5, 2 , 7, 1 2 8 3 4 Page 9 of 9 5 2 7 (1.4.3.1.5) 28-Nov-2015