Euclid's+algorithm

Euclid's algorithms finds the GCD (Greatest Common Divisor) of two integers. Let's call them M and N and WOLOG N > M.

The GCD, call it g, divides both numbers, so g*j = M and g*k = N for some integers j and k. N - M = g*k - g*j = g*(k-j) In words, g must also divide the difference between the two numbers. In fact, we can continue subtracting M from the remainder until the remainder is less than M. Division is a clever trick for repeated subtraction, and in this case we can cut to the chase and take the remainder: M2 = N % M. Rik temporarily forgot that fact of elementary arithmetic while explaining on the board. g must divide M2. Now we can swap roles, and compute M3 = M % M2 and M4 = M2 % M3 etc. until it comes out even. If one of the numbers evenly divides the other, it IS the GCD g.

Example: Find GCD(1183, 897) 1183 % 897 = 1183 - 897 = 286 897 = 286*3 + 39 so 897 % 286 = 39 286 = 39*7 + 13 so 286 % 39 = 13 39 % 13 = 0 so 13 is the GCD

You can read further details in the book or explore it yourself.