310 likes | 533 Views
The Greatest Common Divisor. Programming Fundamentals 2 Feliks Klu ź niak. Executive summary: We discuss a very old algorithm and introduce the notion of a correctness proof. . Consider two integers, A > 0 and B > 0 . Consider two integers, A > 0 and B > 0 .
E N D
The Greatest Common Divisor Programming Fundamentals 2 Feliks Kluźniak The Greatest Common Divisor
Executive summary: We discuss a very old algorithm and introduce the notion of a correctness proof. The Greatest Common Divisor
Consider two integers, A > 0 and B > 0. The Greatest Common Divisor
Consider two integers, A > 0 and B > 0. An integer d is a common divisor of A and Biff there exist integers m > 0 and n > 0 such that d * m = A and d * n = B. NOTE: * denotes multiplication in most programming languages, so we might as well start using it. The Greatest Common Divisor
Consider two integers, A > 0 and B > 0. An integer d is a common divisor of A and Biff there exist integers m > 0 and n > 0 such that d * m = A and d * n = B. NOTE: * denotes multiplication in most programming languages, so we might as well start using it. NOTE: “iff” is a commonly-used abbreviation for “if and only if”. The Greatest Common Divisor
Consider two integers, A > 0 and B > 0. An integer d is a common divisor of A and B iff there exist integers m > 0 and n > 0 such that d * m = A and d * n = B. Example: 3 is a common divisor of 36 and 27. The Greatest Common Divisor
Consider two integers, A > 0 and B > 0. An integer d is a common divisor of A and B iff there exist integers m > 0 and n > 0 such that d * m = A and d * n = B. Does every such pair of integers have a common divisor? The Greatest Common Divisor
Consider two integers, A > 0 and B > 0. An integer d is a common divisor of A and Biff there exist integers m > 0 and n > 0 such that d * m = A and d * n = B. If d is the greatest integer with this property, then it is the greatest common divisor (GCD) of A and B. The Greatest Common Divisor
Consider two integers, A > 0 and B > 0. An integer d is a common divisor of A and Biff there exist integers m > 0 and n > 0 such that d * m = A and d * n = B. If d is the greatest integer with this property, then it is the greatest common divisor (GCD) of A and B. Example: 9 is the greatest common divisor of 36 and 27. The Greatest Common Divisor
Consider two integers, A > 0 and B > 0. An integer d is a common divisor of A and Biff there exist integers m > 0 and n > 0 such that d * m = A and d * n = B. If d is the greatest integer with this property, then it is the greatest common divisor (GCD) of A and B. Does such a pair of integers always have a greatest common divisor? The Greatest Common Divisor
Consider two integers, A > 0 and B > 0. An integer d is a common divisor of A and B iff there exist integers m > 0 and n > 0 such that d * m = A and d * n = B. If d is the greatest integer with this property, then it is the greatest common divisor (GCD) of A and B. How do we find the GCD of two positive, nonzero integers? The Greatest Common Divisor
One method is due to Euclid (ca. 300 BC): The Greatest Common Divisor
One method is due to Euclid (ca. 300 BC): If A = B, then we are done. Why? The Greatest Common Divisor
One method is due to Euclid (ca. 300 BC): If A = B, then we are done. Otherwise: if A > B then replace A with A – B, otherwise replace B with B – A. The Greatest Common Divisor
One method is due to Euclid (ca. 300 BC): If A = B, then we are done. Otherwise: if A > B then replace A with A – B, otherwise replace B with B – A. 3. Repeat from step 1. The Greatest Common Divisor
One method is due to Euclid (ca. 300 BC): If A = B, then we are done. Otherwise: if A > B then replace A with A – B, otherwise replace B with B – A. Repeat from step 1. Example: 36 and 27 The Greatest Common Divisor
One method is due to Euclid (ca. 300 BC): If A = B, then we are done. Otherwise: if A > B then replace A with A – B, otherwise replace B with B – A. Repeat from step 1. Example: 36 and 27 The Greatest Common Divisor
One method is due to Euclid (ca. 300 BC): • If A = B, then we are done. • Otherwise: • if A > B then replace A with A – B, • otherwise replace B with B – A. • Repeat from step 1. • Example: 36 and 27 • 9 and 27 The Greatest Common Divisor
One method is due to Euclid (ca. 300 BC): If A = B, then we are done. Otherwise: if A > B then replace A with A – B, otherwise replace B with B – A. Repeat from step 1. Example: 36 and 27 9 and 27 The Greatest Common Divisor
One method is due to Euclid (ca. 300 BC): If A = B, then we are done. Otherwise: if A > B then replace A with A – B, otherwise replace B with B – A. Repeat from step 1. Example: 36 and 27 9 and 27 9 and 18 The Greatest Common Divisor
One method is due to Euclid (ca. 300 BC): If A = B, then we are done. Otherwise: if A > B then replace A with A – B, otherwise replace B with B – A. Repeat from step 1. Example: 36 and 27 9 and 27 9 and 18 9 and 9 The Greatest Common Divisor
If A = B, then we are done. Otherwise: if A > B then replace A with A – B, otherwise replace B with B – A. Repeat from step 1. This is an example of an algorithm. (Al Khwarizmi, Persian, 825 AD). The Greatest Common Divisor
If A = B, then we are done. Otherwise: if A > B then replace A with A – B, otherwise replace B with B – A. Repeat from step 1. This is an example of an algorithm. Roughly, an algorithm is a precise method of achieving some end. The Greatest Common Divisor
If A = B, then we are done. Otherwise: if A > B then replace A with A – B, otherwise replace B with B – A. Repeat from step 1. This is an example of an algorithm. Roughly, an algorithm is a precise method of achieving some end: in this example, of finding the greatest common divisor of two integers greater than zero. The Greatest Common Divisor
If A = B, then we are done. Otherwise: if A > B then replace A with A – B, otherwise replace B with B – A. Repeat from step 1. The same algorithm can be described in many different ways. For example, one can use old-fashioned flowcharts: The Greatest Common Divisor
If A = B, then we are done. Otherwise: if A > B then replace A with A – B, otherwise replace B with B – A. Repeat from step 1. Y A = B ? STOP N N Y A flowchart A > B ? B <– B – A A <– A – B The Greatest Common Divisor
If A = B, then we are done. Otherwise: if A > B then replace A with A – B, otherwise replace B with B – A. Repeat from step 1. The same algorithm can be described in many different ways. A more modern way is to use so-called pseudocode: while A != B do if A > B then A := A – B else B := B – A fi od The Greatest Common Divisor
If A = B, then we are done. Otherwise: if A > B then replace A with A – B, otherwise replace B with B – A. Repeat from step 1. The same algorithm can be described in many different ways. A more modern way is to use so-called pseudocode: while A != B do if A > B then A := A – B else B := B – A fi od This is supposed to represent a fragment of a program written in some “generic” programming notation (a.k.a. “language”). In fact, this is exactly the notation that we will use in TL. The Greatest Common Divisor
pseudocode • while A != B do • if A > B then A := A – B • else B := B – A • fi • od Y A = B ? STOP N N Y flowchart A > B ? B <– B – A A <– A – B The Greatest Common Divisor
TO BE CONTINUED The Greatest Common Divisor