Modular Exponentiation Calculator

Compute a to the power b modulo m using repeated squaring.

Math and PercentUpdated 2026-11-192 min read
Modular Exponentiation Calculator
A whole number base. Negative bases are reduced into the modulus range.
A whole number exponent from 0 to 1,000,000,000.
A whole number modulus from 2 to 1,000,000.
Result a to the power b mod m
...
Base reduced modulo m...
Exponent used...
Modulus used...
Exponent in binary...

What it does

Modular exponentiation finds the remainder of a to the power b divided by m. Computing the full power first is impossible for large exponents, so the standard method squares repeatedly and reduces after every step.

This tool takes a base, an exponent and a modulus, then applies the repeated squaring algorithm. It also reports the base reduced into the modulus range and the exponent written in binary, which explains which squared values get multiplied.

The page opens with three to the fifth power modulo seven. Three squared is nine, which reduces to two. Squaring again gives four, and multiplying by the original three gives twelve, which reduces to five.

The algorithm is important in cryptography because modular powers are easy to compute in the forward direction but hard to invert without extra information. Keeping every intermediate value below m also keeps the arithmetic small enough for a browser.

The modulus is limited to one million so that the product of two reduced values stays within the safe integer range of JavaScript. Larger moduli need arbitrary precision arithmetic.

How to use

  1. Enter the base as a whole number.
  2. Enter a whole number exponent.
  3. Enter a whole number modulus of at least 2.
  4. Read the result as the main output.
  5. Check the base modulo m to see the first reduction.
  6. Use the binary exponent to follow the squaring steps by hand.

Formula

Write b in binary, square the base repeatedly modulo m, and multiply the squares whose binary digit is 1, reducing after each multiplication
a
The base, reduced modulo m before use.
b
The whole number exponent.
m
The modulus, a whole number of at least 2.

The loop maintains the invariant that the running product times the current base to the current power equals a to the power b modulo m. When the remaining exponent is odd, one factor is absorbed into the product. When it is even, the base is squared and the exponent is halved. A guard limits the loop to 64 steps.

Worked example

Example: 3 to the 5th power modulo 7

The page opens with base 3, exponent 5 and modulus 7.

  1. Reduce the base: 3 modulo 7 is 3.
  2. Square once: 3 squared is 9, which reduces to 2 modulo 7.
  3. Square again: 2 squared is 4 modulo 7.
  4. The exponent 5 is binary 101, meaning powers 1, 2 and 4 are used.
  5. Multiply the square for 4 by the base for 1: 4 times 3 is 12.
  6. Reduce 12 modulo 7 to get 5.
  7. The tool reports result 5 and binary exponent 101.

Reference table

Modular powers for sample inputs
BaseExponentModulusResult
3575
14196722
210100024

Limits

  • Exponents are limited to 1,000,000,000.
  • The modulus must be between 2 and 1,000,000.
  • Inputs must be whole numbers.
  • The loop is capped at 64 iterations to avoid unbounded work.

Common mistakes

  • Computing the full power before reducing modulo m.
  • Forgetting to reduce after each multiplication.
  • Using a modulus of 1 or zero.
  • Reading the binary exponent as a decimal number.
  • Assuming a negative base gives a negative result.

Try it yourself

Quick checks. Answers sit tucked under each question.

14 to the 19th power modulo 67?

22. The binary exponent 10011 selects powers 1, 2 and 16.

2 to the 10th power modulo 1000?

24. 2 to the 10th power is 1024, which leaves remainder 24.

3 to the 5th power modulo 7?

5, as shown in the worked example.

Questions

Why avoid computing the full power?

The number becomes astronomically large. Repeated squaring keeps every value below the modulus.

How many multiplications are needed?

Roughly the number of bits in the exponent, rather than the exponent itself.

Why is the base reduced first?

Because multiplication modulo m depends only on the residues of the factors.

Where is this used?

Public key cryptography and primality testing rely on fast modular powers.

What if the exponent is zero?

Any nonzero base to the power zero modulo m is 1.

Can the modulus be negative?

No. The tool requires a whole number modulus of at least 2.

Sources

How this page was checked. The formula above was matched against the cited sources, and the worked example was run through the tool on this page before publishing. If you spot an error, contact us so we can fix it and note the correction.