Exponentiation by squaring (source code)

= Exponentiation by squaring
{wiki}

= Repeated squaring
{synonym}

Repeated squaring computes $a^n$ using $O(\log n)$ multiplications by reading the binary expansion of $n$. Performing every multiplication modulo $m$ computes $a^n\bmod m$ with the same number of modular multiplications.