Modular Arithmetic Calculator
Calculate a mod n, addition, subtraction, multiplication, and modular exponentiation.
Enter two values and a modulus for instant results.
Modular arithmetic is arithmetic on a number line that wraps around at a fixed value — the modulus. When you reach n, you cycle back to 0.
a mod n = the remainder when a is divided by n
12 mod 5 = 2 (because 12 = 2×5 + 2) −3 mod 5 = 2 (the calculator keeps the result in the range 0 to n−1)
Operations:
- (a + b) mod n: add first, then take the remainder
- (a − b) mod n: subtract, adjust for negative results to stay in [0, n−1]
- (a × b) mod n: multiply, then reduce
- a^b mod n: modular exponentiation — the most computationally interesting one
Modular exponentiation uses the fast exponentiation algorithm (also called binary exponentiation or exponentiation by squaring). Without it, computing 7^1000 mod 13 would require working with a number that has 845 digits. With it, the calculation needs at most log₂(1000) ≈ 10 squarings.
The algorithm: while the exponent is > 0, if it is odd multiply the running result by the base, then square the base and halve the exponent.
Where modular arithmetic appears: cryptography relies on it entirely — RSA encryption is based on the difficulty of factoring large numbers under modular exponentiation. Clock arithmetic is modular (hours mod 12 or 24). ISBN check digits use modular sums. Hash functions compute modular reductions constantly.
The Chinese Remainder Theorem, the foundation of much of number theory, is a statement about systems of modular equations. If you know a number modulo several different primes, you can reconstruct the number — and RSA key generation exploits this directly.