Power Mod Calculator

Enter a base, exponent, and modulus to compute ab mod m using fast modular exponentiation (square-and-multiply), with the algorithm's steps shown.

Quick Facts

Formula
ab mod m
The remainder when a raised to the power b is divided by m; always between 0 and m-1.
Fast method
Square-and-multiply
Computes the result in about 2·log2(b) modular multiplications instead of b-1.
Key identity
a^b mod m = (a mod m)^b mod m
Only the base's remainder modulo m matters, so it can be reduced first.

Your Results

Calculated
Result (a^b mod m)
-
Final remainder, between 0 and m-1
Normalized base (a mod m)
-
a reduced modulo m before exponentiating
Exponent in binary
-
Bit pattern used by square-and-multiply
Modular multiplications used
-
Fast method vs. naive repeated multiplication

Ready

Enter a base, exponent, and modulus, then press Calculate.

How Power Mod (Modular Exponentiation) Works

Power mod computes ab mod m — raise a base a to an exponent b, then keep only the remainder after dividing by a modulus m. The result is always a whole number between 0 and m-1. Computing this directly by first raising a to the power b is impractical for anything but tiny exponents, since ab grows explosively (2313 already has 18 digits). This calculator instead uses modular exponentiation via the square-and-multiply algorithm, which reduces modulo m after every multiplication so the numbers involved never grow larger than roughly m².

Formula and method: square-and-multiply

The fast algorithm writes the exponent b in binary and processes it one bit at a time, from least significant to most significant. Start with result = 1 and reduce the base modulo m. For each bit: if the bit is 1, multiply the running result by the current base (mod m); then always square the current base (mod m) and move to the next bit. This performs about 2·log2(b) modular multiplications total — for b = 13 (binary 1101) that is only 7 multiplications instead of the 12 a naive "multiply a by itself b-1 times" approach would need, and the gap widens dramatically as b grows.

Worked example

To compute 2313 mod 17: first reduce the base, 23 mod 17 = 6. Write 13 in binary as 1101. Processing the bits gives intermediate results of 6, then 7, then 10 — so 2313 mod 17 = 10. You can verify this with a much smaller check: 23 mod 5 = 8 mod 5 = 3, since 8 = 1×5 + 3.

Common sources of error

  • Computing the full power first: for larger exponents this produces enormous intermediate numbers and can silently overflow fixed-precision arithmetic in some tools — always reduce modulo m at every step, not just at the end.
  • Negative exponents: a-b mod m requires the modular inverse of a (via the extended Euclidean algorithm), which only exists when a and m are coprime; this calculator accepts non-negative integer exponents only.
  • Modulus of 0 or a non-integer: the modulus must be a positive integer (m ≥ 1); "mod 0" is undefined, and a modulus of 1 always yields a result of 0.

Applications

Modular exponentiation is the workhorse operation behind RSA and Diffie-Hellman public-key cryptography, digital signatures, and the Miller-Rabin and Fermat primality tests. It also appears in hash functions, checksums, and pseudorandom number generators, and is a standard building block in competitive programming and number theory coursework whenever a result must stay within a fixed-size range.

Frequently Asked Questions

What does "a^b mod m" actually mean?
It means: raise a to the power b, divide by m, and keep only the remainder. The result is always a whole number between 0 and m-1. For example, 23^13 mod 17 = 10.
Why not just compute a^b first and then take the modulus?
For even modest exponents, a^b becomes astronomically large (23^13 already has 18 digits) and is slow or memory-heavy to compute directly. Modular exponentiation avoids this by reducing modulo m after every multiplication, so the numbers involved never grow larger than m².
What is the square-and-multiply (fast exponentiation) algorithm?
It writes the exponent in binary and processes it one bit at a time: square the running base every step, and multiply it into the result whenever the current bit is 1. This computes a^b mod m in about 2·log2(b) multiplications instead of b-1, which is why it stays fast even for huge exponents.
Where is modular exponentiation used in practice?
It is the core operation behind RSA and Diffie-Hellman cryptography, digital signatures, primality tests like Fermat's and Miller-Rabin, hashing, and pseudorandom number generators — anywhere a large power needs to be reduced within a fixed-size number range.