The continued fraction of a rational

Mathematics

Every fraction is a circle resting on the number line, and neighbouring fractions touch. On the left, the circles that touch 3 shrink toward it; the lapis one is 22/7. On the right, 49 times larger, the circles that touch 22/7: the lapis point is 355/113.

The desk reduces a positive rational, writes the continued-fraction terms, and checks that the last convergent is the reduced form. A neighbouring convergent pair has determinant 1 or -1. The Bezout coefficients are the same run. No decimal is stored.

Euclid's squares for 355 over 113: three squares of side 113, seven of side 16, and sixteen of side 1.
The default rational, 355 over 113, cut into squares: three of side 113, then seven of side 16, then sixteen of side 1 along the last strip. The counts 3, 7, and 16 are the terms the desk writes for the default. The figure is drawn from the default input.

Both are integers from 0 to 1000000000. The denominator must be positive. The default is 355 over 113.

The result appears here when the page script runs.

A primality certificate

A certificate is a short record that proves an integer prime or composite, and anyone can check it with modular powers. For a prime n, a Pratt certificate names a witness a with a to the power n - 1 equal to 1 mod n, and a to the power (n - 1)/q different from 1 mod n for every prime q dividing n - 1. Each q gets the same treatment, down to 2. For a composite, the page gives its smallest factor and its smallest Miller-Rabin witness. A second reading takes only the certificate and recomputes every power and product.

An integer from 0 to 1000000000. A larger integer gets a reason in place of a certificate. The default is 113, the denominator above.

The certificate appears here when the page script runs.

How it is computed

Method

Euclid writes each quotient. Convergents use the recurrence h equals a times the previous h plus the one before that, and the same for k. The determinant of neighbours is checked. Extended Euclid returns integers x and y with x times the reduced numerator plus y times the reduced denominator equal to the gcd.

Certificates

Trial division decides prime or composite and factors each n - 1. For a prime, the witness is the smallest a that passes every condition, so it is a primitive root mod n. Each prime q dividing n - 1 is certified once, the first time it is needed, and 2 is the base case. For a composite, write n - 1 as 2 to the s times an odd d, take a to the d, and square it s times to end at a to the n - 1. For a prime, that list starts at 1 or reaches n - 1 before the last square. The Miller-Rabin witness is the smallest base a from 2 up whose list does neither. The second reading checks that the listed factors multiply back to n - 1, that every q has its own certificate, and that every listed power matches a fresh square-and-multiply.

Assumptions

The input is one rational. Terms stay under 64. Integers stay inside the safe range. A zero denominator is refused. The integer to certify stops at 1000000000, where trial division of n - 1 stays quick in a browser.

Limits

The desk does not approximate pi: 355 over 113 is an integer pair, not a measurement of a circle.

Reproducibility

The inputs are kept in the address bar, so the same link reruns the same calculation.