Modulo calculator
Enter a dividend and a modulus to get the remainder. Because languages disagree about negative numbers, you see the Euclidean, floored and truncated remainders side by side, with big-integer precision. A second mode computes a^b mod n.
Runs in your browser: nothing you enter is sent anywhere.
How to use it
- Choose remainder or modular power.
- Enter a and n (and the exponent e in power mode).
- The headline answer is the Euclidean remainder. The rows below show the other two conventions and their quotients.
How it works
The truncated remainder is the raw BigInt %. The floored remainder adds n when the raw remainder is nonzero and has the opposite sign from n. The Euclidean remainder adds |n| when the raw remainder is negative. Each quotient q satisfies a = n × q + r. Powers use square-and-multiply: for each bit of the exponent the running base is squared mod n.
Worked example
−17 mod 5:
- Truncated: −17 = 5 × (−3) + (−2), remainder −2.
- Floored and Euclidean: −17 = 5 × (−4) + 3, remainder 3.
And 2^100 mod 13: since 2^12 ≡ 1 (mod 13) and 100 = 8 × 12 + 4, the answer is 2^4 mod 13 = 16 mod 13 = 3.
Tips
- When a is a multiple of n the remainder is 0 in every convention.
- If a < n and both are positive, the remainder is just a.
- Check your work: n × quotient + remainder must give back a.
Frequently asked questions
What does a mod n mean?
It is the remainder after dividing a by n. 17 mod 5 = 2 because 17 = 3 × 5 + 2. Clocks use it: 15 o'clock is 15 mod 12 = 3 o'clock.
Why do languages give different answers for negative numbers?
The remainder must satisfy a = n × q + r, and several choices of q work. Truncated division (C, Java, JavaScript) rounds q toward zero, so −17 % 5 = −2. Floored division (Python) rounds q down, giving 3. Euclidean always keeps 0 ≤ r < |n|, also 3. Floored and Euclidean differ only when n is negative.
Which one should I use?
For the mathematical "a mod n", for hashing and array wrap-around, or for clock arithmetic, use the Euclidean value, which is never negative. Use the truncated one to match what a C-style % operator returns.
What is modular exponentiation?
It is a^e mod n computed without writing out the huge number a^e. Square-and-multiply keeps every step below n², so 4^13 mod 497 = 445 is quick and so is an exponent with hundreds of digits. It underlies RSA and Diffie-Hellman.
Is there a size limit?
Numbers may have up to 1,000 digits. All arithmetic uses exact big integers, so there is no rounding or overflow, unlike a 64-bit % operator.