Modular exponentiation calculator (a^b mod m)

Enter the base, exponent and modulus to get the remainder of a^b divided by m without computing the full power. Useful for number theory and cryptography like RSA.

Loading the tool…

Processing and privacy: The result is calculated inside your browser and your data is not sent to any server.

How to use it

  1. Enter base a, exponent b and modulus m.
  2. The result appears straight away and updates as you edit.
  3. Copy the result, or copy a link that keeps the same values to share it.

How it is calculated

Square and multiply: read the bits of b from the lowest, square the base modulo m at each step, and multiply it into the result when the bit is 1.

Example

Inputs: Base a = 4, Exponent b = 13, Modulus m = 497. Result: a^b mod m: 445.

Limits and caveats

  • Base and exponent run from 0 to 10^15, the modulus from 1 to 10^15.
  • We take 0^0 = 1, as most number theory texts and programming languages do.

Questions about this tool

Why not compute a^b and then divide?

The full power can have billions of digits. Square and multiply keeps every step below m and needs only about log₂(b) steps.

What is 4^13 mod 497?

445, a common textbook example of the method.

How does it relate to Fermat's little theorem?

If p is prime and does not divide a, then a^(p-1) mod p = 1, for example 2^1000000006 mod 1000000007 = 1.