Math Fundamentals18 sections · 814 units
Open in Course

Super Pow - Modular Exponentiation Rules

Apply modulo properties

Remember the modulo property: (a×b)modm=((amodm)×(bmodm))modm(a \times b) \mod m = ((a \mod m) \times (b \mod m)) \mod m. This applies to (a12)10×a3(a^{12})^{10} \times a^3 as well.

You compute a12mod1337a^{12} \mod 1337 first (recursively). Then raise that result to the 10th power, modulo 1337. Multiply by a3mod1337a^3 \mod 1337, and take modulo again.

Each exponentiation step uses the fast power function. The recursion depth is at most the number of digits in bb, which keeps the computation manageable.