Modulo Calculator (a mod n, Modular Inverse, Modular Power)
Work out a mod n with the C, Python and Euclidean conventions for negatives, find a modular inverse with Euclid steps, or compute a^b mod n for huge integers.
The conventions disagree because a or n is negative. Pick the one your language or textbook uses.
| Convention | Quotient q | Remainder r | Check: q × n + r |
|---|---|---|---|
| Euclidean (r ≥ 0) | -3 | 2 | -7 |
| Truncated (q toward 0) | -2 | -1 | -7 |
| Floored (q rounded down) | -3 | 2 | -7 |
The modulo operation gives the remainder after division: 17 mod 5 = 2 because 17 = 3 × 5 + 2. For positive numbers everyone agrees, but with negatives, programming languages give different answers, which causes real bugs. This calculator shows all three conventions side by side. It also finds modular inverses and modular powers of integers with hundreds of digits, the arithmetic behind clock maths, checksums and RSA.
How to use this tool
- Choose what to work out: a remainder, an inverse, a power or a congruence check.
- Enter a and the modulus n as whole numbers; negative values are allowed for remainders.
- For a power, enter the exponent b; for a congruence check, enter the second number b.
- Read the result and the step table (Euclid steps or square-and-multiply bits).
Formula
a = q × n + r · a⁻¹: a × a⁻¹ ≡ 1 (mod n) · a^b mod n by repeated squaring
Truncated division rounds the quotient q toward zero, so r takes the sign of a (C, Java, JavaScript). Floored division rounds q down, so r takes the sign of n (Python, Excel MOD). The Euclidean remainder is always between 0 and |n| − 1, the usual choice in number theory. The inverse comes from the extended Euclidean algorithm and exists only when gcd(a, n) = 1. Powers use square-and-multiply, so even a 1,000-digit exponent needs only a few thousand steps.
Example
For −7 mod 3, JavaScript's -7 % 3 gives −1 (−7 = −2 × 3 − 1), while Python's -7 % 3 gives 2 (−7 = −3 × 3 + 2); the Euclidean answer is also 2. In RSA with e = 17 and φ = 3120, the inverse of 17 mod 3120 is 2753, since 17 × 2753 = 46,801 = 15 × 3120 + 1. And 4¹³ mod 497 = 445.
Frequently asked questions
Why do Excel and JavaScript give different results for a negative number?
Excel's MOD(−7, 3) uses floored division and returns 2. JavaScript's −7 % 3 truncates toward zero and returns −1. Both satisfy a = q × n + r; they just pick different quotients.
When does a modular inverse not exist?
When a and n share a factor. 6 has no inverse mod 9 because both are divisible by 3, so 6 × x mod 9 can only be 0, 3 or 6, never 1.
How big can the numbers be?
Up to 1,000 digits for a, b and n. Exact BigInt arithmetic is used throughout, so nothing is rounded.
Does a^0 mod n give 1?
Yes, for any n of 2 or more. With n = 1 every result is 0, because every integer is a multiple of 1.
Sources
Last updated:
Page address with these inputs: https://kakao.mobile/tools/modulo-calculator/
Cite or embed this tool
kakaoCalc. (2026, November 8). Modulo Calculator (a mod n, Modular Inverse, Modular Power). https://kakao.mobile/tools/modulo-calculator/
“Modulo Calculator (a mod n, Modular Inverse, Modular Power).” kakaoCalc, 8 Nov. 2026, kakao.mobile/tools/modulo-calculator/.
Paste this code into your page. The widget runs in your visitors' browsers, shows no ads and keeps a link back to this page. Embedding terms