Presentation is loading. Please wait.

Presentation is loading. Please wait.

Application: Algorithms Lecture 20 Section 3.8 Wed, Feb 21, 2007.

Similar presentations


Presentation on theme: "Application: Algorithms Lecture 20 Section 3.8 Wed, Feb 21, 2007."— Presentation transcript:

1 Application: Algorithms Lecture 20 Section 3.8 Wed, Feb 21, 2007

2 Greatest Common Divisors Let a and b be integers that are not both 0. The greatest common divisor of a and b, denoted gcd(a, b), is the unique positive integer d with the following properties: d | a and d | b. For every integer c, if c | a and c | b, then c | d.

3 Least Common Multiples Let a and b be nonzero integers. The least common multiple of a and b, denoted lcm(a, b), is the unique positive integer m with the following properties: a | m and b | m. For every integer c, if a | c and b | c, then m | c.

4 The High-school gcd Algorithm The high-school method is very inefficient. Factor each number into standard form. For each prime that appears in both factorizations, use it as a factor of the gcd along with the smaller of the two exponents.

5 Example Find the gcd of 81900 and 54810. We factor them as 81900 = 2 2  3 2  5 2  7 1  13 1  29 0 54810 = 2 1  3 3  5 1  7 1  13 0  29 1 Therefore, the gcd is 2  3 2  5  7 = 630

6 The Euclidean Algorithm Factoring is inefficient; therefore, this algorithm is inefficient. The run time of this algorithm is O(  10 d ), where d is the number of digits in the number. Euclid had a much better idea. The run time of the Euclidean Algorithm is O(d), where d is the number of digits in the number.

7 The Euclidean Algorithm Input: A, B (positive integers, not both 0)

8 The Euclidean Algorithm Algorithm body: Output: b a := A b := B if b = 0 then swap a and b r := a mod b while r > 0 a := b b := r r := a mod b end while

9 Example Apply the Euclidean Algorithm to A = 81900 and B = 54810. IterationABabr 08190054810819005481027090 1 2

10 Example Apply the Euclidean Algorithm to A = 81900 and B = 54810. IterationABabr 08190054810819005481027090 15481027090630 2

11 Example Apply the Euclidean Algorithm to A = 81900 and B = 54810. IterationABabr 08190054810819005481027090 15481027090630 2270906300

12 Example Find the gcd of 1098011 and 1033133.

13 Least Common Multiples What is the efficient way to find lcm’s? What is the lcm of 1098011 and 1033133?

14 Proof of the Euclidean Algorithm Theorem: The Euclidean Algorithm terminates for all legitimate inputs A and B. Proof: We may assume that B > 0. After the first iteration of the while loop, 0  b < B since b is the remainder of A divided by B.

15 Proof of the Euclidean Algorithm Each iteration produces a nonnegative remainder that is smaller than the previous remainder. This cannot happen more than B times before the remainder is 0.

16 Proof of the Euclidean Algorithm Lemma 1: If b > 0, then gcd(b, 0) = b. Proof: b | b and b | 0. For all integers c, if c | 0 and c | b, then c | b. Therefore, b = gcd(b, 0).

17 Proof of the Euclidean Algorithm Lemma 2: If a and b are integers, with b  0, and q and r are integers such that a = qb + r then gcd(a, b) = gcd(b, r). Proof: Let d = gcd(b, r). Then d | b and d | r and any integer that divides b and r must also divide d.

18 Proof of the Euclidean Algorithm We must show that d | a and d | b and any integer that divides a and b must also divide d. We already know that d | b. Since a = qb + r, it follows that d | a. Let c be an integer such that c | a and c | b. Since r = a – qb, it follows that c | r and so c | d. Therefore, d = gcd(a, b).

19 Proof of the Euclidean Algorithm Theorem: The Euclidean Algorithm produces the gcd of A and B. Proof: After the final iteration of the while loop, r = 0. By Lemma 1, the output b is the gcd of b and r, i.e., b = gcd(b, 0). By Lemma 2, that is equal to the gcd of “a” and “b” in the final iteration.

20 Proof of the Euclidean Algorithm But “a” and “b” on the last iteration were “b” and “r” on the previous iteration. Therefore, gcd(a, b) on the last iteration equals gcd(b, r) on the previous iteration, which equals gcd(a, b) on the previous iteration, and so on. Following this argument all the back to the first iteration, we see that the output is gcd(A, B).

21 Proof of the Euclidean Algorithm In the next chapter, we will study mathematical induction. At that point, we will be able to make this argument more rigorous.


Download ppt "Application: Algorithms Lecture 20 Section 3.8 Wed, Feb 21, 2007."

Similar presentations


Ads by Google