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.
🕐 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
📖 How to Use This Calculator
Steps
💡 Example Calculations
Example 1 — Modular Addition
Find (17 + 25) mod 12
Example 2 — Modular Exponentiation
Find 7^128 mod 13
Example 3 — A Huge Exponent (Precision Stress Test)
Find 7^1000 mod 13
Example 4 — Modular Division (Modular Inverse)
Find 3 ÷ 11 mod 26
❓ Frequently Asked Questions
🔗 Related Calculators
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.