#222EasyMath & Number Theory· Fast Modular Exponentiation
Modular Power Checksum
Input format
`base`, `exponent` and `modulus` as integers.
Output format
An integer in `[0, modulus - 1]`.
Examples
Input: base = 2, exponent = 10, modulus = 1000
Output: 24
2^10 = 1024 and 1024 mod 1000 = 24.
Input: base = 3, exponent = 200, modulus = 13
Output: 9
3^3 = 27 = 1 (mod 13), so 3^200 = 3^(3*66 + 2) = 3^2 = 9 (mod 13).
Input: base = 10, exponent = 5, modulus = 1
Output: 0
Every integer is 0 modulo 1.
Constraints
- 0 <= base <= 10^9
- 0 <= exponent <= 10^9
- 1 <= modulus <= 10^9
- Intermediate products can exceed 32 bits: widen them before taking the remainder.
- #math
- #modular-arithmetic
- #exponentiation
- #number-theory