1 / 30

The Greatest Common Divisor

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 .

shayna
Download Presentation

The Greatest Common Divisor

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Greatest Common Divisor Programming Fundamentals 2 Feliks Kluźniak The Greatest Common Divisor

  2. Executive summary: We discuss a very old algorithm and introduce the notion of a correctness proof. The Greatest Common Divisor

  3. Consider two integers, A > 0 and B > 0. The Greatest Common Divisor

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. One method is due to Euclid (ca. 300 BC): The Greatest Common Divisor

  13. One method is due to Euclid (ca. 300 BC): If A = B, then we are done. Why? The Greatest Common Divisor

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. TO BE CONTINUED The Greatest Common Divisor

More Related