−17 mod 5
- Calculate
- Remainder a mod n
- Dividend a
- -17
- Divisor n
- 5
- Result
- 3
- Truncated remainder (sign of a)
- -2
- Euclidean remainder (never negative)
- 3
- Floored quotient ⌊a ÷ n⌋
- -4
Checked against: Independent calculation
Calculate a mod n under the floored, truncated and Euclidean conventions, modular powers a^b mod m of large numbers, and modular inverses, with steps.
By AdminDrafted with AI writing toolsUpdated Checked examples: 10
−17 ÷ 5 leaves 3 when the quotient is rounded down to −4 (Python, Excel MOD), but −2 when it is rounded toward zero to −3 (C, JavaScript %).
Python's %, Excel's MOD and most maths texts round the quotient down.
C, Java and JavaScript's % round the quotient toward zero.
The modulo operation a mod n gives the remainder left when a is divided by n. For positive numbers every convention agrees (17 mod 5 = 2), but for negative numbers they split: the floored remainder r = a − n⌊a/n⌋ takes the sign of the divisor, the truncated remainder rounds the quotient toward zero and takes the sign of the dividend, and the Euclidean remainder is never negative. The calculator shows all three, and also computes modular powers by square-and-multiply and modular inverses by the extended Euclidean algorithm.
Clock and calendar arithmetic, hashing and cyclic buffers all use remainders, and cryptography rests on modular powers. The default, −17 mod 5, is 3 under floored division (Python's %, Excel's MOD) but −2 under truncated division (the % of C and JavaScript). The textbook RSA example encrypts 65 as 65^17 mod 3233 = 2790.
Modular powers stay exact for exponents as large as 10^18 because every squaring is reduced mod m. An inverse a⁻¹ mod m exists only when gcd(a, m) = 1.
The value a mod n is the remainder left when a is divided by n. Divide, round the quotient down and subtract: a mod n = a − n⌊a/n⌋. For 17 mod 5, 17 = 3 × 5 + 2, so the answer is 2. Positive inputs give the same answer under every convention; negative ones do not.
Every convention keeps a = n × q + r. They differ only in how the quotient q is rounded:
For −17 mod 5, −17 ÷ 5 = −3.4. Floored division rounds that to −4, so r = −17 − 5 × (−4) = 3. Truncation rounds it to −3, so r = −17 − 5 × (−3) = −2.
| Dividend a | Divisor n | Floored remainder | Truncated remainder | Euclidean remainder |
|---|---|---|---|---|
| −17 | 5 | 3 | −2 | 3 |
| 17 | −5 | −3 | 2 | 2 |
| −17 | −5 | −2 | −2 | 3 |
| 7.5 | 2 | 1.5 | 1.5 | 1.5 |
| 1/3 | 1/5 | 2/15 | 2/15 | 2/15 |
When the floored and truncated remainders disagree, they differ by exactly |n|: 3 and −2 are 5 apart. The last two rows show that remainders work for decimals and fractions too: 1/3 = 1 × 1/5 + 2/15.
Python's expression reference documents its floored %, and Leijen's paper on division and modulus compares the conventions.
For a positive n, ((a % n) + n) % n turns a truncated remainder into the floored one: ((−17 % 5) + 5) % 5 = (−2 + 5) % 5 = 3. Excel's MOD(−17, 5) already returns 3 (Microsoft's MOD documentation).
Square-and-multiply never lets a number grow past m². Write the exponent in binary and start from 1. For each bit from left to right, square the running value, multiply by a if the bit is 1, and reduce mod m. For 4¹³ mod 497, 13 is 1101 in binary:
So 4¹³ mod 497 = 445, found without computing 4¹³ = 67,108,864. The same method handles exponents such as 10^18 exactly. A negative exponent uses the inverse: 3⁻² mod 11 = 4² mod 11 = 5, because 3⁻¹ mod 11 is 4.
The inverse of a modulo m is the number x with a × x ≡ 1 (mod m), and it exists only when gcd(a, m) = 1. 3⁻¹ mod 11 = 4, because 3 × 4 = 12 = 11 + 1. 3⁻¹ mod 10 = 7, because 3 × 7 = 21 = 2 × 10 + 1.
6 has no inverse mod 15, because 6 and 15 share the factor 3. Multiples of 6 only ever leave remainders 0, 3, 6, 9 or 12 when divided by 15, never 1.
The extended Euclidean algorithm finds an inverse. For 3 mod 11: 11 = 3 × 3 + 2 and 3 = 1 × 2 + 1. Working back, 1 = 3 − 2 = 3 − (11 − 3 × 3) = 4 × 3 − 11, so the inverse is 4.
To find common factors before looking for an inverse, use the GCD and LCM calculator. For weekday questions, use the day of the week calculator.
Checked against: Independent calculation
Checked against: Euclidean 17 − 5·⌊17/5⌋ = 2
Checked against: 7.5 % 2 = 1.5
Checked against: Wikipedia — Modular exponentiation worked example
Divide, round the quotient down, and subtract: a mod n = a − n⌊a/n⌋. For 17 mod 5, 17 ÷ 5 = 3.4, which rounds down to 3, and 17 − 5 × 3 = 2. For −17 mod 5, −3.4 rounds down to −4, and −17 − 5 × (−4) = 3. On a 12-hour clock, 15:00 is 15 mod 12 = 3 o'clock.
They round the quotient differently. Python's % floors it, so −17 % 5 = 3, with the sign of the divisor; JavaScript, C and Java truncate toward zero, so −17 % 5 = −2, with the sign of the dividend. Both satisfy a = n × q + r. Excel's MOD matches Python. In JavaScript, ((a % n) + n) % n gives the floored answer when n is positive.
Use square-and-multiply: write the exponent in binary, square repeatedly, and reduce mod m after every step so the numbers never grow past m². For 4^13 mod 497, 13 is 1101 in binary and the answer is 445, the same as Python's pow(4, 13, 497). Computing 4^13 = 67,108,864 first works here, but not for exponents like 10^18.
The inverse of a modulo m is the number x with a × x ≡ 1 (mod m). 3⁻¹ mod 11 = 4 because 3 × 4 = 12 = 11 + 1. It exists only when gcd(a, m) = 1, so 2 has no inverse mod 10. The extended Euclidean algorithm finds it; in the textbook RSA example the private key 2753 is the inverse of 17 mod 3120, since 17 × 2753 = 46,801 = 15 × 3120 + 1.
For positive numbers they agree: 17 divided by 5 leaves 2 either way. For negative numbers the remainder in the C and JavaScript sense follows the sign of the dividend (−17 rem 5 = −2), while modulo in the mathematical sense follows the divisor or is never negative (−17 mod 5 = 3). When the two differ, they differ by exactly |n|.
This calculator includes 10 worked examples with independently sourced answers. They run in the test suite, and you can run them here too.