Calculate a raised to the power b, modulo m, even when the numbers have hundreds of digits. The calculator uses exact BigInt arithmetic and the square-and-multiply method, and lists each step with the binary digits of the exponent.
Last updated:
Whole number, may be negative. Expressions like 2^64 work.
Whole number; negative uses the modular inverse
Whole number ≥ 1, e.g. 1000000007 or 2^127-1
Enter your values to see the result.
Advertisement
Advertisement
How to use the Modular Exponentiation Calculator
Enter the base a, e.g. a message value in RSA.
Enter the exponent b; it can be enormous, like 10^18.
Enter the modulus m, e.g. 3233 or 10^9+7.
Press Calculate to get a^b mod m.
Study the table to see each squaring and multiplication.
Worked example: 3^13 mod 7 by square-and-multiply
13 in binary is 1101₂ (8 + 4 + 1). Square the base each step and multiply when the bit is 1, reducing mod 7 every time:
Bit (right to left)
Base power mod 7
Bit
Running result
0
3¹ ≡ 3
1
1 × 3 = 3
1
3² ≡ 9 ≡ 2
0
3
2
3⁴ ≡ 2² ≡ 4
1
3 × 4 = 12 ≡ 5
3
3⁸ ≡ 4² = 16 ≡ 2
1
5 × 2 = 10 ≡ 3
So 3^13 mod 7 = 3, found with 3 squarings and 3 multiplications. Check with Fermat’s little theorem: 7 is prime, so 3^13 ≡ 3^(13 mod 6) = 3^1 = 3 ✓.
Useful rules
Product rule: (x × y) mod m = ((x mod m) × (y mod m)) mod m, which is why reducing at every step is safe.
Fermat: if p is prime and p does not divide a, a^(p−1) ≡ 1 (mod p), so b can be reduced mod p − 1.
Euler: more generally, if gcd(a, m) = 1 then a^φ(m) ≡ 1 (mod m), where φ is Euler’s totient.
Cost: square-and-multiply needs at most 2·log₂(b) modular multiplications, so even a 2048-bit exponent takes only a few thousand steps.
Common moduli in programming contests are 10^9 + 7 and 998,244,353, both prime.
Frequently asked questions
What is modular exponentiation?
It is computing the remainder when a^b is divided by m, written a^b mod m. For example 3^4 mod 5 = 81 mod 5 = 1. It is a core operation in RSA, Diffie–Hellman, primality tests and hashing.
Why not just compute a^b and then take the remainder?
a^b gets astronomically large: 2^(10^18) has about 3 × 10^17 digits. Reducing mod m after every multiplication keeps every number below m², and the identity (x·y) mod m = ((x mod m)(y mod m)) mod m guarantees the same final answer.
How does square-and-multiply work?
Write b in binary. Repeatedly square the base (a, a², a⁴, a⁸, … all mod m) and multiply into the result the powers whose bit is 1. 3^13 uses 13 = 1101₂, so 3^13 = 3^8 · 3^4 · 3^1. This needs about log₂(b) steps instead of b − 1 multiplications.
How is it used in RSA?
Encryption computes c = m^e mod n and decryption computes m = c^d mod n. With the textbook key n = 3233, e = 17, d = 2753, the message 65 encrypts to 65^17 mod 3233 = 2790, and 2790^2753 mod 3233 gives 65 back.
What does a negative exponent mean in modular arithmetic?
a^(−k) mod m means (a⁻¹)^k mod m, where a⁻¹ is the modular inverse. It exists only when gcd(a, m) = 1. For example 7^(−1) mod 26 = 15, since 7 × 15 = 105 = 4 × 26 + 1.
What is Fermat's little theorem?
If p is prime and a is not a multiple of p, then a^(p − 1) ≡ 1 (mod p). So exponents can be reduced mod p − 1: 3^200 mod 13 = 3^(200 mod 12) mod 13 = 3^8 mod 13 = 9. The calculator shows this shortcut whenever the modulus is prime.
Are all the tools on FreeToolCabin tested for quality and accuracy?
Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.