Modular Exponentiation

Fast aᵇ mod m by repeated squaring

4^13 mod 497
a^b mod m
445
Base reduced (a mod m)
4
Exponent in binary
1101
Repeated-squaring ladder
kbita^(2^k) mod mrunning result
0144
10164
2125630
31429445
Modular ExponentiationRepeated-squaring ladder for 4^13 mod 497: each rung shows a successive square a^(2^k) mod m, with rungs whose exponent bit is 1 highlighted as the ones multiplied into the running result 445.4^13 mod 497 = 445Exponent in binary: 1101²²²a^(2^0)41a^(2^1)160a^(2^2)2561a^(2^3)4291a^b mod m = 445

About this calculator

The Modular Exponentiation Calculator computes a^b mod m exactly using right-to-left binary exponentiation (repeated squaring), the same fast algorithm behind RSA and Diffie–Hellman. It handles huge bases and exponents with exact big-integer arithmetic, normalizes negative bases into [0, m), and shows the full squaring ladder bit by bit so you can follow how each exponent bit folds into the result. Useful for cryptography, number theory, and competitive-programming study.

Common examples

  • 3^4 mod 5 = 1 (81 mod 5)
  • 2^10 mod 1000 = 24 (1024 mod 1000)
  • 7^128 mod 13 = 3, computed in 8 squarings rather than 128 multiplications
  • a^0 mod m = 1 for any base (0^0 = 1 by convention)
  • (-3)^3 mod 7 = 1, since −3 ≡ 4 (mod 7) and 4^3 = 64 ≡ 1