01 / 22
mod n

Modular Arithmetic
Operations & Congruences

Master the clock arithmetic that powers cryptography, computer science, and number theory: from congruence notation to modular operations.

Discrete Math

What Is Modular Arithmetic?

Modular arithmetic is a system where integers "wrap around" after reaching a fixed value called the modulus. Think of a clock: after \( 12 \), we go back to \( 1 \), not \( 13 \).

Formally, \( a \equiv b \pmod{n} \) means \( n \mid (a - b) \): the modulus \( n \) divides the difference \( a - b \) evenly.

The Clock Analogy

0 1 2 3 4 5 6 7 8 9 10 11 mod 12 clock arithmetic

Why Modular Arithmetic Matters

Cryptography
RSA encryption relies on modular exponentiation with large primes
Computer Science
Hash functions, checksums, and circular buffers all use mod operations
Number Theory
Divisibility tests, prime analysis, and Diophantine equations

From checking credit card numbers (Luhn algorithm) to securing online banking, modular arithmetic is everywhere in the modern world.

Formal Congruence Definition

1
Equivalent: \( a \) and \( b \) leave the same remainder when divided by \( n \).
2
Example: \( 17 \equiv 5 \pmod{4} \) because \( 17 - 5 = 12 = 3 \times 4 \).
3
The modulus \( n \) must be a positive integer (typically \( n \ge 2 \)).

Properties of Congruence

Congruence modulo \( n \) is an equivalence relation satisfying three properties:

1
Reflexive: \( a \equiv a \pmod{n} \) for all integers \( a \).
2
Symmetric: If \( a \equiv b \pmod{n} \), then \( b \equiv a \pmod{n} \).
3
Transitive: If \( a \equiv b \) and \( b \equiv c \pmod{n} \), then \( a \equiv c \pmod{n} \).

Arithmetic Operations mod \( n \)

If \( a \equiv b \pmod{n} \) and \( c \equiv d \pmod{n} \), then:

Addition
\( a + c \equiv b + d \pmod{n} \)
Subtraction
\( a - c \equiv b - d \pmod{n} \)
Multiplication
\( a \cdot c \equiv b \cdot d \pmod{n} \)

Modular Exponentiation

This lets us reduce the base before raising to a power, avoiding enormous numbers:

1
To compute \( 7^{100} \pmod{5} \): note \( 7 \equiv 2 \pmod{5} \).
2
\( 2^2 = 4 \equiv -1 \pmod{5} \), so \( 2^{100} = (2^2)^{50} \equiv (-1)^{50} \pmod{5} \).
3
\( (-1)^{50} = 1 \), so \( 7^{100} \equiv 1 \pmod{5} \).

Operator vs Relation

Students often confuse two uses of "mod." They are related but fundamentally different:

\( a \bmod n \) (Operator) \( a \equiv b \pmod{n} \) (Relation)
Type Returns a number Returns true or false
Example \( 17 \bmod 5 = 2 \) \( 17 \equiv 2 \pmod{5} \)
Range \( 0 \le r < n \) Both sides: any integer
Use case Computing remainders Proving relationships

The Division Caveat

Addition, subtraction, and multiplication work seamlessly. But division is dangerous:

!
Safe cancellation: \( ac \equiv bc \pmod{n} \) implies \( a \equiv b \pmod{n / \gcd(c,n)} \).
✓
When \( \gcd(c, n) = 1 \): Cancel freely, or multiply by the modular inverse \( c^{-1} \).

How to Compute Step by Step

1
Identify the modulus \( n \). All results live in \( \{0, 1, \ldots, n-1\} \).
2
Reduce each operand mod \( n \) first. Replace large numbers with their remainders.
3
Perform the operation (add, subtract, multiply, or exponentiate) on reduced values.
4
Reduce the result mod \( n \) again. The answer is always in \( \{0, 1, \ldots, n-1\} \).
5
For large exponents: Use repeated squaring: decompose the exponent into powers of 2.

Example: Addition & Multiplication

Compute \( (23 + 19) \cdot 7 \pmod{10} \).

1
Reduce: \( 23 \equiv 3 \), \( 19 \equiv 9 \), \( 7 \equiv 7 \pmod{10} \).
2
Add: \( 3 + 9 = 12 \equiv 2 \pmod{10} \).
3
Multiply: \( 2 \cdot 7 = 14 \equiv 4 \pmod{10} \).

Example: Repeated Squaring

Compute \( 3^{13} \pmod{7} \) using repeated squaring.

1
Square up: \( 3^1 \equiv 3 \), \( 3^2 \equiv 2 \), \( 3^4 \equiv 4 \), \( 3^8 \equiv 2 \pmod{7} \).
2
Decompose: \( 13 = 8 + 4 + 1 \).
3
Combine: \( 3^{13} = 3^8 \cdot 3^4 \cdot 3^1 \equiv 2 \cdot 4 \cdot 3 = 24 \equiv 3 \pmod{7} \).

Example: Negative Numbers mod \( n \)

What is \( -17 \bmod 5 \)?

1
Divide: \( -17 \div 5 = -3.4 \). Take the floor: \( \lfloor -3.4 \rfloor = -4 \).
2
Remainder: \( -17 - (-4)(5) = -17 + 20 = 3 \).
3
Result: \( -17 \equiv 3 \pmod{5} \). The remainder is always non-negative.

Residue Classes mod \( 5 \)

\[[0]\]
...−10 −5 0 5 10 15...
\[[1]\]
...−9 −4 1 6 11 16...
\[[2]\]
...−8 −3 2 7 12 17...
\[[3]\]
...−7 −2 3 8 13 18...
\[[4]\]
...−6 −1 4 9 14 19...

Common Mistakes

✕ Dividing Both Sides
Cancelling a factor without checking \( \gcd(c, n) = 1 \) produces wrong results.
✕ Negative Remainders
Writing \( -3 \bmod 7 = -3 \) instead of \( 4 \). Always convert to \( [0, n) \).
✕ Mixing \( = \) and \( \equiv \)
Using \( = \) for relations (e.g., \( 17 = 2 \pmod{5} \)) is incorrect. Use \( \equiv \) for relations and \( = \) for operator results (e.g., \( 17 \bmod 5 = 2 \)).

Summary of Operations

Operation Rule Condition
Addition\( (a + b) \bmod n \)Always valid
Subtraction\( (a - b) \bmod n \)Always valid
Multiplication\( (a \cdot b) \bmod n \)Always valid
Exponentiation\( a^k \bmod n \)Always valid
Division\( a \cdot b^{-1} \bmod n \)\( \gcd(b, n) = 1 \)

Question 1 of 5

What is \( 47 \bmod 7 \)?

Question 2 of 5

If \( a \equiv 3 \pmod{7} \) and \( b \equiv 5 \pmod{7} \), what is \( a \cdot b \pmod{7} \)?

Question 3 of 5

What is \( -11 \bmod 4 \)?

Question 4 of 5

Which statement about dividing in modular arithmetic is correct?

Question 5 of 5

Compute \( 2^{10} \pmod{11} \).