Modulo Calculator
The definitive modular arithmetic calculator for calculating standard residues (a mod n), negative modulo transformations, fast modular exponentiation, and modular multiplicative inverses.
Enter Modulo Parameters
a mod nMathematical Division Theorem Form
The canonical mathematical modulo operation always yields a non-negative residue 0 ≤ r < n, calculated via floored integer division: r = a − n × ⌊a/n⌋.
Modular Clock / Ring Representation
ℤ/nℤClock arithmetic modulo 5: traversing 17 positions lands on 2.
How to Calculate Modulo (a mod n)
The modulo operation (abbreviated 'mod') computes the integer remainder left over after dividing one integer (the dividend a) by another (the modulus n). In canonical number theory, the remainder r is strictly non-negative (0 ≤ r < n), calculated via floored division: r = a - n × ⌊a/n⌋.
Modular Arithmetic Fundamentals & Clock Math
Modular arithmetic is a system of arithmetic for integers where numbers "wrap around" upon reaching a certain fixed value—the modulus (n). It is intuitively known as clock arithmetic because a standard 12-hour clock wraps back to 1 after reaching 12.
For example, if it is currently 9:00 and 5 hours pass, simple addition gives 9 + 5 = 14. On a 12-hour clock, we calculate 14 mod 12 = 2, so the clock reads 2:00.
The total number of unique states in the ring ℤ/nℤ, strictly {0, 1, 2, ..., n-1}.
All integers sharing the exact same remainder when divided by n (e.g. 2, 7, 12, 17 mod 5).
The unique non-negative representative of the equivalence class lying strictly within the base ring.
Mathematical vs Programming Modulo (Handling Negative Numbers)
One of the most frequent sources of bugs in computer programming is the distinction between Mathematical Modulo (Floored Division) and Programming Remainder (Truncated Division) when dealing with negative dividends:
Uses floored division (⌊-17 / 5⌋ = -4). Remainder is calculated as -17 - (5 × -4) = -17 + 20 = 3. The result is always guaranteed to be non-negative (0 ≤ r < 5).
Truncates division towards zero (trunc(-17 / 5) = -3). Remainder is calculated as -17 - (5 × -3) = -17 + 15 = -2. To fix this in JavaScript or C++, use ((a % n) + n) % n.
Core Modular Arithmetic Algebraic Rules
Modular arithmetic preserves standard algebraic identities under addition, subtraction, multiplication, and exponentiation:
- • Modular Addition: (a + b) mod n = [(a mod n) + (b mod n)] mod n
- • Modular Subtraction: (a − b) mod n = [(a mod n) − (b mod n) + n] mod n
- • Modular Multiplication: (a × b) mod n = [(a mod n) × (b mod n)] mod n
- • Modular Exponentiation: (ab) mod n = [(a mod n)b] mod n
Real-World Applications: Cryptography & Computing
Public-Key Cryptography
RSA encryption and Diffie-Hellman key exchange rely on the one-way trapdoor function of modular exponentiation and prime moduli.
Hash Tables & Data Structures
Hash functions map arbitrary keys into fixed-size array slots by evaluating hash(key) mod table_size.
Checksums & Barcodes
Credit card validation (Luhn Algorithm mod 10), ISBN-10 book codes (mod 11), and IBAN bank numbers (mod 97) prevent data transmission errors.
Step-by-Step Worked Examples
Calculate 125 mod 12.
1. Divide: 125 ÷ 12 = 10.4166... → Integer quotient q = 10.
2. Multiply: 12 × 10 = 120.
3. Subtract: 125 − 120 = 5.
Result: 125 mod 12 = 5 ⇔ 125 ≡ 5 (mod 12).
Calculate -25 mod 7.
1. Compute floored division: ⌊-25 / 7⌋ = ⌊-3.571⌋ = -4.
2. Multiply: 7 × (-4) = -28.
3. Subtract: -25 − (-28) = -25 + 28 = 3.
Result: -25 mod 7 = 3 ⇔ -25 ≡ 3 (mod 7). (Notice 0 ≤ 3 < 7).
Common Calculation Pitfalls & Division in Modular Math
You cannot simply divide numbers modulo n. Instead of dividing by a, you must multiply by its modular multiplicative inverse a-1, which only exists if gcd(a, n) = 1.
When computing (a - b) mod n where b > a, remember to add n before the final modulo to avoid negative residue errors.
Lead Developer & Founder of Basic Math Tools. Specializes in browser-native computational algorithms and applied mathematics.
Mathematics & curriculum specialists. Audited against standard algebraic and arithmetic principles.