Modular Arithmetic Calculator (Mod Calculator)
A modular arithmetic calculator with 4 modes: basic mod, modular addition/subtraction/multiplication, modular exponentiation (repeated squaring), and modular inverse (extended Euclidean algorithm). Handles negative mod and huge exponents accurately using BigInt.
Basic Properties of Modular Arithmetic
| Property | Description |
|---|---|
| (a + b) mod n = ((a mod n) + (b mod n)) mod n | Whether you take the mod before or after adding, the result is exactly the same. |
| (a − b) mod n = ((a mod n) − (b mod n) + n) mod n | Since subtraction can produce a negative value, adding n at the end before taking mod n again keeps the result within [0, n). |
| (a × b) mod n = ((a mod n) × (b mod n)) mod n | As with addition, taking the mod partway through multiplication does not change the final result. This property underlies the fast computation of exponentiation (repeated squaring). |
| a and n are coprime ⇔ an inverse of a modulo n exists | Only when the extended Euclidean algorithm gives gcd(a, n) = 1 does there exist an x (the inverse) satisfying a × x ≡ 1 (mod n). |
What Is Modular Arithmetic (Mod Calculation)?
Modular arithmetic is a way of working with numbers where only the remainder after dividing by a positive integer called the modulus matters. When two integers a and b leave the same remainder after dividing by the same modulus n, we say "a is congruent to b modulo n," written as a ≡ b (mod n). This tool brings together the four calculations you run into most often when working with congruences: basic mod, modular addition/subtraction/multiplication, exponentiation, and inverse.
All the arithmetic runs on JavaScript's native BigInt under the hood, so exponents with hundreds of digits and multiplication or addition between very large integers are all handled without rounding errors. Negative inputs are normalized according to the mathematical definition of congruence (the result always falls between 0 and one less than the modulus), so you never have to worry about the mod operator behaving differently from one programming language to the next.
How to Use the Modular Arithmetic Calculator
- Choose a mode Pick the kind of calculation you need from the four modes: Basic Mod, Add/Subtract/Multiply, Exponentiation, or Inverse.
- Enter your integers Depending on the mode, enter a, b, and the modulus n (or the base and exponent). Negative integers can be entered directly.
- Pick an operator (Add/Subtract/Multiply mode only) Choose addition, subtraction, or multiplication from the dropdown.
- Check the result The result recalculates automatically as you type, showing both the formula and the answer. If no inverse exists, the tool switches to a message explaining why.
- Start over The Clear button empties the input fields for every mode at once.
Tips for getting more out of it
- The behavior of mod on negative numbers differs between programming languages. This tool follows the mathematical definition (the result is always between 0 and n − 1), so -7 mod 3 is 2, not -1.
- The exponentiation mode uses repeated squaring, so it returns results instantly even when the exponent has hundreds of digits. The same algorithm is used in RSA encryption and decryption.
- Clock time is a familiar example of modular arithmetic: converting "15:00" to 12-hour notation gives 15 mod 12 = 3 o'clock.
- The inverse mode works as long as a and n are coprime (their greatest common divisor is 1), even if n is not prime.
- In competitive programming, problems often ask for an answer modulo a large prime such as 1,000,000,007 instead of the raw, huge number. This tool's exponentiation mode is handy for checking such calculations by hand.
Where Modular Arithmetic Comes in Handy
Studying public-key cryptography
Encryption and decryption in RSA are, at their core, about raising a huge number to a power and taking the remainder modulo n. Try the exponentiation mode with a large exponent and modulus to see just how fast repeated squaring really is.
Understanding how hash functions are designed
Many hash functions use a modulo operation internally to fold values down into a fixed range. The Add/Subtract/Multiply mode lets you trace how the remainder changes at each step.
Working out how check digits are calculated
Check digits for ISBNs, credit card numbers, and bank account numbers are derived from the remainder of a weighted sum of digits divided by a modulus. The Basic Mod mode is handy for verifying these calculations by hand.
Working out day-of-week and calendar cycles
Questions like "what day of the week falls n days from now?" or "how many years make up a leap-year cycle?" can be expressed as congruences modulo 7 or 4.
Checking answers in competitive programming
For the common problem format that asks you to "give the answer modulo 1,000,000,007," you can use the exponentiation and Add/Subtract/Multiply modes to double-check that your own implementation produces the right result.
Modular Arithmetic Glossary
- Congruence
- An expression of the form a ≡ b (mod n), meaning "a and b leave the same remainder when divided by n." The symbol "≡" is used instead of "=" because it is the remainders that match, not the values themselves.
- Modulus
- The number you divide by. This tool requires an integer of 1 or greater. Changing the modulus changes which numbers are considered congruent to each other.
- Modulo operation (mod)
- The operation of actually computing the remainder after dividing a number by the modulus. Under the mathematical definition, the result always falls between 0 and one less than the modulus.
- Modular inverse
- An integer x satisfying a · x ≡ 1 (mod n). In modular arithmetic, multiplying by the inverse of a achieves the same effect as "dividing by a."
- Coprime
- Two integers whose greatest common divisor is 1. An inverse of a modulo n exists if and only if a and n are coprime.
- Extended Euclidean algorithm
- An algorithm that finds the greatest common divisor of two numbers while simultaneously finding integers x and y satisfying a · x + n · y = gcd(a, n). It is what powers the Inverse mode's calculation.
- Repeated squaring (fast exponentiation)
- An algorithm that computes a power by expanding the exponent in binary and repeatedly squaring the base, multiplying it into the result only at the digits that matter. This keeps the number of multiplications proportional to the number of digits in the exponent, and it is what the Exponentiation mode uses.
- Modular exponentiation
- The calculation of a base raised to a power, then reduced modulo n. It is the central operation behind RSA encryption and decryption, and it corresponds to this tool's Exponentiation mode.
Frequently Asked Questions
Side Note — The "Clock Arithmetic" Behind Modern Cryptography
Modular arithmetic (congruence) is often called "clock arithmetic." On a 12-hour clock, 13 o'clock is treated as the "same" as 1 o'clock — this is exactly the congruence 13 ≡ 1 (mod 12), an idea that focuses only on the remainder when a number is divided by a modulus (12 in this case). The German mathematician Carl Friedrich Gauss systematized the "≡" notation for congruence in his 1801 book Disquisitiones Arithmeticae, turning this idea into a standard tool of modern mathematics.
This seemingly simple operation underlies the foundations of modern internet security. In public-key cryptosystems such as RSA, "modular exponentiation" — computing a huge number raised to a power and then taking the remainder modulo n — is the central operation in encryption and decryption. Because the exponent and modulus can each run to hundreds of digits, naively computing the full power before taking the remainder would be computationally explosive. Repeated squaring instead requires only a number of multiplications proportional to the number of digits in the exponent, making it practical.
Computing modular inverses via the extended Euclidean algorithm is likewise a foundational technique used across computer science, from cryptography to coding theory and hash function design. The fact that "arithmetic over 2,000 years old" and "cutting-edge security technology" rest on the same mathematical foundation is a striking symbol of the universality of number theory.