Modular Exponentiation Calculator

Calculate a large integer power modulo another integer efficiently with the fast square-and-multiply algorithm.

Calculate a power modulo m
Enter an integer base, a nonnegative exponent, and a modulus greater than one.

About modular exponentiation

Modular exponentiation calculates the remainder when an integer base is raised to a nonnegative integer exponent and divided by a positive modulus. The expression a^b mod m asks for the unique remainder from 0 through m - 1 that is congruent to a^b. A direct calculation can become impractical very quickly because powers grow to enormous sizes, even when the final remainder is small. This calculator avoids constructing that enormous intermediate integer. The efficient method is called binary exponentiation, repeated squaring, or square and multiply. Write the exponent in binary and process its bits. The base is first reduced modulo m. At each step, the current factor is squared modulo m. When the corresponding exponent bit is one, that factor is multiplied into the result, again reducing modulo m immediately. Because every intermediate value is reduced, it remains smaller than the square of the modulus. This algorithm needs only a number of steps proportional to the logarithm of the exponent. Computing a power with exponent one billion therefore requires only about thirty squaring stages rather than one billion repeated multiplications. The displayed operation count separates squarings from the selected multiplications associated with one bits in the exponent. An exponent of zero correctly returns 1 mod m. Modular arithmetic treats numbers with the same remainder as equivalent. For example, 17 and 5 are congruent modulo 12 because both leave remainder 5. A negative base is also supported: it is first normalized to a nonnegative residue. The exponent must be nonnegative in this calculator. Negative modular exponents require finding a modular multiplicative inverse, which exists only when the base and modulus are coprime. Modular powers are fundamental to number theory and modern cryptography. RSA encryption and signatures raise messages to large powers modulo a composite number. Diffie-Hellman key exchange and many discrete-logarithm systems use powers modulo a prime. The same operation also supports primality tests, pseudorandom generators, checksums, cyclic patterns, and programming contest problems. All three fields are processed as exact arbitrary-size integers rather than floating-point numbers. That means values beyond JavaScript's usual safe integer limit retain every digit. The modulus must be greater than one, and no commas or decimal points should be entered. For a useful manual check, reduce the base first and verify small exponents through repeated multiplication, taking the remainder after every step.

Modular exponentiation examples

These examples range from a small arithmetic check to common number-theory patterns.

ExpressionResultContext
3^4 mod 5181 leaves remainder 1 when divided by 5
7^10 mod 134A compact cryptographic-style example
123^456 mod 789699Fast exponentiation avoids creating the full power
2^16 mod 171An example of Fermat's little theorem

How to calculate a power modulo

  1. Enter the integer base, which may be positive, zero, or negative.
  2. Enter a nonnegative whole-number exponent.
  3. Enter a whole-number modulus greater than one.
  4. Select Calculate to get the exact remainder and the fast-algorithm operation count.

Modular exponentiation FAQ

Why not calculate the full power first?

The full power can contain millions of digits and waste both time and memory. Reducing after every multiplication gives the same final remainder while keeping intermediate integers manageable.

What is the square-and-multiply algorithm?

It reads the exponent through its binary digits and repeatedly squares the current base residue. Only factors corresponding to one bits are multiplied into the result.

Can the base be negative?

Yes, the calculator normalizes a negative base to its equivalent nonnegative residue modulo m. The final result is always between zero and m - 1.

What happens when the exponent is zero?

Any nonzero base raised to the zero power is one, and the modular result is 1 mod m. The calculator follows the same convention for a zero base with exponent zero.

Can this calculator handle cryptographic-size integers?

It uses arbitrary-size integer arithmetic, so input is not limited to ordinary floating-point precision. Very large exponents still require more work, but binary exponentiation keeps the number of stages logarithmic.