The modulo operation gives the remainder left after dividing one number by another: because . It is the arithmetic of clocks and calendars, of check digits on bank cards and ISBNs, of hash tables and of every public-key cryptosystem.
This modulo calculator does three jobs. a mod n works for whole numbers of up to 60 digits and for decimals, using exact decimal arithmetic, so really gives 0.1$ (a typical programming language returns 0.09999999999999937). It also shows the three answers you can meet with negative numbers: the Euclidean remainder used in mathematics, the truncated remainder of the % operator in C, Java and JavaScript, and the floored remainder of Python and spreadsheet MOD functions.
Modular exponentiation, , uses the square-and-multiply method on numbers of up to 200 digits, with a bit-by-bit table. Modular inverse, , runs the extended Euclidean algorithm and shows the Bézout identity it produces.
How to use this calculator
- Pick the operation: a mod n, aᵇ mod n or a⁻¹ mod n.
- Enter the number a and the modulus n (and the exponent b for a power).
- For a mod n with negative values, choose the sign convention you need; all three remainders are listed anyway.
- Read the result and the congruence, then open the steps and the table to follow the algorithm.
The formula
Remainder. For a modulus , every number can be written . The convention decides which quotient is taken:
| Convention | Quotient | Remainder |
|---|---|---|
| Euclidean | chosen so that | never negative |
| Truncated | sign of | |
| Floored | sign of |
When and are positive the three agree. Two numbers are congruent modulo n, written , when divides .
Modular exponentiation. Write the exponent in binary, . Starting from , square once per bit and multiply it into the result whenever , reducing modulo after every operation:
A 200-digit exponent needs only about 660 squarings instead of an astronomically long chain of multiplications.
Modular inverse. is the number with . The extended Euclidean algorithm finds integers with ; when the gcd is 1, is the inverse. If the gcd is larger, no inverse exists.
Worked example
Negative numbers. For , Euclidean division gives , so the remainder is 3. The truncated remainder (C, JavaScript) is −2, the floored one (Python) is 3. With a negative modulus, is 2 (Euclidean), 2 (truncated) and −3 (floored).
Decimals. , since .
Power mod. : the exponent is 1001010100₂ (ten bits), so the method needs 9 squarings and 3 multiplications, and the answer is 1013. This is the worked example of the Handbook of Applied Cryptography (Example 2.144).
Inverse. , because . This is the private exponent of the classic textbook RSA example with and .
Modulo in everyday life and in code
- Clock arithmetic: 10 hours after 9 o’clock is .
- Days of the week: 100 days after a Monday, , so a Wednesday.
- Even or odd: is 0 for even numbers and 1 for odd ones.
- Check digits: an ISBN-10 is valid when its weighted digit sum is ; IBANs use .
- Programming: to wrap an index around an array of length , use the Euclidean (or floored) remainder; the % operator of C or JavaScript returns a negative value for negative indices.
For the quotient and the full column layout of a division, see the long division calculator; for common divisors, the GCD and LCM calculator.
Frequently asked questions
What does mod mean in math?
“a mod n” is the remainder of the division of a by n. Saying a ≡ b (mod n) means that a and b leave the same remainder, or equivalently that n divides a − b.
Why do calculators give different answers for −17 mod 5?
Because they choose different quotients. Mathematics and the Euclidean convention give 3 (a remainder is never negative); C, Java and JavaScript truncate the quotient toward zero and give −2; Python and Excel floor it and give 3. All three satisfy a = n × q + r.
Can you take the modulo of a decimal number?
Yes. The definition a = n × q + r with a whole quotient q still works: 7.5 mod 2 = 1.5. This calculator scales both numbers to whole numbers first, so there is no floating-point error.
How do you calculate a large power modulo n by hand?
Reduce the base modulo n, write the exponent in binary, then repeatedly square, keeping only the remainder after each step, and multiply together the squares that match the 1 bits. The numbers never grow beyond n².
When does a modular inverse exist?
Exactly when a and n are coprime, that is gcd(a, n) = 1. For example 4 has no inverse modulo 10, since every multiple of 4 is even and can never be 1 more than a multiple of 10.
Sources
- Boute, R. T. (1992). The Euclidean definition of the functions div and mod. ACM TOPLAS 14(2), 127–144
- Knuth, D. E. The Art of Computer Programming, Vol. 2, §4.5.2 (Euclid’s algorithm) and §4.6.3 (Evaluation of powers)
- Menezes, van Oorschot & Vanstone, Handbook of Applied Cryptography, ch. 2 (Algorithms 2.107, 2.142, 2.143)
- Python Language Reference — Binary arithmetic operations (% operator)
- MDN Web Docs — Remainder (%) operator in JavaScript
Last updated: