Modulo Calculator
Enter two whole numbers and get a mod n, shown next to the remainder so you can see why they differ for negative numbers. Switch tabs to find a modular inverse with the extended Euclidean steps, a power modulo n, or to test whether two numbers are congruent. Numbers can have up to 300 digits, and everything runs in your browser.
-7 = 3 x -3 + 2
The modulo operation gives what is left after dividing by n, written a mod n. For positive numbers it matches the % operator, but for negative numbers they differ: -7 mod 3 is 2 in mathematics and Python, while -7 % 3 is -1 in JavaScript, Java and C. This calculator shows both. A modular inverse of a is the number x with a x = 1 (mod n); it exists only when a and n share no common factor. Numbers can have up to 300 digits, and everything is worked out in your browser.
Related Tools
[5 suggestions]GCD and LCM Calculator
Find the greatest common divisor and least common multiple of two or more whole numbers, with the Euclidean algorithm steps.
Math & MoneyPrime Factorization Calculator
Break any whole number up to 18 digits into its prime factors, with division steps, all divisors and a prime check.
Math & MoneyBitwise Calculator
Apply AND, OR, XOR, NOT, shifts and rotations to 8, 16, 32 or 64-bit numbers and see the result in binary, hexadecimal and signed and unsigned decimal.
Developer & WebNumber Base Converter
Convert whole numbers between binary, octal, decimal, hex and any base from 2 to 36, with two's complement.
Developer & WebPermutation and Combination Calculator
Count permutations (nPr) and combinations (nCr), with or without repeats, plus factorials, as exact whole numbers.
Math & MoneyAbout the Modulo Calculator
The modulo operation finds what is left over after dividing one whole number by another. 17 mod 5 is 2, because 17 = 5 x 3 + 2. For positive numbers the mathematical modulo and the % operator in programming agree, but for negative numbers they do not. In mathematics, and in Python, a mod n always has the sign of n, so -7 mod 3 is 2. In JavaScript, Java and C, the % operator is a remainder that has the sign of the first number, so -7 % 3 is -1. The calculator shows both together with the floor and truncated quotients that go with them, and each satisfies a = n x quotient + result. The modular inverse of a modulo n is the number x for which a x leaves 1 when divided by n. It exists only when a and n share no common factor, for example the inverse of 3 mod 11 is 4 because 3 x 4 = 12 and 12 mod 11 = 1. It is found with the extended Euclidean algorithm, and the steps are shown. The power tab computes a to the power b mod n by repeated squaring, so even exponents with hundreds of digits are fast; for example 2 to the 10 mod 1000 is 24. A negative exponent uses the inverse. Two numbers are congruent modulo n when n divides their difference. These operations are the building blocks of clock arithmetic, hashing, checksums, calendars and public key cryptography.
What is the modulo operation?▾
It gives the remainder after division. 17 mod 5 = 2 because 5 goes into 17 three times with 2 left over. The result for a positive divisor is always from 0 up to one less than the divisor.
What is -7 mod 3?▾
In mathematics and Python it is 2, because -7 = 3 x (-3) + 2. In JavaScript, Java and C the % operator gives -1, because it keeps the sign of the first number. Both are correct for their definition.
How do I find a modular inverse?▾
Use the extended Euclidean algorithm to find x with a x + n y = 1. Then x mod n is the inverse. It exists only when the greatest common divisor of a and n is 1. For 3 mod 11 the inverse is 4.
How do I calculate a large power mod n?▾
Square repeatedly and reduce mod n at each step, multiplying in the base for each 1 bit of the exponent. This avoids ever forming the huge power, so it is fast even for very large exponents.
What does congruent mean?▾
a is congruent to b modulo n, written a = b (mod n), when n divides a minus b, that is, when a and b leave the same remainder on division by n. 38 and 14 are congruent mod 12.
Is my data uploaded?▾
No. The calculation runs in your browser and nothing you enter is sent anywhere.