Notes, code, and other related materials generated while reading through Donald E. Knuth's "The Art of Computer Programming"

notes.org 815B

123456789101112131415161718192021222324252627282930313233
  1. * Euclid's algorithm (Page 2-9)
  2. ** Calculates the greatest common divisor of two numbers.
  3. ** Definition
  4. *** E0. [Ensure m >= n.] If m < n, exchange m <-> n.
  5. *** E1. [Find remainder.] Divide m by n and let r be the remainder.
  6. *** E2. [Is it zero?] If r = 0, the algorithm terminates; n is the answer
  7. *** E3. [Reduce.] Set m <- n, n <- r, and go back to step E1.
  8. ** Use
  9. *** Own example: m = 235; n = 95;
  10. **** 235 % 95 = 45
  11. **** r = 45
  12. **** m = 95; n = 45;
  13. **** 95 % 45 = 5
  14. **** r = 5
  15. **** m = 45; n = 5;
  16. **** 45 % 5 = 0
  17. **** r = 0
  18. **** answer = n = 5
  19. *** Knuth example: m = 119; n = 544
  20. **** m = 544; n = 119;
  21. **** 544 % 119 = 68
  22. **** r = 68
  23. **** m = 119; n = 68;
  24. **** 119 % 68 = 51
  25. **** r = 51
  26. **** m = 68; n = 51;
  27. **** 68 % 51 = 17
  28. **** r = 17
  29. **** m = 51; n = 17;
  30. **** 51 % 17 = 0
  31. **** r = 0
  32. **** answer = n = 17