What is the Modular Arithmetic Calculator?
Modular arithmetic is "clock arithmetic": numbers wrap around after reaching a fixed modulus n. This calculator evaluates an expression of the form (a op b) mod n, where the operation can be addition, subtraction, multiplication, or simply reducing a single value a modulo n. It always returns the least non-negative residue, a number between 0 and n − 1.
How to use it
Enter a value for a, choose an operation, supply b (ignored for "Modulo only"), and set the modulus n. The calculator first computes the raw expression and then reduces it modulo n. Negative results are wrapped into the standard 0…n−1 range, so \(-1 \bmod 12\) returns 11 rather than \(-1\).
The formula explained
The core relationship is \(r = (a \text{ op } b) \bmod n\). Because a programming-style remainder can be negative, we use the Euclidean form $$r = ((x \bmod n) + n) \bmod n$$ to guarantee a non-negative answer. This matches the mathematical convention used in number theory, cryptography, and hashing.
Worked example
Suppose a = 17, the operation is addition, b = 25, and n = 12. First compute \(17 + 25 = 42\). Then \(42 \bmod 12\): $$42 = 3 \times 12 + 6,$$ so the residue is 6. On a 12-hour clock, 17 + 25 "hours" lands at the 6 position.
FAQ
What does "mod" mean? It returns the remainder after division. \(13 \bmod 5 = 3\) because \(13 = 2 \times 5 + 3\).
Why is my negative number turned positive? We return the least non-negative residue. For example \(-7 \bmod 5 = 3\), since adding 5 twice (\(-7 + 10 = 3\)) lands in the 0…4 range.
What if n = 1? Every integer is congruent to 0 modulo 1, so the result is always 0.