Modular Exponentiation Calculator
Compute a to the power b modulo m using repeated squaring.
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
- Enter the base as a whole number.
- Enter a whole number exponent.
- Enter a whole number modulus of at least 2.
- Read the result as the main output.
- Check the base modulo m to see the first reduction.
- Use the binary exponent to follow the squaring steps by hand.
Formula
- 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.
- Reduce the base: 3 modulo 7 is 3.
- Square once: 3 squared is 9, which reduces to 2 modulo 7.
- Square again: 2 squared is 4 modulo 7.
- The exponent 5 is binary 101, meaning powers 1, 2 and 4 are used.
- Multiply the square for 4 by the base for 1: 4 times 3 is 12.
- Reduce 12 modulo 7 to get 5.
- The tool reports result 5 and binary exponent 101.
Reference table
| Base | Exponent | Modulus | Result |
|---|---|---|---|
| 3 | 5 | 7 | 5 |
| 14 | 19 | 67 | 22 |
| 2 | 10 | 1000 | 24 |
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
- Stanford University crypto notes, modular exponentiation by repeated squaring
- University of Toronto CSC373, repeated squaring and binary search notes
- Trinity College Dublin, efficient modular exponentiation notes