Fast Power and Modular Power

Asked at Meta · Coding Questions · Difficulty: Medium

Raising a number to a large power by multiplying it by itself `n` times is far too slow when `n` is in the billions. Implement exponentiation that needs only a logarithmic number of multiplications, first for a real base with a possibly negative exponent, then for integers modulo `p`.

Loading the problem…