Modular Arithmetic Calculator
Compute modular addition, subtraction, and multiplication instantly. Use it in cryptography, competitive programming, or number theory to keep results within a fixed range.
About this calculator
Modular arithmetic works by wrapping numbers around a fixed value called the modulus m. For any two integers a and b, the modular addition is (a + b) mod m, subtraction is (a − b) mod m, and multiplication is (a × b) mod m. A shortcut that avoids overflow is to reduce each operand first: ((a % m) + (b % m)) % m. The result always falls in the range [0, m − 1]. This system underpins RSA encryption, hash functions, cyclic scheduling, and clock arithmetic. For example, hours on a 12-hour clock are computed mod 12, so 14 mod 12 = 2.
How to use
Suppose a = 17, b = 9, and m = 5. For addition: ((17 % 5) + (9 % 5)) % 5 = (2 + 4) % 5 = 6 % 5 = 1. For subtraction: ((17 % 5) − (9 % 5) + 5) % 5 = (2 − 4 + 5) % 5 = 3 % 5 = 3. For multiplication: ((17 % 5) × (9 % 5)) % 5 = (2 × 4) % 5 = 8 % 5 = 3. Enter your own a, b, and m to get all three results instantly.
Frequently asked questions
What is modular arithmetic and why is it used in cryptography?
Modular arithmetic restricts integer operations to a finite set of residues [0, m−1], making it ideal for cryptography. Algorithms like RSA and Diffie-Hellman rely on the difficulty of reversing modular exponentiation. The bounded output also prevents integer overflow in hardware implementations. This 'clock arithmetic' ensures computations stay within predictable limits no matter how large the inputs grow.
How do you calculate a negative result in modular arithmetic?
When subtraction yields a negative intermediate value, you add m before taking the final mod. For example, (2 − 4) mod 5 is computed as (2 − 4 + 5) % 5 = 3. This keeps the result in the non-negative range [0, m−1]. The calculator handles this adjustment automatically so you never get a negative output.
When should I use modular multiplication instead of regular multiplication?
Use modular multiplication whenever you need the product of two numbers reduced to a fixed range, such as in cyclic data structures, hash tables, or public-key cryptography. Regular multiplication can produce astronomically large numbers, causing overflow. Computing (a % m) × (b % m) % m keeps every intermediate value small. This is especially critical in embedded systems or languages with fixed integer widths.