Modular Arithmetic Calculator

Compute (a op b) mod m for addition, subtraction, multiplication, exponentiation, and modular division. Uses exact big-integer arithmetic, so huge exponents never lose precision.

🕐 Modular Arithmetic Calculator
Result
a mod m

🕐 What is Modular Arithmetic?

Modular arithmetic is a system where numbers wrap around once they reach a fixed value called the modulus, m. Instead of the usual number line stretching to infinity, modular arithmetic treats numbers as sitting on a circle of m positions, labeled 0 through m−1. Adding, subtracting, or multiplying two numbers is done normally, then the result is reduced to fit back onto that circle by taking the remainder after division by m.

The most familiar example is a 12-hour clock, which is exactly mod-12 arithmetic: if it is 9 o'clock and 5 hours pass, the time is not 14 o'clock but 2 o'clock, because 14 mod 12 = 2. Days of the week work the same way with modulus 7. Beyond everyday timekeeping, modular arithmetic is the mathematical backbone of check-digit systems (ISBN numbers, credit card validation), hash functions and hash tables in computer science, and nearly all modern public-key cryptography, including RSA encryption, which depends on fast modular exponentiation of very large numbers.

A common pitfall when implementing modular exponentiation in code is computing the full power first and then taking the remainder, for example Math.pow(a, b) % m in JavaScript. For even moderately large exponents, the full power vastly exceeds what a standard floating-point number can represent exactly, silently corrupting the result. The correct approach is fast modular exponentiation (repeated squaring), which keeps every intermediate value reduced modulo m throughout the computation, so the numbers involved never grow unreasonably large.

This calculator supports all five common modular operations: addition, subtraction, multiplication, exponentiation (via safe repeated squaring using exact BigInt arithmetic, so exponents in the thousands compute correctly), and division (via the modular inverse, computed with the extended Euclidean algorithm). It always shows a mod m alone as well, which is useful as a quick standalone remainder check independent of the chosen operator.

📐 Formula

Result  =  (a  op  b) mod m
a, b = any integers (positive, negative, or zero)
m = the modulus, a positive integer (m ≥ 1)
op = +, −, ×, ^ (power via repeated squaring), or ÷ (via modular inverse)
Division: a ÷ b mod m = (a × b⁻¹) mod m, where b⁻¹ is the modular inverse of b, found via the extended Euclidean algorithm (exists only when gcd(b, m) = 1)
Example: (17 + 25) mod 12 = 42 mod 12 = 6

📖 How to Use This Calculator

Steps

1
Enter a, b, and the modulus m – a and b can be any whole number, including negative values. The modulus m must be a positive whole number.
2
Choose an operator – select +, −, ×, ^ (power), or ÷ (division via modular inverse) from the dropdown.
3
Read the result – the calculator shows (a op b) mod m, a mod m alone, and the full worked expression, including the modular inverse used for division.

💡 Example Calculations

Example 1 — Modular Addition

Find (17 + 25) mod 12

1
Add first: 17 + 25 = 42
2
Reduce modulo 12: 42 = 3 × 12 + 6, so 42 mod 12 = 6
(17 + 25) mod 12 = 6
Try this example →

Example 2 — Modular Exponentiation

Find 7^128 mod 13

1
7^128 has over 100 digits – too large to compute directly, so use repeated squaring, reducing modulo 13 after every squaring step
2
Each intermediate result stays a small number below 13, keeping the computation exact throughout
7^128 mod 13 = 3
Try this example →

Example 3 — A Huge Exponent (Precision Stress Test)

Find 7^1000 mod 13

1
7^1000 has over 800 digits – Math.pow(7,1000) would lose all precision in standard floating point
2
Fast modular exponentiation with exact BigInt arithmetic computes this correctly in about 10 squaring steps (since 1000 in binary needs 10 bits)
7^1000 mod 13 = 9
Try this example →

Example 4 — Modular Division (Modular Inverse)

Find 3 ÷ 11 mod 26

1
Find the modular inverse of 11 mod 26 using the extended Euclidean algorithm: 11⁻¹ ≡ 19 (mod 26), since 11 × 19 = 209 = 8×26 + 1
2
Multiply: 3 ÷ 11 mod 26 = (3 × 19) mod 26 = 57 mod 26 = 5
3 ÷ 11 mod 26 = 5
Try this example →

❓ Frequently Asked Questions

What is modular arithmetic?+
Modular arithmetic is a system where numbers wrap around after reaching a fixed value, the modulus m. The result of a mod m is the remainder after dividing a by m, always kept in the range 0 to m−1. It is often called clock arithmetic because a 12-hour clock is mod-12 arithmetic: 9 + 5 wraps to 2 o'clock, not 14.
How do you compute a mod m by hand?+
Divide a by m and keep the remainder, adjusting negative values so the result stays in [0, m−1]. Example: 17 mod 12 = 5, since 17 = 1×12 + 5. For −5 mod 12: −5 = −1×12 + 7, so the answer is 7, not −5. This calculator handles the negative-number normalization automatically.
What is (17 + 25) mod 12?+
17 + 25 = 42, and 42 mod 12 = 6 because 42 = 3×12 + 6. Modular addition always works this way: add normally, then reduce by the modulus.
How do you compute large exponents like 7^128 mod 13?+
Never compute 7^128 directly (it has over 100 digits). Instead use fast modular exponentiation (repeated squaring), reducing modulo m after every squaring step. This gives 7^128 mod 13 = 3 exactly, and works just as well for far larger exponents like 7^1000 mod 13 = 9.
Why is Math.pow(a, b) % m wrong for large b?+
JavaScript numbers are IEEE-754 doubles with about 15-17 significant digits of precision. Math.pow(7, 128) has over 100 digits, far beyond what a double can hold exactly, so the result is silently rounded before the modulo is even applied, producing a wrong answer. Repeated squaring with BigInt avoids ever forming the full power, keeping every step exact.
What is a modular inverse?+
The modular inverse of b modulo m is a number b⁻¹ such that (b × b⁻¹) mod m = 1. It exists only when b and m are coprime (gcd(b, m) = 1). Division a ÷ b (mod m) is defined as a × b⁻¹ mod m. Example: the inverse of 11 mod 26 is 19, since 11 × 19 = 209 = 8×26 + 1.
How is the modular inverse calculated?+
The extended Euclidean algorithm finds integers x and y such that b×x + m×y = gcd(b, m). If gcd(b, m) = 1, x mod m is the modular inverse. This calculator runs the algorithm automatically for the ÷ operator and reports 'no inverse exists' whenever gcd(b, m) is greater than 1.
What is 3 ÷ 11 mod 26?+
First find 11's inverse mod 26 using the extended Euclidean algorithm: 11⁻¹ ≡ 19 (mod 26), because 11 × 19 = 209 = 8×26 + 1. Then 3 ÷ 11 mod 26 = (3 × 19) mod 26 = 57 mod 26 = 5.
Where is modular arithmetic used in real life?+
It underlies clocks and calendars, check-digit systems like ISBN and the credit card Luhn algorithm, hash tables in computer science, and modern public-key cryptography such as RSA and Diffie-Hellman, both of which rely heavily on fast modular exponentiation and modular inverses.
What happens when I subtract a larger number from a smaller one?+
The result still lands correctly in [0, m−1] because negative remainders are normalized by adding the modulus. Example: (5 − 20) mod 7 = −15 mod 7 = 6, since −15 = −3×7 + 6.
Can the modulus m be 1?+
Yes, though it is trivial: every integer is congruent to 0 modulo 1, since any number divides evenly by 1. This calculator accepts m = 1 and correctly returns 0 for every operation.
Why does JavaScript's % operator give different results than true mod?+
Mathematically, a mod m is always non-negative for a positive m. JavaScript's built-in % operator returns a result with the same sign as the dividend, so −5 % 12 gives −5 in raw JavaScript, not the mathematically correct 7. This calculator always applies the true mathematical modulo convention used in number theory.

What is modular arithmetic?

Modular arithmetic is a system of arithmetic for integers where numbers 'wrap around' after reaching a fixed value called the modulus (m). The result of a mod m is always the remainder after dividing a by m, restricted to the range 0 to m−1. It is often called clock arithmetic because a 12-hour clock is exactly mod-12 arithmetic: 9 + 5 = 14, which wraps to 2 o'clock.

How do you compute a mod m by hand?

Divide a by m and keep the remainder, adjusting for negative values so the result stays in [0, m−1]. For example, 17 mod 12 = 5, since 17 = 1×12 + 5. For a negative number like −5 mod 12: −5 = −1×12 + 7, so the answer is 7, not −5. This calculator normalizes negative inputs automatically.

What is (17 + 25) mod 12?

17 + 25 = 42. 42 mod 12 = 6, because 42 = 3×12 + 6. So (17 + 25) mod 12 = 6. Modular addition always follows this pattern: add normally, then reduce by the modulus.

How do you compute large exponents in modular arithmetic, like 7^128 mod 13?

Never compute 7^128 directly (it has over 100 digits) and then take the remainder - use fast modular exponentiation instead, which repeatedly squares the base and reduces modulo m at every step. Using this method, 7^128 mod 13 = 3. This calculator implements repeated squaring with BigInt internally so results stay exact even for exponents in the thousands, such as 7^1000 mod 13 = 9.

Why is Math.pow(a, b) % m wrong for large b in JavaScript?

JavaScript numbers are IEEE-754 doubles with about 15-17 significant decimal digits of precision. Math.pow(7, 128) is a number with over 100 digits, far beyond what a double can represent exactly, so the result is silently rounded and the final mod is wrong. The fix is repeated squaring (also called fast or binary exponentiation), which never forms the full power - it keeps every intermediate result reduced modulo m, so it stays exact using BigInt.

What is a modular inverse?

The modular inverse of b modulo m is a number b⁻¹ such that (b × b⁻¹) mod m = 1. It exists only when b and m are coprime, meaning their greatest common factor is 1. Division a ÷ b (mod m) is defined as a × b⁻¹ mod m. For example, the inverse of 11 mod 26 is 19, since 11 × 19 = 209 = 8×26 + 1.

How is the modular inverse calculated?

The extended Euclidean algorithm finds integers x and y such that b×x + m×y = gcd(b, m). If gcd(b, m) = 1, then x mod m is the modular inverse of b. This calculator runs the extended Euclidean algorithm automatically whenever you choose the divide (÷) operator, and reports 'no inverse exists' when gcd(b, m) is greater than 1.

What is 3 ÷ 11 mod 26?

First find the inverse of 11 mod 26 using the extended Euclidean algorithm: 11⁻¹ ≡ 19 (mod 26), because 11 × 19 = 209 = 8×26 + 1. Then 3 ÷ 11 mod 26 = (3 × 19) mod 26 = 57 mod 26 = 5.

Where is modular arithmetic used in real life?

Modular arithmetic underlies clocks and calendars (time and day-of-week calculations), check-digit systems like ISBN and credit card numbers (Luhn algorithm), hash tables and hash functions in computer science, and almost all modern public-key cryptography, including RSA and Diffie-Hellman key exchange, which rely heavily on fast modular exponentiation and modular inverses.

What happens if I subtract a larger number from a smaller one in modular arithmetic?

The result still lands correctly in [0, m−1] because negative remainders are normalized by repeatedly adding the modulus until the value falls in range. For example, (5 − 20) mod 7: 5 − 20 = −15, and −15 mod 7 = 6, since −15 = −3×7 + 6.

Can the modulus m be 1?

Yes, though it is a trivial case: every integer is congruent to 0 modulo 1, since any number divides evenly by 1 with remainder 0. This calculator accepts m = 1 and correctly returns 0 for every operation.

What is the difference between mod and remainder in some programming languages?

In mathematics, a mod m is always non-negative for a positive m. Some programming languages (including JavaScript's % operator) return a result with the same sign as the dividend, so −5 % 12 gives −5 in raw JavaScript, not the mathematically correct 7. This calculator always applies the true mathematical modulo, matching the convention used in number theory.