Modular Arithmetic

Storing a few results here for future use


(a + b) % m = (a % m + b % m) % m is fairly obvious to derive.

(a * b) % m = ((a % m) * (b % m)) % m can be derived by writing out the multiplication as repeated addition.

Fermat’s Little Theorem states that if $p$ is a prime, then for any $a\in\mathbb{Z},$

\[a^{p}\equiv a\text{ mod }p.\]

(a / b) % m = (a % m * b^-1 % m) % m can be derived from Fermat’s Little Theorem. The multiplicative inverse of $b$ modulo $m$ is $b^{-1}$ such that $b\cdot b^{-1}\equiv 1\text{ mod }m.$