Modular Arithmetic Calculator
Modular arithmetic reduces integers to their remainders after division by a fixed number called the modulus. Enter values for a, b, and m to compute all five standard modular operations at once: basic remainder (a mod m), modular addition ((a + b) mod m), modular subtraction ((a - b) mod m), modular multiplication ((a times b) mod m), and modular exponentiation (a raised to the power b, mod m). The exponentiation uses the fast square-and-multiply algorithm so even large exponents like a^1000 mod m compute instantly. Modular arithmetic underpins clock arithmetic, cyclic patterns, hash functions, and public-key cryptography such as RSA and Diffie-Hellman. The key property is that (a + b) mod m = ((a mod m) + (b mod m)) mod m, and the same holds for multiplication. This allows computations to stay within a small range no matter how large the original values grow.
Modular arithmetic formulas
a mod m = a - m * floor(a / m)
(a + b) mod m = ((a mod m) + (b mod m)) mod m
(a - b) mod m = ((a mod m) - (b mod m) + m) mod m
(a * b) mod m = ((a mod m) * (b mod m)) mod m
a^b mod m: use square-and-multiply (repeated squaring)
The +m in the subtraction formula ensures the result stays non-negative when (a mod m) is less than (b mod m).
Modular arithmetic: frequently asked questions
What is modular arithmetic?
Modular arithmetic is a system of arithmetic for integers in which numbers wrap around upon reaching a certain value called the modulus. For example, 17 mod 5 = 2, because 17 = 3 times 5 plus 2, and the remainder is 2. It is the same arithmetic used in a 12-hour clock.
What does a mod m mean?
a mod m means the remainder when a is divided by m. Formally, a mod m = a - m * floor(a/m). For example, 23 mod 7 = 2 because 23 = 3 * 7 + 2. This calculator follows the convention that the result is always non-negative, in the range 0 to m-1.
How does modular exponentiation work?
Computing a^b mod m directly would require computing an enormous number first. Fast modular exponentiation (also called square-and-multiply) instead uses the fact that (a*b) mod m = ((a mod m)*(b mod m)) mod m. The exponent b is processed one bit at a time, keeping all intermediate results below m.
Why is modular arithmetic important in cryptography?
Many public-key cryptography systems, including RSA, rely on modular exponentiation. The security comes from the difficulty of reversing a^b mod m to find b when a and m are very large. The RSA encryption operation is: ciphertext = message^e mod n, where e and n are public key components.
What is the difference between mod and remainder in programming?
In mathematics, a mod m is always non-negative (in 0 to m-1). In some programming languages, the % operator returns a negative remainder for negative inputs. For example, -7 % 3 = -1 in many languages, but -7 mod 3 = 2 mathematically. This calculator uses the mathematical convention.
Sources
- Modular arithmetic fundamentals: NIST Introduction to Finite Fields.
- Fast modular exponentiation: NIST SP 800-56B Rev. 2.
Reviewed by the CalculatorHub team, edited by James Graham, 14 June 2026. See our methodology.