Skip to content

Modulo Calculator: a mod n, Power Mod and Inverse

a mod n for integers and decimals in every sign convention, plus modular exponentiation and modular inverses.

Your details

Operation

Negative numbers are allowed. Decimals are accepted for a mod n and kept exact.

Sign convention

Only matters when a or n is negative. Euclidean is the mathematical standard.

Results update as you type

Result

2

Congruence
17 ≡ 2 (mod 5)
Quotient (q)
3
a = n × q + r
17 = 5 × 3 + 2
Euclidean remainder (always ≥ 0)
2
Truncated: % in C, Java, JavaScript
2
Floored: % in Python, MOD in Excel
2

Step-by-step solution
  1. Quotient (Euclidean, 0 ≤ r < |n|): q = 3
  2. Remainder: r = a − n × q = 17 − 5 × 3 = 2

The modulo operation gives the remainder left after dividing one number by another: 17 mod 5=217 \bmod 5 = 2 because 17=5×3+217 = 5 \times 3 + 2. 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 5.1 mod 0.25.1 \bmod 0.2 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, ab mod na^b \bmod n, uses the square-and-multiply method on numbers of up to 200 digits, with a bit-by-bit table. Modular inverse, a−1 mod na^{-1} \bmod n, runs the extended Euclidean algorithm and shows the Bézout identity it produces.

How to use this calculator

  1. Pick the operation: a mod n, aᵇ mod n or a⁻¹ mod n.
  2. Enter the number a and the modulus n (and the exponent b for a power).
  3. For a mod n with negative values, choose the sign convention you need; all three remainders are listed anyway.
  4. Read the result and the congruence, then open the steps and the table to follow the algorithm.

The formula

Remainder. For a modulus n≠0n \ne 0, every number aa can be written a=n q+ra = n\,q + r. The convention decides which quotient qq is taken:

Convention Quotient qq Remainder rr
Euclidean chosen so that 0≤r<∣n∣0 \le r < \lvert n \rvert never negative
Truncated trunc⁡(a/n)\operatorname{trunc}(a / n) sign of aa
Floored ⌊a/n⌋\lfloor a / n \rfloor sign of nn

When aa and nn are positive the three agree. Two numbers are congruent modulo n, written a≡b(modn)a \equiv b \pmod n, when nn divides a−ba - b.

Modular exponentiation. Write the exponent in binary, b=∑ki2ib = \sum k_i 2^i. Starting from A=a mod nA = a \bmod n, square AA once per bit and multiply it into the result whenever ki=1k_i = 1, reducing modulo nn after every operation:

ab≡∏ki=1a2i(modn)a^b \equiv \prod_{k_i = 1} a^{2^i} \pmod n

A 200-digit exponent needs only about 660 squarings instead of an astronomically long chain of multiplications.

Modular inverse. x=a−1 mod nx = a^{-1} \bmod n is the number with a x≡1(modn)a\,x \equiv 1 \pmod n. The extended Euclidean algorithm finds integers s,ts, t with a s+n t=gcd⁡(a,n)a\,s + n\,t = \gcd(a, n); when the gcd is 1, s mod ns \bmod n is the inverse. If the gcd is larger, no inverse exists.

Worked example

Negative numbers. For −17 mod 5-17 \bmod 5, Euclidean division gives −17=5×(−4)+3-17 = 5 \times (-4) + 3, so the remainder is 3. The truncated remainder (C, JavaScript) is −2, the floored one (Python) is 3. With a negative modulus, 17 mod −517 \bmod -5 is 2 (Euclidean), 2 (truncated) and −3 (floored).

Decimals. 7.5 mod 2=1.57.5 \bmod 2 = 1.5, since 7.5=2×3+1.57.5 = 2 \times 3 + 1.5.

Power mod. 5596 mod 12345^{596} \bmod 1234: 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. 17−1 mod 3120=275317^{-1} \bmod 3120 = 2753, because 17×2753=46801=15×3120+117 \times 2753 = 46801 = 15 \times 3120 + 1. This is the private exponent of the classic textbook RSA example with p=61p = 61 and q=53q = 53.

Modulo in everyday life and in code

  • Clock arithmetic: 10 hours after 9 o’clock is (9+10) mod 12=7(9 + 10) \bmod 12 = 7.
  • Days of the week: 100 days after a Monday, 100 mod 7=2100 \bmod 7 = 2, so a Wednesday.
  • Even or odd: a mod 2a \bmod 2 is 0 for even numbers and 1 for odd ones.
  • Check digits: an ISBN-10 is valid when its weighted digit sum is ≡0(mod11)\equiv 0 \pmod{11}; IBANs use  mod 97\bmod 97.
  • Programming: to wrap an index around an array of length nn, 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

Last updated:

More Math calculators

People also use