block by bmershon 496aa57731fdc6b83b0d7ea8d75cda62

Greatest Common Measure

Full Screen

While working through Stepanov and Rose’s From Mathematics to Generic Programming, it struck me that the pencil and paper exercises I performed in order to understand the algorithms presented in the text would pose an interesting challenge if I wanted to recreate them… programatically. As the difficulty of the text and its examples ramps up, I wanted to begin with some milder exercises which might lend themselves most easily to visualization.

This example is an illustration a method for finding the greatest common “measure” for two line segments for which a common measure exists (also known as the greatest common divisor). For example, gcd(14, 21) = 7. This is Pythagoras’s world: numbers are expected to come in discrete, whole units. And in Euclid’s world, those numbers are line segments with nonzero length. Although this greatest common measure algorithm may be humble in what it accomplishes, this example demonstrates an approach to visualizing the work done by an algorithm which even includes recursion.

A history of work performed is created by emitting actions along with pieces of useful information throughout the algorithm’s execution. The history (an array of actions) is played forward in time by rendering changes to a visual representation of the state of the algorithm. In this particular case, the state of the algorithm was that of two line segments which are lengthened, shortened, and “swapped” during the course of the algorithm. Alternative visualizations might benefit from choosing spatial separation of the steps of an algorithm over temporal separation (as this example uses).

See pages 48-49 of From Mathematics to Generic Programming.

See also a wiki of solutions (in progress).

index.html

gcd.js