Fast Power Modulo
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Write powMod(base, exp, mod): return base to the power exp, modulo mod. All three are BigInts and so is the result: base and exp are at least 0n, exp can be as large as 10^18, and mod is at least 1n. Anything modulo 1n is 0n, and anything to the power 0n is 1n before taking the modulo. For example, powMod(2n, 10n, 1000n) is 24n, since 2 to the power 10 is 1,024. Multiplying exp times is far too slow, and base ** exp gets too big to store: square the base and halve the exponent instead, taking the modulo at every step.
Your code must pass
- Works for small numbers
- Handles an exponent of 0 and a mod of 1
- Handles a base bigger than the mod
- Is fast enough for an exponent of 10^18