MathCompass

Modular Arithmetic I: Basics

Number Theory Intermediate

πŸ“‹ Prerequisites

Modular arithmetic is "clock math" β€” numbers wrap around after reaching a certain value called the modulus. It's the single most powerful tool in number theory. Once you think in terms of remainders, divisibility rules, last-digit problems, and cyclic patterns all become trivial. Master the basics here, then we'll go deep.

πŸ“š Key Concepts

Congruence Definition

We say $a$ is congruent to $b$ modulo $m$ if $m$ divides $(a - b)$.

\[ a \equiv b \pmod{m} \quad \Longleftrightarrow \quad m \mid (a - b) \]

Intuitively: a and b leave the same remainder when divided by m.

Examples:

Basic Arithmetic with Congruences

If $ a \equiv b \pmod{m} $ and $ c \equiv d \pmod{m} $, then:

Important: Division doesn't always work! You can only divide both sides by k if gcd(k, m) = 1. We'll cover modular inverses for this.

Reducing Modulo m

Any integer can be reduced to its residue β€” the unique remainder r where $ 0 \leq r < m $.

To find a mod m: divide by m, take the positive remainder.

For negative numbers: add multiples of m until you get into [0, m).

Example: -3 mod 7 = -3 + 7 = 4. So $ -3 \equiv 4 \pmod{7} $.

Last Digit Problems (mod 10)

The last digit of a number is just the number mod 10.

Last digits of powers cycle! For example, powers of 2:

Cycle length = 4. So \(2^{n}\) last digit depends on n mod 4.

Why Divisibility Rules Work

Since $ 10 \equiv 0 \pmod{2} $ and $ 10 \equiv 0 \pmod{5} $, only the last digit matters for divisibility by 2 or 5.

Since $ 10 \equiv 1 \pmod{3} $ and $ 10 \equiv 1 \pmod{9} $, we have $ 10^k \equiv 1^k = 1 \pmod{3} $. So a number equals the sum of its digits mod 3 (and mod 9).

Since $ 10 \equiv -1 \pmod{11} $, we have $ 10^k \equiv (-1)^k \pmod{11} $. This gives the alternating-sum rule for 11.

Modular arithmetic is the proof behind every divisibility rule!

Residue Classes

Modulo m, there are exactly m distinct residue classes: {0, 1, 2, ..., m-1}.

Every integer belongs to exactly one class. Two numbers in the same class are congruent mod m.

The set $ \mathbb{Z}/m\mathbb{Z} = \{0, 1, 2, ..., m-1\} $ is the set of residues mod m.

Modular Exponentiation (Basic)

To compute $ a^n \pmod{m} $ efficiently, you can reduce at each step:

Key idea: you never need to deal with huge numbers. Reduce after every multiplication.

Solving Simple Congruences

$ ax \equiv b \pmod{m} $ has a solution iff gcd(a, m) divides b.

If gcd(a, m) = 1, there's exactly one solution mod m (found via modular inverse).

If gcd(a, m) = d > 1 and d | b, there are d solutions mod m.

If d doesn't divide b, no solution.

⚠️ Common Mistake: You can't just "divide both sides" in modular arithmetic like you do with regular equations. $ 2x \equiv 4 \pmod{6} $ doesn't simplify to $ x \equiv 2 \pmod{6} $ by dividing by 2 β€” actually x = 2 AND x = 5 both work (there are gcd(2,6)=2 solutions). Only divide when the divisor is coprime to the modulus. Also: negative mod requires care. -1 mod 5 = 4, not -1. Always make the result positive.
πŸ’‘ Key Insight: Modular arithmetic turns "is X divisible by Y?" into an equation. Instead of asking "what's the last digit of 3^100?", ask "what's 3^100 mod 10?" β€” and use cycles. The pattern is always: find the cycle length, reduce the exponent mod cycle length, then compute. This works for last digit (mod 10), last two digits (mod 100), divisibility by any number, and more.

✏️ Example Problems

πŸ“ Example 1 (Last digit of a power)

What is the last digit of $3^{25}$?

Last digit = number mod 10. Find the cycle of 3^n mod 10.

Step 1: Compute powers of 3 mod 10.

\((3^{1} = 3 mod 10 \\to\) 3\)

\((3^{2} = 9 mod 10 \\to\) 9\)

\((3^{3} = 27 mod 10 \\to\) 7\)

\((3^{4} = 81 mod 10 \\to\) 1\)

\((3^{5} = 243 mod 10 \\to\) 3 (cycle repeats!)\)

Step 2: Cycle length = 4.

Step 3: Reduce exponent mod cycle length.

25 Γ· \(4 = 6 remainder 1, so 25 \\equiv 1 (mod 4)\)

Step 4: \((3^{2}^\){5} \\equiv 3^{1} \\equiv 3 (mod 10)\)

Last digit is 3.

Answer: 3

πŸ“ Example 2 (Computing a large number mod m)

What is $ 137 \pmod{7} $?

Method 1: Direct division

\(7 \\times 19 = 133\)

137 - 133 = 4

\(So 137 = 7 \\times 19 + 4, remainder = 4.\)

Method 2: Reduce step by step (useful for larger numbers)

\(137 \\to 137 - 70 = 67 \\to 67 - 70 = -3 \\to -3 + 7 = 4\)

Same answer.

Method 3: Digit tricks

\(Since 10 \\equiv 3 mod 7: 137 = 1 \\times 10^{2} + 3 \\times 10 + 7 \\equiv 1 \\times 9 + 3 \\times 3 + 0 \\equiv 9 + 9 \\equiv 18 \\equiv 4 mod 7\) βœ“

Answer: 4

πŸ“ Example 3 (Solving a linear congruence)

Find the smallest positive integer x such that $ 3x \equiv 1 \pmod{7} $.

\(We need 3x \\equiv 1 (mod 7). We're finding the modular inverse of 3 mod 7.\)

Method: Try residues 0 through 6.

\(x = 0: 3 \\times 0 = 0 \\equiv 0 \\neq 1\) βœ—

\(x = 1: 3 \\times 1 = 3 \\equiv 3 \\neq 1\) βœ—

\(x = 2: 3 \\times 2 = 6 \\equiv 6 \\neq 1\) βœ—

\(x = 3: 3 \\times 3 = 9 \\equiv 2 \\neq 1\) βœ—

\(x = 4: 3 \\times 4 = 12 \\equiv 5 \\neq 1\) βœ—

\(x = 5: 3 \\times 5 = 15 \\equiv 1\) βœ“

Step 2: \(Verify: 3 \\times 5 = 15, and 15 - 1 = 14, which is divisible by 7.\) βœ“

x = 5 is the smallest positive solution.

Note: since gcd(3, 7) = 1, there's exactly one solution mod 7.

Answer: 5

Previous
Next