Euclid’s algorithm (dart 027) gives the gcd of a and b. The extended version also returns the two integers s, t with s·a + t·b = gcd — and when the gcd is 1, that s is the modular inverse of a, the workhorse behind RSA, CRT, and every “divide mod n.” It naturally returns three values at once.
Run Euclid’s remainder ladder, but alongside each remainder carry a pair of coefficients (s, t) telling how to rebuild it from the original a and b. When the remainder hits 0, the previous row holds gcd = s·a + t·b. If gcd = 1, s mod b is a−1. Enter a and a modulus. live demo
“Bézout’s identity” for integers — Bézout proved the integer case proved the polynomial case (1779); the integer statement was Bachet’s (1624), and the algorithm is far older. cited
Euclid gave the gcd loop; the explicit coefficients are Aryabhata’s. kuttaka, c. 499 CE
The algorithm wants to hand back three values — (g, s, t) — and I-13 returns one. Threaded to a single value (the inverse), it runs: