Modular multiplicative inverse

Find x so that dividing a × x by m leaves remainder 1. Use a modulus m of at least 2.

If the GCD is 1, a × x differs from 1 by a multiple of m. This makes x an inverse.

Choose the residue of x between 0 and m − 1. Multiplication by a still gives remainder 1.

The division count matches Euclid’s GCD algorithm. For positive inputs it grows logarithmically. Three rows suffice.