Worked examples, one step per line

Teaching and explanation

Euclid's algorithm on 355 and 113. Three squares of side 113 leave a strip, enlarged beside it; seven squares of side 16 leave a sliver, enlarged again; sixteen unit squares fill it. Moss marks the unit, the common measure of both numbers.

Pick an example. The page runs that lab's own code on one input and writes out what it returns, a step at a time. The page does not grade a reader and it does not claim a learning outcome.

Euclid's algorithm

From the mathematics desk: The continued fraction of a rational

Each line is the identity dividend equals quotient times divisor plus remainder, with the remainder strictly smaller than the divisor. The last non-zero remainder is the gcd.

Three bars, one per division line for 355 and 113, each cut into copies of the divisor with the remainder shaded.
The default pair, 355 and 113, in three lines. Each bar is one dividend, scaled to the same width and cut into copies of its divisor: 355 is 3 times 113 plus 16, 113 is 7 times 16 plus 1, and 16 is 16 times 1 with nothing left. The shaded ends are the remainders.

Both are integers from 1 to 1000000000. The default pair is 355 and 113, the same rational as the continued-fraction page.

The result appears here when the page script runs.

How it is computed

Method

Start with the two integers. While the divisor is not zero, the quotient is the integer division and the remainder is dividend minus quotient times divisor. The next dividend is the old divisor and the next divisor is the remainder. Each line is checked before it is shown. The reduced ratio divides by the gcd.

The other examples load the script of the lab they come from and run it on one input. Each value in a line is read from what that run returns: the stacked ratios and their cents for the comma, the priors, products and normaliser for the update, the stored finishes for the tour, and the certificate and its reread for the prime. Where the same run gives a value two ways, such as the comma as a stack and as a quotient, the two are compared first, and a mismatch replaces the lines with a message.

Assumptions

Euclid runs here on the two integers in the form, and the continued-fraction page uses the same recurrence. The other inputs are fixed by their labs: the sound lab's twelve fifths, the first test of the decisions lab's screening example, the routing lab's four stops on one road, and the prime 113 from the mathematics desk.

Limits

No score is given to a reader. A worked line is not a credential.

Reproducibility

The example and Euclid's inputs are kept in the address bar, so the same link reruns the same calculation.