Calculator Tools
Modular Arithmetic Calculator
Compute a mod m, modular add, subtract, multiply, exponentiation, and the modular inverse, plus GCD with Bezout. Exact for huge numbers, no signup.
Modular operation
Pick an operation, then enter the operands and the modulus m.
The mathematical remainder, always between 0 and m minus 1.
Result
17 mod 12
5
As a + q * m
17 = 1 * 12 + 5
% operator (sign follows a)
5 (same)
GCD and Bezout coefficients
The extended Euclidean algorithm, the engine behind the modular inverse. It also reports the least common multiple.
gcd
2
lcm
5520
Coprime?
No
Bezout identity
(240) * -9 + (46) * 47 = 2
How modular arithmetic works here
- The mod result is the mathematical residue, always between 0 and m minus 1. So negative-7 mod 3 is 2, not negative-1. The % operator in C, Java, Go, and JavaScript instead keeps the sign of the dividend, which is the common source of off-by-one bugs.
- a^b mod m uses fast (square and multiply) exponentiation, reducing the value at every step. That is why an enormous exponent, like the ones in RSA and Diffie-Hellman, never overflows.
- The modular inverse of a is the number that multiplies with a to give 1 modulo m. It exists only when a and m are coprime, meaning gcd(a, m) is 1, which is why a prime modulus is so convenient.
- The extended Euclidean algorithm returns gcd(a, b) together with Bezout coefficients x and y that satisfy a*x + b*y = gcd(a, b). The modular inverse falls straight out of that identity.
- Every value is held as an arbitrary-precision integer, so results stay exact no matter how large the numbers get.
How to use
- Choose an operation: a mod m, modular add, subtract, multiply, a^b mod m, or the modular inverse of a.
- Enter the operand a, the second operand b when the operation needs one, and the modulus m (a positive whole number).
- Read the result, and for plain mod compare the always-non-negative residue with the percent operator remainder and the quotient.
- For the modular inverse, check the line that multiplies a by the inverse to confirm it gives 1 modulo m, or read why no inverse exists.
- Use the GCD panel for any two integers to get the greatest common divisor, the least common multiple, and the Bezout coefficients, then copy any value.
About this tool
Modular Arithmetic Calculator works out the modulo and the standard modular operations that number-theory students, cryptography learners, and programmers keep needing, and it does them exactly for arbitrarily large numbers because every value is held as an arbitrary-precision integer. The starting point is the mod itself. Given a number a and a modulus m, the calculator returns a mod m as the mathematical residue, the value that is always between 0 and m minus 1. This is the detail that trips people up, because the percent operator in C, Java, Go, and JavaScript keeps the sign of the dividend, so in those languages negative-7 percent 3 is negative-1, while negative-7 mod 3 is 2 in proper modular arithmetic. The tool shows both side by side, along with the quotient q in the identity a equals q times m plus the residue, so you can see exactly how the wrap-around works and flags when the two disagree. On top of the plain mod it computes modular addition, subtraction, and multiplication, each reduced back into the 0 to m minus 1 range, and modular exponentiation, a to the power b modulo m, using fast square-and-multiply exponentiation. Square and multiply reduces the running value at every step instead of forming the astronomically large a to the b first, which is why a huge exponent of the kind used in RSA and Diffie-Hellman is handled instantly and never overflows. It also finds the modular multiplicative inverse, the number that multiplies with a to give 1 modulo m, computed with the extended Euclidean algorithm. The inverse exists only when a and m are coprime, meaning their greatest common divisor is 1, and when no inverse exists the tool says so and reports the gcd that blocks it rather than returning a wrong answer, which is also why a prime modulus is so convenient in cryptography. A negative exponent is supported by inverting the base first, where that base is invertible. A separate panel runs the extended Euclidean algorithm on any two integers and reports the greatest common divisor, the least common multiple, whether the pair is coprime, and the Bezout coefficients x and y that satisfy a times x plus b times y equals the gcd, which is the identity the modular inverse falls out of. Inputs accept an optional sign, digit separators, and a 0x or 0b prefix so machine values paste cleanly, and the modulus is required to be a positive whole number. Common uses include discrete mathematics and number theory homework, understanding hashing and checksums, working through RSA and Diffie-Hellman by hand, clock and calendar style wrap-around problems, and debugging the negative-modulo behaviour of a programming language. Everything is computed in your browser with native big-integer math, so the numbers you enter are never uploaded.
Free to use. Works in your browser. No signup, no login.
Related tools
You may also like
GCD and LCM Calculator
GCD and LCM for any set of integers, with Euclidean steps and prime factorization.
Open tool
CalculatorPrime Number Calculator
Primality test plus prime factorization, divisors, divisor count, divisor sum, and next or previous prime.
Open tool
CalculatorPermutation and Combination Calculator
nPr, nCr, factorial, n^r, and multiset coefficient with exact step-by-step working.
Open tool
DeveloperTwo's Complement Calculator
Signed and unsigned integer to binary, hex, and octal at every common bit width.
Open tool
DeveloperBinary Arithmetic Calculator
Binary add, subtract, multiply, and divide with step-by-step carry and borrow.
Open tool