Number Theory & Bases

Mod Calculator

This mod calculator (modulo calculator) finds a mod n, the remainder when a is divided by n, with steps: a=nq+ra = nq + r with q=⌊a/n⌋q = \lfloor a/n \rfloor.

Try
Calculation

Any whole number, negative allowed, up to 100 digits.

Not 0. Powers need n ≥ 1 and inverses n ≥ 2.

Result

−17 mod 5: 3

−17 mod 5

3

In C, Java and JavaScript, −17 % 5 is −2.

Quotient, rounded down ⌊a ÷ n⌋
−4
Floored remainder (sign of n)Python %, Ruby %, R %%, Excel MOD
3
Truncated remainder (sign of a)C, C++, Java, JavaScript, C#, Go and Rust %
−2
Euclidean remainder (never negative)The usual mathematical residue, 0 ≤ r < |n|
3
Division statement
−17 = 5 × (−4) + 3
Congruence
−17≡3(mod5)-17 \equiv 3\;(\mathrm{mod}\; 5)
Exact quotient a ÷ n
−3.4

−17 mod 5 = 3 in mathematics. Programming languages disagree for negative numbers: Python gives 3, while C, Java and JavaScript give −2.

Step-by-step working (4 steps)
  1. Divide

    Start with the ordinary quotient.

    −175≈−3.4\frac{-17}{5} \approx -3.4
  2. Round the quotient down and find the remainder

    The floored convention rounds toward minus infinity, so the remainder takes the sign of n.

    q=⌊−175⌋=−4,r=−17−5⋅(−4)=3q = \left\lfloor \frac{-17}{5} \right\rfloor = -4,\quad r = -17 - 5 \cdot \left(-4\right) = 3
  3. Compare the truncated convention

    Many programming languages round the quotient toward zero instead, which gives a remainder with the sign of a.

    qt=−3,rt=−17−5⋅(−3)=−2q_t = -3,\quad r_t = -17 - 5 \cdot \left(-3\right) = -2
  4. Check

    Quotient times divisor plus remainder gives back a.

    5⋅(−4)+3=−175 \cdot \left(-4\right) + 3 = -17

For example, 17 mod 5 = 2 because 17 = 3 × 5 + 2. It also handles negative numbers, where Python and C or Java disagree, computes power mod abmodna^b \bmod n by square-and-multiply, and finds modular inverses with the extended Euclidean algorithm.

By Hira Noor, mathematicianLast reviewed Free, no sign-up, runs in your browserReport a problem

How to use this calculator

  1. Choose the calculation: a mod n, a product a × b mod n, a power aᵇ mod n (power mod), or the inverse a⁻¹ mod n.
  2. Type a and n as whole numbers. Negative values of a are allowed, for example −17; the modulus n cannot be 0.
  3. For a product, enter the second number b. For a power, enter the exponent b (0 or more, up to 30 digits). The modulus must be positive.
  4. For an inverse, use n ≥ 2. If a and n share a factor, the calculator explains why no inverse exists.
  5. Open the steps and the table to see the division, the reduced factors, the repeated squarings or the extended Euclidean rows.

Formulas

Division with remainder

a=nq+r,q=⌊an⌋,r=a−n⌊an⌋a = n q + r,\qquad q = \left\lfloor \frac{a}{n} \right\rfloor,\qquad r = a - n\left\lfloor \frac{a}{n} \right\rfloor

Plain-text formula: a = nq + r, q = ⌊a/n⌋, r = a − n⌊a/n⌋

With the quotient rounded down (floored division), the remainder has the same sign as n. For n > 0 this is the usual residue, 0 ≤ r < n. Rounding the quotient toward zero (truncated division) gives a remainder with the sign of a instead.

Square-and-multiply

ab=∏k:bk=1a2k,a2k+1=(a2k)2a^{b} = \prod_{k:\, b_k = 1} a^{2^{k}},\qquad a^{2^{k+1}} = \left(a^{2^{k}}\right)^{2}

Plain-text formula: a^b = ∏_{k: b_k = 1} a^2^k, a^2^(k + 1) = (a^2^k)^2

bkb_k
binary digit k of the exponent b

Writing b in binary means about log2⁡b\log_2 b squarings and at most as many multiplications, reducing modulo n each time so the numbers never grow beyond n2n^2.

Modular inverse

ax≡1;(mod;n)⇔ax+ny=1a x \equiv 1 ;(\mathrm{mod}; n) \iff a x + n y = 1

Plain-text formula: ax ≡ 1;(mod ;n) ⇔ ax + ny = 1

An inverse exists exactly when gcd(a, n) = 1. The extended Euclidean algorithm finds x and y, and x reduced modulo n is the inverse.

Reading the result

The big number is a mod n, the remainder in the range 0 to n − 1 for a positive modulus. The quotient and the division statement a=nq+ra = nq + r below it let you check the answer by hand: multiply back and add the remainder.

For positive numbers every convention gives the same remainder. They only differ when a or n is negative, which is why the result lists three remainders. Mathematics, Python and Excel give −17 mod 5 = 3 (count up from −20, the multiple of 5 just below −17). C, C++, Java, JavaScript, C#, Go and Rust compute −17 % 5 = −2, because they round −3.4 toward zero.

A congruence a ≡ r (mod n) means a and r leave the same remainder, or equivalently that n divides a − r. Modular powers are the basis of RSA encryption, Diffie–Hellman key exchange and primality tests; modular inverses are how RSA turns the public exponent into the private one.

How to calculate modulo by hand or on a calculator

Any calculator can find a mod n, even one without a mod key, in three steps:

1. Divide a by n. For 125 mod 7: 125 ÷ 7 = 17.857…

2. Round the quotient down to a whole number: 17. Multiply it back by n: 17 × 7 = 119.

3. Subtract from a: 125 − 119 = 6. So 125 mod 7 = 6, and the formula is amodn=a−n⌊a/n⌋a \bmod n = a - n \lfloor a/n \rfloor.

For a negative a, round down means toward minus infinity, not toward zero. For −17 mod 5: −17 ÷ 5 = −3.4 rounds down to −4, then −17 − 5 × (−4) = −17 + 20 = 3. Dropping the decimals instead (−3) gives −2, the answer C and Java print.

When a is smaller than n the answer is a itself: 1 mod 3 = 1 and 2 mod 4 = 2, because n goes into a zero times. When n divides a evenly the answer is 0: 10 mod 5 = 0. For mod 10 the answer is just the last digit: 1234 mod 10 = 4. In the same way, a number mod 2 is its last binary digit, so it tells you whether the number is odd or even.

Mod of negative numbers: Python vs C, Java and JavaScript

Programming languages split into two camps for the % operator. Floored languages give the remainder the sign of n; truncated languages give it the sign of a. The Euclidean remainder is never negative. Here is −7 mod 3 and its relatives under each rule:

To get the mathematical result (0 to n − 1) in a truncated language, write ((a % n) + n) % n, or use Math.floorMod(a, n) in Java and a.rem_euclid(n) in Rust. Python’s math.fmod gives the truncated result if you need it.

Remainder conventions for negative operands
ExpressionFloored (Python, Ruby, R %%, Excel MOD)Truncated (C, C++, Java, JavaScript, C#, Go, Rust %)Euclidean (math, always ≥ 0)
7 mod 3111
−7 mod 32−12
7 mod −3−211
−7 mod −3−1−12
−17 mod 53−23
−1 mod 2625−125

Clock and calendar arithmetic

Mod is the arithmetic of anything that wraps around. On a 12-hour clock, 5 hours after 10 o’clock is 3 o’clock, because (10 + 5) mod 12 = 15 mod 12 = 3.

On a 24-hour clock, 30 hours after 22:00 is (22 + 30) mod 24 = 52 mod 24 = 4, so 04:00 two days later.

For weekdays use mod 7. If today is Thursday, then 100 days from now is 100 mod 7 = 2 weekdays later, a Saturday, because 98 days is exactly 14 weeks.

Modular arithmetic rules

You can reduce each number modulo n before adding, subtracting, multiplying or raising to a power, and the final remainder is the same. That is what keeps modular arithmetic fast. The checks below use n = 7, with 23 ≡ 2 and 19 ≡ 5 (mod 7). Choose a × b mod n above to see the multiplication rule worked step by step.

Division is the exception: to divide by a modulo n, multiply by its modular inverse instead, which exists only when gcd(a, n) = 1. The GCD calculator shows that check with Euclid’s algorithm.

Modular arithmetic rules, checked with 23 and 19 mod 7
RuleDirectReduced first
(a + b) mod n = ((a mod n) + (b mod n)) mod n42 mod 7 = 0(2 + 5) mod 7 = 0
(a − b) mod n = ((a mod n) − (b mod n)) mod n4 mod 7 = 4(2 − 5) mod 7 = −3 mod 7 = 4
(a × b) mod n = ((a mod n) × (b mod n)) mod n437 mod 7 = 3(2 × 5) mod 7 = 10 mod 7 = 3
aᵏ mod n = (a mod n)ᵏ mod n23² = 529, 529 mod 7 = 42² mod 7 = 4

Solving ax ≡ b (mod n) with the inverse

A linear congruence such as 3x ≡ 5 (mod 11) is solved by multiplying both sides by the inverse of 3. Choose a⁻¹ mod n above with a = 3 and n = 11 to get 4, since 3 × 4 = 12 ≡ 1. Then x ≡ 4 × 5 = 20 ≡ 9 (mod 11). Check: 3 × 9 = 27 = 2 × 11 + 5.

If g = gcd(a, n) is larger than 1, there are solutions only when g divides b, and then there are g of them. For 6x ≡ 3 (mod 9), g = 3 divides 3, so divide everything by 3 to get 2x ≡ 1 (mod 3), giving x ≡ 2 (mod 3): x = 2, 5 or 8 modulo 9. For 6x ≡ 4 (mod 9) there is no solution, because 3 does not divide 4.

RSA key generation is the same calculation: with public exponent 17 and φ = 3120, the private exponent is the inverse of 17 mod 3120, which is 2753. RSA starts from two large primes, which you can test with the prime number checker.

Common mod, power mod and inverse results

Answers to values people often look up. Type any of them into the calculator to see the full steps.

Quick answers
QuestionAnswerWhy
17 mod 3217 = 5 × 3 + 2
125 mod 76125 = 17 × 7 + 6
1234 mod 104The last digit
−17 mod 53−17 = −4 × 5 + 3
9¹² mod 2924Square-and-multiply: 9⁸ × 9⁴ ≡ 20 × 7
19⁶ mod 23219 ≡ −4, and 4⁶ = 4096 = 178 × 23 + 2
5¹⁷ mod 299Square-and-multiply: 5¹⁶ × 5
3²⁰⁰ mod 1393³ = 27 ≡ 1, and 200 = 3 × 66 + 2
Inverse of 3 mod 1143 × 4 = 12 ≡ 1
Inverse of 7 mod 1187 × 8 = 56 ≡ 1
Inverse of 5 mod 26215 × 21 = 105 ≡ 1
Inverse of 11 mod 261911 × 19 = 209 ≡ 1
Inverse of 9 mod 2639 × 3 = 27 ≡ 1

Worked example

Compute 3¹³ mod 7

Find the remainder of 3¹³ divided by 7 without computing 3¹³ = 1,594,323 in full.

  1. Write the exponent in binary: 13 = 1101₂ = 8 + 4 + 1.
  2. Square repeatedly modulo 7: 31≡33^1 \equiv 3, 32=9≡23^2 = 9 \equiv 2, 34≡22=43^4 \equiv 2^2 = 4, 38≡42=16≡23^8 \equiv 4^2 = 16 \equiv 2.
  3. Multiply the powers for the 1 bits: 313=38⋅34⋅31≡2⋅4⋅3=243^{13} = 3^8 \cdot 3^4 \cdot 3^1 \equiv 2 \cdot 4 \cdot 3 = 24.
  4. Reduce: 24 = 3 × 7 + 3.

Answer: 313≡3;(mod;7)3^{13} \equiv 3 ;(\mathrm{mod}; 7). Check: 1,594,323 = 7 × 227,760 + 3.

Frequently asked questions

How do you calculate mod?

Divide a by n, round the quotient down to a whole number, multiply it by n and subtract the result from a. For 125 mod 7: 125 ÷ 7 = 17.86, rounded down to 17; 17 × 7 = 119; 125 − 119 = 6.

How do I calculate mod on a calculator without a mod key?

Use a mod n = a − n × ⌊a ÷ n⌋. Divide, keep the whole-number part, multiply it by n and subtract from a. For 100 mod 7: 100 ÷ 7 = 14.28…, 14 × 7 = 98, 100 − 98 = 2. For a negative a, round the quotient down (−3.4 becomes −4), not toward zero.

What does “mod” mean?

a mod n is the remainder after dividing a by n. For example, 125 mod 7 = 6 because 125 = 17 × 7 + 6. Clock arithmetic is mod 12: 5 hours after 10 o’clock is 3 o’clock, since 15 mod 12 = 3.

What is −17 mod 5?

In mathematics and in Python it is 3, because −17 = 5 × (−4) + 3. In C, Java and JavaScript, −17 % 5 is −2, because those languages use −17 = 5 × (−3) − 2. Both satisfy a = nq + r; they just round the quotient differently.

What is 1 mod 3?

1. Whenever a is smaller than n (and not negative), a mod n is a itself, because n goes into a zero times: 1 = 0 × 3 + 1. Likewise 2 mod 4 = 2.

What is 10 mod 5?

0. 5 divides 10 exactly (10 = 2 × 5 + 0), so there is no remainder. a mod n = 0 exactly when n is a factor of a.

What is a mod 0?

As a remainder it is undefined, because it would mean dividing by 0, so the calculator asks for a non-zero modulus. Python raises an error and JavaScript returns NaN; some textbooks define a mod 0 = a as a convention.

When does a modular inverse exist?

Exactly when a and n are coprime (their GCD is 1). For example, 3 × 4 = 12 ≡ 1 (mod 11), so 4 is the inverse of 3 mod 11, but 6 has no inverse mod 9 because gcd(6, 9) = 3.

How can a^b mod n be computed when b is huge?

With square-and-multiply. The work grows with the number of binary digits of b, not with b itself: 2560mod5612^{560} \bmod 561 needs only 9 squarings and 2 multiplications, and a 30-digit exponent needs about 100 squarings.

LCM and GCF Calculator

LCM and GCF of up to 10 numbers with steps: multiples, prime factors, Euclid’s algorithm and a coprime check.

Number Theory & Bases

Binary Calculator

Binary addition, subtraction and bitwise operations with steps, at 8 to 64 bits, signed or unsigned.

Number Theory & Bases

Hex to Decimal Converter

Hex to decimal with place-value steps, plus binary, octal and any base 2 to 36, fractions included.

Number Theory & Bases

Factorial Calculator

Exact n! with steps and every digit, plus digit count, trailing zeros, n!! and Stirling’s approximation.

Number Theory & Bases

Sources and further reading

  1. Concrete Mathematics, §3.4 “mod”: the binary operation, Addison-Wesley (Ronald L. Graham, Donald E. Knuth and Oren Patashnik, 1994)
  2. The Euclidean definition of the functions div and mod (opens in a new tab), ACM Transactions on Programming Languages and Systems 14, Association for Computing Machinery (Raymond T. Boute, 1992)
  3. The Art of Computer Programming, Vol. 2: Seminumerical Algorithms (§4.6.3), Addison-Wesley (Donald E. Knuth, 1997)
  4. What is modular arithmetic? (opens in a new tab), Khan Academy
  5. Binary arithmetic operations: the % operator (opens in a new tab), Python Software Foundation
  6. Remainder (%) (opens in a new tab), MDN Web Docs (Mozilla)

Results are calculated in your browser with methods checked by automated tests, and are intended for learning and general use. For engineering, medical, financial, legal or safety-critical decisions, verify important values independently and follow the rounding rules your work requires. How accurate are these results?