Why not just compute the power?
Computing ab first and reducing afterwards is hopeless for cryptographic sizes: 7560 already has 474 digits, and RSA exponents are hundreds of digits long. Modular exponentiation avoids this by reducing modulo n after every multiplication, so the numbers never grow beyond n².
The square-and-multiply algorithm
Write the exponent b in binary. Start with r = 1 and read the bits from the most significant to the least significant. For each bit, first square the running result, r ← r² mod n; then, if the bit is 1, also multiply by the base, r ← r·a mod n. After the last bit, r equals ab mod n.
The method needs only about log₂(b) squarings and at most as many multiplications, so even a 2048-bit exponent needs only a few thousand modular multiplications. The table in this calculator lists each bit, the square, the optional multiplication and the running value.
Where it is used
RSA encryption and decryption are modular exponentiations (c = me mod n and m = cd mod n). Diffie–Hellman and ElGamal compute powers modulo a prime, and primality tests such as Fermat and Miller–Rabin depend on them. A classic check: for the Carmichael number 561, 7560 ≡ 1 (mod 561) even though 561 = 3·11·17 is not prime.
Notes on inputs
The base may be negative; it is first reduced to its residue modulo n. The exponent must be a non-negative integer and the modulus a positive integer. If n = 1 every result is 0, and any non-zero base with exponent 0 gives 1 mod n.
A worked mini example: to compute 313 mod 7, write 13 = 1101 in binary. Start with r = 1. Bit 1: square (1) then multiply by 3 to get 3. Bit 1: square to 9 ≡ 2, multiply by 3 to get 6. Bit 0: square to 36 ≡ 1. Bit 1: square to 1, multiply by 3 to get 3. So 313 ≡ 3 (mod 7), which agrees with Fermat's little theorem because 36 ≡ 1 (mod 7) and 13 = 2·6 + 1.
Worked examples
These examples are computed by the same engine as the calculator above, so you can compare each step with your own working.
7^560 mod 561
Step-by-step solution
Formula used
r ← r² mod n; if the bit is 1: r ← r·a mod n
NoteBinary expansion of the exponent
560 in binary is 1000110000 (10 bits)
CalculateSquare, then multiply when the bit is 1
| Position | Bit | Square | Multiply | r |
|---|
| 9 | 1 | 1 | 7 | 7 |
| 8 | 0 | 49 | — | 49 |
| 7 | 0 | 157 | — | 157 |
| 6 | 0 | 526 | — | 526 |
| 5 | 1 | 103 | 160 | 160 |
| 4 | 1 | 355 | 241 | 241 |
| 3 | 0 | 298 | — | 298 |
| 2 | 0 | 166 | — | 166 |
| 1 | 0 | 67 | — | 67 |
| 0 | 0 | 1 | — | 1 |
3^200 mod 50
Step-by-step solution
Formula used
r ← r² mod n; if the bit is 1: r ← r·a mod n
NoteBinary expansion of the exponent
200 in binary is 11001000 (8 bits)
CalculateSquare, then multiply when the bit is 1
| Position | Bit | Square | Multiply | r |
|---|
| 7 | 1 | 1 | 3 | 3 |
| 6 | 1 | 9 | 27 | 27 |
| 5 | 0 | 29 | — | 29 |
| 4 | 0 | 41 | — | 41 |
| 3 | 1 | 31 | 43 | 43 |
| 2 | 0 | 49 | — | 49 |
| 1 | 0 | 1 | — | 1 |
| 0 | 0 | 1 | — | 1 |
Frequently asked questions
What is the complexity of square and multiply?
It needs about log₂(b) squarings and at most the same number of multiplications, so the cost grows with the number of bits of the exponent, not with the exponent itself.
How do I compute a^b mod n by hand?
Write b in binary, then for each bit from the left square the running value and, when the bit is 1, multiply by a, reducing modulo n each time. The table in this tool follows exactly that procedure.
Can the exponent be negative?
Not in this calculator. A negative exponent means a modular inverse followed by a power; compute the inverse with the extended Euclidean tool first.
Why is 7^560 mod 561 equal to 1?
561 is a Carmichael number, a composite number that satisfies a^(n−1) ≡ 1 (mod n) for every a coprime to it. It is the smallest such number.