Last verified · v1.0
Calculator · math
Modulo Calculator
Calculate the remainder of any division instantly. Enter dividend and divisor to compute a mod n using floored, truncated, or Euclidean convention.
Inputs
Remainder (a mod n)
—
Explain my result
Get a plain-English breakdown of your result with practical next steps.
The formula
How the
result is
computed.
What Is the Modulo Operation?
The modulo operation returns the remainder after dividing one integer by another. Given a dividend a and a divisor n, the result r satisfies the equation:
r = a mod n = a − n · ⌊a ÷ n⌋
For example, 17 mod 5 = 2, because 17 = 5 × 3 + 2. The modulo operation appears throughout number theory, computer science, and cryptography — from clock arithmetic to RSA encryption. Mastering the uses of modulo calculator tools starts with understanding this foundational definition.
Formula Derivation
The formula is derived directly from the Division Algorithm: for any integers a and nonzero n, there exist unique integers q (quotient) and r (remainder) such that a = n · q + r, with the constraint that 0 ≤ r < |n|. Solving for r gives:
r = a − n · ⌊a / n⌋
Here, ⌊·⌋ denotes the floor function, which rounds toward negative infinity. This floored definition is the standard used by Python and aligns with the approach described in Khan Academy's introduction to modular arithmetic. The rigorous number-theoretic treatment appears in Evan Dummit's lecture notes on modular arithmetic in ℤ.
The Three Modulo Conventions
Different programming languages and mathematical traditions handle negative inputs differently. This calculator supports all three major conventions:
- Floored (Python / Khan Academy standard): The remainder always shares the sign of the divisor n. The quotient rounds toward −∞. Example: −7 mod 3 = 2, because ⌊−7 / 3⌋ = −3, so −7 − 3 × (−3) = 2.
- Truncated (C / Java / JavaScript standard): The remainder shares the sign of the dividend a. The quotient truncates toward 0. Example: −7 mod 3 = −1, because trunc(−7 / 3) = −2, so −7 − 3 × (−2) = −1.
- Euclidean: The remainder is always non-negative, regardless of the signs of a or n. Formula: r = a − |n| · ⌊a / |n|⌋. Example: −7 mod 3 = 2. Formal congruence theory covering this convention is available in UC Irvine's notes on congruences and congruence equations.
For positive values of both a and n, all three conventions produce identical results. Differences appear only when at least one operand is negative.
Variable Reference
- a — Dividend: The integer being divided. Accepts any positive, negative, or zero integer value.
- n — Divisor / Modulus: The number that a is divided by. Must be nonzero; a zero modulus makes the operation undefined.
- r — Remainder: The output of the operation. Its sign and range depend on the chosen convention.
Worked Examples
Example 1: Positive Integers
Compute 29 mod 6: quotient = ⌊29 / 6⌋ = 4; remainder = 29 − 6 × 4 = 5. Verification: 6 × 4 + 5 = 29. All three conventions agree on this result.
Example 2: Negative Dividend
Compute −13 mod 5 under each convention:
- Floored: ⌊−13 / 5⌋ = −3 → −13 − 5 × (−3) = 2
- Truncated: trunc(−13 / 5) = −2 → −13 − 5 × (−2) = −3
- Euclidean: ⌊−13 / 5⌋ = −3 → −13 − 5 × (−3) = 2
Real-World Uses of the Modulo Calculator
- Clock arithmetic: To find the hour 50 hours after 9:00 AM, compute (9 + 50) mod 12 = 59 mod 12 = 11 — the answer is 11:00 AM.
- Cryptography: RSA encryption computes ciphertext as me mod n, where n is the product of two large primes. The security of the scheme rests on the difficulty of reversing modular exponentiation.
- Day-of-week calculation: Zeller's congruence uses mod 7 to determine the weekday for any historical or future date, given the day, month, and year.
- Hash tables: Programs map arbitrary keys to fixed-size array indices via the formula
index = key mod tableSize, spreading data evenly across memory. - Cyclic sequences: Repeating patterns — carousel indices, pixel color wrapping, round-robin scheduling — use modulo to keep values within a fixed range without branching logic.
- Divisibility testing: A number a is divisible by n if and only if a mod n = 0. For instance, 144 mod 12 = 0 confirms that 144 is exactly divisible by 12.
Reference