Lecture 9: Arithmetics II 1 Greatest Common Divisor
Total Page:16
File Type:pdf, Size:1020Kb
DD2458, Problem Solving and Programming Under Pressure Lecture 9: Arithmetics II Date: 2008-11-10 Scribe(s): Marcus Forsell Stahre and David Schlyter Lecturer: Douglas Wikström This lecture is a continuation of the previous one, and covers modular arithmetic and topics from a branch of number theory known as elementary number theory. Also, some abstract algebra will be discussed. 1 Greatest Common Divisor Definition 1.1 If an integer d divides another integer n with no remainder, d is said to be a divisor of n. That is, there exists an integer a such that a · d = n. The notation for this is d | n. Definition 1.2 A common divisor of two non-zero integers m and n is a positive integer d, such that d | m and d | n. Definition 1.3 The Greatest Common Divisor (GCD) of two positive integers m and n is a common divisor d such that every other common divisor d0 | d. The notation for this is GCD(m, n) = d. That is, the GCD of two numbers is the greatest number that is a divisor of both of them. To get an intuition of what GCD is, let’s have a look at this example. Example Calculate GCD(9, 6). Say we have 9 black and 6 white blocks. We want to put the blocks in boxes, but every box has to be the same size, and can only hold blocks of the same color. Also, all the boxes must be full and as large as possible . Let’s for example say we choose a box of size 2: As we can see, the last box of black bricks is not full. It’s easy to see that it wouldn’t be, since 2 isn’t a divisor of 9. We know that 3 is a divisor of both 9 and 6, so we try this size of boxes. 1 2 DD2458 – Popup HT 2008 There is no larger number that is a divisor of both 9 and 6, so there can’t be any larger size of boxes that can be completely filled. We therefore come to the conclusion that GCD(9, 6) must be 3. From the third definition above, it’s not very hard to see that the Greatest Common Divisor is equivalent to the greatest common factor (not to be confused with prime factors!), since every other common divisor is smaller than GCD(m, n). The third definition also gives us an inefficient but simpler way than the above to find the Greatest Common Divisor of two numbers. First, find the prime factorizations of m and n. Take all the common prime factors and calculate the product of them. Let’s call this product d. Since both m and n is divisible by this d, and since every common divisor is a divisor of d, we get GCD(m, n) = d. Note that we know that every common divisor is a divisor of d since every common divisor can be written as a product of common prime factors. Example Calculate GCD(60, 42) 60 = 2 · 2 · 3 · 5 42 = 2 · 3 · 7 GCD(60, 42) = 2 · 3 = 6 Since finding the prime factorization of a number in general is hard, this is obviously not a very good way to calculate GCD, especially when m and n grows larger. There are much more efficient ways to calculate it, and we will talk about two of them in a moment, but first let’s have a look at a few useful properties of GCD. 1.1 Properties of GCD m and n are positive integers, and k is any integer. GCD(m, n) = GCD(n, m) (1) GCD(m, n) = GCD(m + k · n, n) (2) GCD(m, n) = GCD(m mod n, n) (3) GCD(m, n) = 2 · GCD(m/2, n/2) if m and n are even. (4) GCD(m, n) = GCD(m/2, n) if m is even and n is odd. (5) It is not hard too see that (1) is true, but the others might need some explanation. Property (2) is the basis for (3), so let’s start with (2). If x is a common divisor of m and n, then x | m and x | n. Therefore x | k · n, and thus x | (m + k · n). Arithmetics II 3 If x is a common divisor of (m + k · n) and n, then x | m + k · n and x | n. Therefore x | k · n, and thus x | (m + k · n) − k · n ⇔ x | m. From this we conclude that the set of common divisors to m and n is equal to the set of common divisors to m + k · n and n. This means that GCD(m, n) = GCD(m + k · n, n) We’ll now use this to show (3). We are going to use the fact that m mod n is defined as an integer 0 ≤ r < n such that for some integer a, m = a · n + r. From this we get r = m − a · n, and from (2) we know that GCD(m − a · n, n) = GCD(m, n). Since m and n both being even is equivalent to m and n having a common di- visor 2, we can move this factor outside of the GCD function, and still get the same result (4). By similar reasoning, if one of m and n are even and not the other one, 2 is a non-common divisor, and the result is not affected if it’s removed (5). 1.2 Euclidean Algorithm The Euclidean Algorithm makes use of property (3) from above. The idea is to reduce m and n repeatedly until one of them is zero. The original version of the algorithm actually used property (2) from above, and looks like this: Algorithm 1: Euclidean algorithm for finding the GCD Input: Two integers, m and n Output: The GCD of m and n EuclideanOld(m,n) (1) if m = 0 (2) return n (3) while n 6= 0 (4) if n > m (5) n ← n − m (6) else (7) m ← m − n (8) return m It uses the fact that GCD(m, n) = GCD(m − n, n), and simply repeats the op- eration until n hits zero. A more efficient way of doing this is of course to use (3). 4 DD2458 – Popup HT 2008 Algorithm 2: A better version of the Euclidean algorithm Input: Two integers, m and n Output: The GCD of m and n EuclideanIterative(m,n) (1) while n 6= 0 (2) t ← n (3) n ← m mod n (4) m ← t (5) return m This is very much the same idea as the previous one. GCD(m, n) = GCD(n, m mod n), repeat until n hits zero. Since modulo basically is repeated subtraction, this is very much the same algorithm, but several subtractions are “done at once”. The recursive version of the algorithm looks like this: Algorithm 3: A recursive version of Algorithm 2 Input: Two integers, m and n Output: The GCD of m and n EuclideanRecursive(m,n) (1) if n = 0 (2) return m (3) else (4) return EuclideanRecursive(n, m mod n) 1.3 Binary GCD Algorithm When using a computer to calculate GCD, the Binary GCD Algorithm (also known as Stein’s algorithm) might be preferable, since it makes good use of the binary representation of numbers that a computer uses. The idea is to use property (4) and (5) in order to step by step decrease the number of bits in the numbers. Arithmetics II 5 Algorithm 4: Stein’s algorithm for binary GCD calculation Input: Two integers, m and n Output: The GCD of m and n BinaryGCD(m,n) (1) if m = 0 or n = 0 (2) return 0 (3) s ← 0 (4) while m and n are even (5) m ← m/2 (6) n ← n/2 (7) s ← s + 1 (8) while n is even (9) n ← n/2 (10) while m 6= 0 (11) while m is even (12) m ← m/2 (13) if m < n (14) Swap(m,n) (15) m ← m − n (16) m ← m/2 (17) return 2s · n First, all the common factors 2 are removed. Since the numbers are stored in binary on a computer, this can be done with shift-operations, which are very fast. If n still has any factors 2, they can be discarded, as they’re not common factors. The main-loop of the algorithm is then entered. At this point there are no common factors 2 that are left, since they’ve all been removed. Therefore, m can be divided by 2 until it is odd. If it then is smaller than n, they are swapped. This is done so that the next operation never gives a negative result. When row 15 is about to be executed, both m and n are guaranteed to be odd numbers. And since the difference of two odd numbers always is even, row 15 always makes m into an even number. It can therefore be divided by two once again. The loop with shifting right and making even continues until m is zero, and then n is returned, but not before multiplying with the common factors 2 that were removed on rows 4 to 7. 2 Bézout’s identity Bézout’s identity states that for every pair of non-zero integers a and b, there exists integers x and y such that ax + by = GCD(a, b). These can be found using the Extended Euclidean Algorithm. Note though that there is an infinite number of x and y that satisfies this equation.