Modular Arithmetic Calculator

Modular Arithmetic Calculator

Growing AI Skill PRO

Enter Calculation

Results

Least Non-Negative Residue
6
the result lies in the range 0 to n − 1
Expression value (before mod) 42
Modulus n 12

Formula

result=(((a+b)modn)+n)modn\text{result} = \left(\left(\left(\text{a} + \text{b}\right) \bmod \text{n}\right) + \text{n}\right) \bmod \text{n}
Usage stats

Advertisement

Report an error on this page

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.

Circular number ring from 0 to 11 with an arrow showing values wrapping around
Modular arithmetic wraps numbers around a circle of size n.

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.

Advertisement
Number line split into equal blocks showing negative values mapping to non-negative residues
Negative numbers map to the same least non-negative residue between 0 and n-1.

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.

More Worked Examples with Negative Numbers and Multiplication

In every case the calculator returns the least non-negative residue \(r\), the unique value with \(0 \le r < n\) and \(\text{raw} = n\cdot q + r\) for some integer \(q\). The modulus \(n\) must be a positive integer.

Example 1: Subtraction with a negative raw result

Compute \((5 - 23) \bmod 7\).

  1. Raw result: \(5 - 23 = -18\).
  2. Division: \(-18 = 7 \cdot (-3) + 3\), so the quotient is \(-3\) and the remainder is \(3\). Check: \(-18 + 21 = 3\).
  3. Final residue: 3.

Example 2: Multiplication

Compute \((47 \times 19) \bmod 10\).

  1. Raw result: \(47 \times 19 = 893\).
  2. Division: \(893 = 10 \cdot 89 + 3\), so the quotient is \(89\) and the remainder is \(3\).
  3. Final residue: 3. (A shortcut: mod 10 only the last digits matter, and \(7 \times 9 = 63\) ends in 3.)

Example 3: Plain modulo of a negative number

Compute \(-29 \bmod 6\).

  1. Raw value: \(-29\) (no second operand is needed for plain modulo).
  2. Division: \(-29 = 6 \cdot (-5) + 1\), so the quotient is \(-5\) and the remainder is \(1\). Check: \(-29 + 30 = 1\).
  3. Final residue: \(1\).

Note that the quotient is rounded down (toward negative infinity), not toward zero. This is what keeps the remainder between 0 and \(n-1\).

Advertisement

Scenario Comparison: Same Inputs, Different Operations

a b n Operation Raw result Final residue
17 25 12 Addition 42 = 12·3 + 6 6
17 25 12 Subtraction −8 = 12·(−1) + 4 4
17 25 12 Multiplication 425 = 12·35 + 5 5
−7 not used 5 Plain modulo −7 = 5·(−2) + 3 3
17 25 1 Multiplication 425 = 1·425 + 0 0

With \(n = 1\) every integer is congruent to 0, so the result is always 0.

Note: the Euclidean (least non-negative) residue can differ from the signed remainder returned by some programming languages. In C, C++, Java and JavaScript, the % operator truncates toward zero, so -7 % 5 gives \(-2\), while this calculator gives \(3\). Python's % with a positive modulus gives \(3\), matching the Euclidean result. The two values differ by exactly \(n\) whenever the signed remainder is negative.

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.

Last updated:

Most popular in Math and Statistics

View all Math and Statistics calculators