Single Calculation
Batch Mode
Enter one pair per line as a,n,mode. Mode is euclidean or truncated. Mode optional; defaults to current selector.
| # | a | n | Mode | q | r | Identity |
|---|
Example Data Table
| a | n | Mode | q | r | Identity |
|---|---|---|---|---|---|
| -17 | 5 | Euclidean | -4 | 3 | -17 = (-4)·5 + 3 |
| -17 | 5 | Truncated | -3 | -2 | -17 = (-3)·5 + (-2) |
| 12345678901234567890 | 97 | Euclidean | 127270917535009968 | 14 | 12345678901234567890 = 127270917535009968·97 + 14 |
| 42 | 10 | Euclidean | 4 | 2 | 42 = 4·10 + 2 |
Formula Used
Euclidean remainder: For integer a and modulus n > 0, the remainder r is defined by r = a − n ⌊a / n⌋ with 0 ≤ r < n. The quotient is q = ⌊a / n⌋.
Truncated remainder: Some languages define q as the integer part truncated toward zero, i.e. q = trunc(a / n), then r = a − n·q, which may be negative when a is negative.
When numbers exceed native integer size, this tool computes Euclidean remainder using a string-based reduction algorithm requiring n to fit within native integer range.
How to Use This Calculator
- Enter the dividend a and a positive modulus n.
- Choose the remainder convention: Euclidean for non‑negative remainders, or Truncated to match many programming environments.
- Click Calculate to see q, r, and the identity a = q·n + r.
- Use Batch Mode to process many lines at once.
- Download your results table as CSV or PDF for reports.
- Refer to the Formula Used and FAQs if unsure about conventions.
FAQs
Language Conventions & Examples
When n > 0, different environments use different remainder conventions. Examples shown for a = -17, n = 5.
| Environment | Division Rule | Remainder Sign | Example Result | Notes |
|---|---|---|---|---|
| Python (%) | Floor division pairing | Non-negative when n > 0 | -17 % 5 = 3 | a == (a//n)*n + (a%n) |
| C / C++ (%) | Truncate toward zero | Same sign as dividend | -17 % 5 = -2 | q = trunc(a/n) |
| Java (%) | Truncate toward zero | Same sign as dividend | -17 % 5 = -2 | Matches C family behavior |
| JavaScript (%) | Truncate toward zero | Same sign as dividend | -17 % 5 = -2 | Remainder, not true modulo |
MATLAB mod(a,n) |
Euclidean-style | Non-negative when n > 0 | mod(-17,5) = 3 | rem(a,n) would give -2 |
| R (%%) | Euclidean-style for n > 0 | Non-negative when n > 0 | -17 %% 5 = 3 | Pairs with integer division %/% |
| Ruby (%) | Floor division pairing | Sign of divisor | -17 % 5 = 3 | Non-negative when n > 0 |
Residue Class Patterns
Euclidean remainder classes group integers that share the same remainder. Below are compact examples for common moduli.
Modulo 5 (Euclidean)
| r | Example a values |
|---|---|
| 0 | -20, -15, -10, -5, 0, 5, 10, 15, 20 |
| 1 | -19, -14, -9, -4, 1, 6, 11, 16, 21 |
| 2 | -18, -13, -8, -3, 2, 7, 12, 17, 22 |
| 3 | -17, -12, -7, -2, 3, 8, 13, 18, 23 |
| 4 | -16, -11, -6, -1, 4, 9, 14, 19, 24 |
Modulo 7 (Euclidean)
| r | Example a values |
|---|---|
| 0 | -14, -7, 0, 7, 14, 21 |
| 1 | -13, -6, 1, 8, 15, 22 |
| 2 | -12, -5, 2, 9, 16, 23 |
| 3 | -11, -4, 3, 10, 17, 24 |
| 4 | -10, -3, 4, 11, 18, 25 |
| 5 | -9, -2, 5, 12, 19, 26 |
| 6 | -8, -1, 6, 13, 20, 27 |