Modular Arithmetic Calculator — Free Online Mod Calculator
Perform modular arithmetic operations including addition, subtraction, multiplication, and exponentiation with GCD and modular inverse calculations.
Expression
(17 + 5) mod 7Result
Calculation
(17 + 5) mod 7 = 1The result of (17 + 5) mod 7 is 1. The answer is always in the range [0, 6].
a mod m
3
b mod m
5
gcd(a, m)
1
Modular Inverse (a⁻¹ mod m)
5
How to Use the Modular Arithmetic Calculator
- Select the operation: Choose from four modular operations using the dropdown. Addition computes (a + b) mod m, subtraction computes (a - b) mod m, multiplication computes (a x b) mod m, and power computes a^b mod m using efficient modular exponentiation.
- Enter value a: Type the first integer operand. For exponentiation, this is the base. The value can be any integer including negative numbers — the calculator automatically reduces it modulo m.
- Enter value b: Type the second integer operand. For exponentiation, this is the exponent (must be non-negative). For other operations, b can be any integer.
- Enter the modulus (m): Type the positive integer modulus. All results will be in the range [0, m-1]. The modulus must be at least 1.
The result panel also shows the reduced values of a and b (their remainders mod m), the greatest common divisor of a and m, and the modular inverse of a modulo m if it exists. The default values (17, 5, 7) demonstrate a simple addition: (17 + 5) mod 7 = 22 mod 7 = 1.
Modular Arithmetic Formulas
Modular Addition
(a + b) mod m = ((a mod m) + (b mod m)) mod m Modular Subtraction
(a - b) mod m = ((a mod m) - (b mod m) + m) mod m Modular Multiplication
(a x b) mod m = ((a mod m) x (b mod m)) mod m Modular Exponentiation
a^b mod m (computed via square-and-multiply) Modular Inverse
a^(-1) mod m: find x such that (a x x) mod m = 1 Variables Explained
- a (First Operand): The first integer in the operation. For exponentiation, this is the base. Any integer is valid — the calculator reduces it modulo m internally.
- b (Second Operand/Exponent): The second integer. For exponentiation, b must be a non-negative integer (the exponent). For other operations, any integer works.
- m (Modulus): The positive integer that defines the arithmetic system. Results are always in [0, m-1]. Common moduli include primes (for cryptography), powers of 2 (for computing), 10 (for check digits), 12 (for clocks), and 7 (for days of the week).
- gcd(a, m): The greatest common divisor. If gcd(a, m) = 1, the modular inverse of a exists. If gcd(a, m) > 1, no modular inverse exists.
Step-by-Step Example
Calculate 7^13 mod 11:
- Start with a = 7, b = 13, m = 11
- Express 13 in binary: 13 = 1101 in base 2 (8 + 4 + 1)
- Square-and-multiply: 7^1 mod 11 = 7, 7^2 mod 11 = 49 mod 11 = 5, 7^4 mod 11 = 5^2 mod 11 = 25 mod 11 = 3, 7^8 mod 11 = 3^2 mod 11 = 9
- Combine: 7^13 = 7^8 x 7^4 x 7^1 = 9 x 3 x 7 = 189
- 189 mod 11 = 189 - 17 x 11 = 189 - 187 = 2
By Fermat's Little Theorem, since 11 is prime, 7^10 mod 11 = 1. So 7^13 = 7^10 x 7^3 = 1 x 343, and 343 mod 11 = 2. Both methods confirm the answer.
Practical Examples
Example 1: Alex Determines the Day of the Week
Alex wants to know what day of the week it will be 200 days from a Wednesday. Using modular arithmetic with modulus 7 (days in a week), where Wednesday = 3:
- Current day: Wednesday = 3
- (3 + 200) mod 7 = 203 mod 7
- 203 / 7 = 29 remainder 0
- 203 mod 7 = 0 = Sunday
200 days from Wednesday is a Sunday. This is exactly how calendar applications calculate future dates. The modulo operation elegantly handles the cyclic nature of weeks without needing to count through all 200 days individually.
Example 2: Priya Validates an ISBN
Priya is writing a library system and needs to validate ISBN-10 numbers using check digits. For ISBN 0-306-40615-2, the validation uses modular arithmetic with modulus 11:
- Multiply each digit by its position: 0x1 + 3x2 + 0x3 + 6x4 + 4x5 + 0x6 + 6x7 + 1x8 + 5x9 + 2x10
- Sum: 0 + 6 + 0 + 24 + 20 + 0 + 42 + 8 + 45 + 20 = 165
- 165 mod 11 = 0 (since 165 = 15 x 11)
- Result is 0, so the ISBN is valid
If the remainder is not 0, the ISBN contains an error. This check digit system can detect any single-digit error and most transposition errors. Credit card numbers use a similar modular validation called the Luhn algorithm (mod 10).
Example 3: Carlos Implements Hash Table Indexing
Carlos is building a hash table with 13 slots to store student records. He uses student IDs as keys and the modulo operation to compute array indices:
- Student 10452: 10452 mod 13 = 10452 - 803 x 13 = 10452 - 10439 = 13, 13 mod 13 = 0 (slot 0)
- Student 20318: 20318 mod 13 = 20318 - 1563 x 13 = 20318 - 20319 = hmm, 1562 x 13 = 20306, so 20318 - 20306 = 12 (slot 12)
- Student 15677: 15677 mod 13 = 15677 - 1205 x 13 = 15677 - 15665 = 12 (collision with slot 12!)
Carlos encounters a collision — two keys mapping to the same slot. He resolves it using chaining or open addressing. Choosing a prime number for the table size (like 13) helps distribute keys more evenly. This modular hashing is used in every programming language's dictionary/map implementation.
Modular Arithmetic Quick Reference Table
| Operation | a | b | mod | Result |
|---|---|---|---|---|
| (a+b) mod m | 14 | 23 | 12 | 1 |
| (a-b) mod m | 5 | 18 | 7 | 1 |
| (a*b) mod m | 7 | 8 | 10 | 6 |
| a^b mod m | 2 | 10 | 1000 | 24 |
| a^b mod m | 3 | 100 | 7 | 4 |
| (a+b) mod m | -3 | 8 | 5 | 0 |
| Inverse | 3 | - | 7 | 5 (3x5=15, 15 mod 7=1) |
Tips and Complete Guide
Fermat's Little Theorem
If p is a prime number and a is not divisible by p, then a^(p-1) mod p = 1. This powerful theorem simplifies many modular exponentiation problems. For example, to compute 7^100 mod 13: since 13 is prime, 7^12 mod 13 = 1. Then 100 = 12 x 8 + 4, so 7^100 = (7^12)^8 x 7^4 = 1^8 x 7^4 mod 13. Computing 7^4 = 2401, and 2401 mod 13 = 9 (since 2401 = 184 x 13 + 9). This theorem is also the basis for the Fermat primality test used in cryptography.
Chinese Remainder Theorem
The Chinese Remainder Theorem (CRT) states that if you know the remainders of an integer when divided by several pairwise coprime moduli, you can reconstruct the original number uniquely modulo the product of those moduli. For example, a number that is 2 mod 3, 3 mod 5, and 2 mod 7 is uniquely determined modulo 105 (= 3 x 5 x 7). CRT has practical applications in RSA decryption (speeding up computation by working with smaller moduli) and in ancient Chinese and Indian mathematics for solving systems of congruences.
Modular Arithmetic in Programming
Every programming language provides a modulo operator (usually %). However, languages differ in how they handle negative numbers: in C, C++, and Java, -7 % 3 = -1, while in Python, -7 % 3 = 2. The Python convention (result has the same sign as the divisor) matches the mathematical definition. To ensure non-negative results in any language, use ((a % m) + m) % m. Modulo is used extensively for circular buffers, hash tables, random number generators, and any situation where values need to cycle through a fixed range.
Applications in Cryptography
RSA encryption works as follows: choose two large primes p and q, compute n = pq, find e and d such that ed mod ((p-1)(q-1)) = 1. Encrypt message m as c = m^e mod n. Decrypt as m = c^d mod n. The security relies on the fact that factoring n into p and q is computationally infeasible for sufficiently large primes (2048+ bits). Diffie-Hellman key exchange uses g^a mod p (where p is prime and g is a generator) to establish shared secrets over insecure channels. All of these require efficient modular exponentiation.
Common Mistakes to Avoid
- Forgetting the modulus must be positive: The modulus m must be a positive integer. Negative moduli and zero moduli are undefined in standard modular arithmetic. Our calculator requires m greater than 0.
- Assuming negative mod results are valid: In pure mathematics, the result of a mod m is always in [0, m-1]. Some programming languages return negative remainders for negative dividends. Always normalize: ((a % m) + m) % m gives the correct non-negative result.
- Trying to divide in modular arithmetic: Division is not directly defined. Instead, multiply by the modular inverse: a/b mod m = a x b^(-1) mod m, but only when the inverse of b exists (when gcd(b, m) = 1). If gcd(b, m) > 1, the "division" is undefined.
- Confusing modular exponentiation with regular exponentiation: a^b mod m is NOT the same as (a mod m)^b for large b — intermediate values overflow. Use the square-and-multiply algorithm to keep numbers manageable throughout the computation.
- Using non-prime moduli when primality is required: Fermat's Little Theorem and many cryptographic algorithms require a prime modulus. Using a composite modulus in these contexts gives incorrect results. Always verify that your modulus is prime when using theorems that assume primality.
Frequently Asked Questions
Modular arithmetic is a system of arithmetic for integers where numbers wrap around after reaching a certain value called the modulus. The operation a mod m gives the remainder when a is divided by m. For example, 17 mod 5 = 2 because 17 divided by 5 is 3 with remainder 2. The most familiar real-world example is clock arithmetic: 10 o'clock + 5 hours = 3 o'clock on a 12-hour clock, which is 15 mod 12 = 3. Modular arithmetic is fundamental to number theory, cryptography, and computer science.
Modular addition computes (a + b) mod m. First add a and b normally, then take the remainder when divided by m. For example, (8 + 7) mod 5: 8 + 7 = 15, and 15 mod 5 = 0. An equivalent approach is to reduce each operand first: 8 mod 5 = 3, 7 mod 5 = 2, then (3 + 2) mod 5 = 5 mod 5 = 0. Both methods give the same result. This property — that you can reduce before or after — is what makes modular arithmetic so useful in computation.
Modular exponentiation computes a^b mod m — raising a to the power b, then taking the remainder when divided by m. Direct computation of a^b can produce astronomically large numbers, but modular exponentiation can be done efficiently using the square-and-multiply algorithm without ever computing the full power. This is the mathematical foundation of RSA encryption, Diffie-Hellman key exchange, and most public-key cryptography. For example, computing 7^256 mod 13 is trivial with modular exponentiation but the raw value of 7^256 has hundreds of digits.
The modular inverse of a modulo m is a number x such that (a x x) mod m = 1. It exists only when a and m are coprime (their greatest common divisor is 1). For example, the inverse of 3 mod 7 is 5, because 3 x 5 = 15, and 15 mod 7 = 1. Not all numbers have modular inverses — 4 has no inverse mod 6 because gcd(4, 6) = 2, not 1. Modular inverses are essential in cryptographic algorithms and in solving linear congruences.
Nearly all modern cryptography relies on modular arithmetic. RSA encryption uses modular exponentiation with extremely large prime numbers — a message m is encrypted as c = m^e mod n and decrypted as m = c^d mod n, where n is the product of two large primes. The security relies on the difficulty of factoring n. Elliptic curve cryptography operates over modular arithmetic on special curves. Hash functions, digital signatures, and key exchange protocols all use modular operations extensively.
Two integers a and b are congruent modulo m (written a is congruent to b mod m) if they have the same remainder when divided by m, or equivalently, if m divides (a - b). For example, 38 is congruent to 14 mod 12 because both have remainder 2 when divided by 12 (38 = 3 x 12 + 2, 14 = 1 x 12 + 2). Congruences satisfy many properties of equality: you can add, subtract, multiply, and raise to powers on both sides while maintaining the congruence.
Division answers 'how many times does the divisor fit into the dividend?' while modulo answers 'what is left over?' For 17 divided by 5: the quotient is 3 (division) and the remainder is 2 (modulo). Mathematically, a = q x m + r, where q is the quotient and r is the remainder (0 to m-1). In programming, the mod operator (% in most languages) returns this remainder. Note that different programming languages handle negative numbers differently with modulo — our calculator always returns non-negative results.
Clock time is modular arithmetic with modulus 12 (or 24). Days of the week use modulus 7 — if today is Wednesday (day 3) and you add 10 days, (3 + 10) mod 7 = 6 (Saturday). ISBN check digits use mod 10 or mod 11. Credit card validation (Luhn algorithm) uses mod 10. Hash table indexing in programming uses modular arithmetic to map keys to array positions. Calendar calculations for leap years, day-of-week formulas, and even music theory (12-tone equal temperament uses mod 12) all rely on modular arithmetic.
Related Calculators
Exponent Calculator
Calculate powers before applying modular reduction.
Log Calculator
Compute logarithms related to discrete logarithm problems.
Root Calculator
Calculate roots that connect to modular square roots.
Matrix Calculator
Matrix operations can also be performed in modular arithmetic.
Percentage Calculator
Remainder-based calculations for everyday percentage problems.
Random Number Generator
Random number algorithms use modular arithmetic internally.
Disclaimer: This calculator is for informational and educational purposes only. Results are estimates and may not reflect exact values.
Last updated: February 23, 2026
Sources
- Khan Academy — Modular Arithmetic: khanacademy.org
- Wolfram MathWorld — Modular Arithmetic: mathworld.wolfram.com
- NIST — Cryptographic Standards and Guidelines: csrc.nist.gov