2.5.6Number Theory (Intermediate)

Modular arithmetic — definition, addition, multiplication, congruence

2,656 words12 min readdifficulty · medium6 backlinks

Overview

Modular arithmetic is arithmetic that "wraps around" after reaching a certain value called the modulus. It's the mathematical foundation of clock arithmetic, cyclic patterns, cryptography, and computer science hash functions.

Figure — Modular arithmetic — definition, addition, multiplication, congruence

Core Concepts

Instead of numbers extending infinitely, we partition them into equivalence classes based on their remainder when divided by a fixed number (the modulus). This lets us:

  • Simplify calculations with large numbers (cryptography uses this!)
  • Find patterns in divisibility
  • Work with cyclic structures (days of week, months, rotations)

Key insight: Two numbers are "the same" in modular arithmetic if they leave the same remainder when divided by the modulus.


Equivalent definition: ab(modn)a \equiv b \pmod{n} if and only if aa and bb leave the same remainder when divided by nn.

What does this mean? The difference between aa and bb is a multiple of nn. They're in the same "bucket" when we divide integers into groups based on remainder.


From First Principles:

1. Addition: If ab(modn)a \equiv b \pmod{n} and cd(modn)c \equiv d \pmod{n}, then: a+cb+d(modn)a + c \equiv b + d \pmod{n}

Why?

  • We know ab=k1na - b = k_1n and cd=k2nc - d = k_2n for some integers k1,k2k_1, k_2
  • Add these: (ab)+(cd)=k1n+k2n(a - b) + (c - d) = k_1n + k_2n
  • Rearrange: (a+c)(b+d)=(k1+k2)n(a + c) - (b + d) = (k_1 + k_2)n
  • Since k1+k2k_1 + k_2 is an integer, (a+c)(b+d)(modn)(a+c) \equiv (b+d) \pmod{n}

2. Multiplication: If ab(modn)a \equiv b \pmod{n} and cd(modn)c \equiv d \pmod{n}, then: acbd(modn)ac \equiv bd \pmod{n}

Why?

  • We have a=b+k1na = b + k_1n and c=d+k2nc = d + k_2n
  • Multiply: ac=(b+k1n)(d+k2n)ac = (b + k_1n)(d + k_2n)
  • Expand: ac=bd+bk2n+dk1n+k1k2n2ac = bd + bk_2n + dk_1n + k_1k_2n^2
  • Factor: ac=bd+n(bk2+dk1+k1k2n)ac = bd + n(bk_2 + dk_1 + k_1k_2n)
  • Therefore: acbd=n(integer)ac - bd = n \cdot (\text{integer}), so acbd(modn)ac \equiv bd \pmod{n}

3. Power Rule: If ab(modn)a \equiv b \pmod{n}, then: akbk(modn)a^k \equiv b^k \pmod{n}

Why? Apply the multiplication rule repeatedly kk times.


Solution:

  • Method 1 (Difference): 238=15=3×523 - 8 = 15 = 3 \times 5. Since the difference is a multiple of 5, yes.
  • Method 2 (Remainder): 23=4×5+323 = 4 \times 5 + 3 (remainder 3), 8=1×5+38 = 1 \times 5 + 3 (remainder 3). Same remainder, yes.

Why this matters: Both numbers are in the "remainder 3" equivalence class when working mod 5.


Solution:

  • Step 1: Reduce each number first

    • 17=2×7+317 = 2 \times 7 + 3, so 173(mod7)17 \equiv 3 \pmod{7}
    • 23=3×7+223 = 3 \times 7 + 2, so 232(mod7)23 \equiv 2 \pmod{7}
    • Why this step? Smaller numbers are easier to work with, and we can reduce before operating.
  • Step 2: Add the reduced numbers

    • 3+2=53 + 2 = 5
    • Why this step? By the addition rule, (17+23)(3+2)(mod7)(17 + 23) \equiv (3 + 2) \pmod{7}
  • Step 3: Result

    • 5<75 < 7, so 55 is already in standard form
    • Why this step? We want the answer in the range [0,n1][0, n-1].

Answer: (17+23)5(mod7)(17 + 23) \equiv 5 \pmod{7}

Verification: 17+23=40=5×7+517 + 23 = 40 = 5 \times 7 + 5. Remainder is 5. ✓


Solution:

  • Step 1: Reduce each factor

    • 13=2×5+313 = 2 \times 5 + 3, so 133(mod5)13 \equiv 3 \pmod{5}
    • 8=1×5+38 = 1 \times 5 + 3, so 83(mod5)8 \equiv 3 \pmod{5}
    • Why this step? Working with smaller numbers prevents overflow and simplifies calculation.
  • Step 2: Multiply reduced numbers

    • 3×3=93 \times 3 = 9
    • Why this step? By multiplication rule, (13×8)(3×3)(mod5)(13 \times 8) \equiv (3 \times 3) \pmod{5}
  • Step 3: Reduce the result

    • 9=1×5+49 = 1 \times 5 + 4, so 94(mod5)9 \equiv 4 \pmod{5}
    • Why this step? Final answer should be in standard form [0,4][0, 4].

Answer: (13×8)4(mod5)(13 \times 8) \equiv 4 \pmod{5}

Verification: 13×8=104=20×5+413 \times 8 = 104 = 20 \times 5 + 4. Remainder is 4. ✓


Solution:

  • Step 1: Reduce the base

    • 7=1×6+17 = 1 \times 6 + 1, so 71(mod6)7 \equiv 1 \pmod{6}
    • Why this step? If the base reduces to something simple, the power becomes trivial.
  • Step 2: Apply power rule

    • 71001100(mod6)7^{100} \equiv 1^{100} \pmod{6}
    • Why this step? By the power rule, congruence is preserved under exponentiation.
  • Step 3: Evaluate

    • 1100=11^{100} = 1
    • Why this step? Any power of 1 is 1.

Answer: 71001(mod6)7^{100} \equiv 1 \pmod{6}

Key insight: Reducing the base first can make impossible-looking calculations trivial!


Why it feels right: This actually works! But for large numbers (like 1234567+9876543(mod7)1234567 + 9876543 \pmod{7}), calculating the full sum first can cause overflow or unnecessary complexity.

The fix: Reduce each operand first, then operate. This is always valid by the addition/multiplication rules, and it's more efficient.

Steel-man: The "calculate then reduce" approach is mathematically correct, just computationally inefficient. The modular arithmetic rules exist precisely to let us reduce during calculation.


Why it feels right: In regular arithmetic, we can divide both sides by the same number.

The fix: Division in modular arithmetic is not always defined. You can only divide by aa if gcd(a,n)=1\gcd(a, n) = 1 (i.e., aa and nn are coprime). Here, gcd(6,15)=31\gcd(6, 15) = 3 \neq 1, so we can't divide by 6.

Correct approach:

  • Notice gcd(6,15)=3\gcd(6, 15) = 3 divides both 6 and 9
  • Simplify: 6x9(mod15)6x \equiv 9 \pmod{15} becomes 2x3(mod5)2x \equiv 3 \pmod{5}
  • Now we can solve: multiply both sides by the modular inverse of 2 5

Steel-man: The intuition from regular algebra is powerful, but modular arithmetic has different rules. Always check if division is valid by checking the gcd.


Why it feels right: In programming, some languages return negative remainders.

The fix: In mathematics, we always want the result in [0,n1][0, n-1]. Convert negative numbers: 33+74(mod7)-3 \equiv -3 + 7 \equiv 4 \pmod{7}

General rule: If you get a negative result rr, add multiples of nn until you reach [0,n1][0, n-1]: rr+kn(modn)r \equiv r + kn \pmod{n} where kk is chosen so the result is non-negative.


Recall Explain to a 12-year-old

Imagine you have a circular track with 12 positions, numbered 0through 11 (like a clock face). You start at position 0. If you take 5 steps forward, you're at position 5. If you take 8 more steps, you'd be at 13... but wait, there's no position 13! You go around: after position 11 comes position 0 again. So 13 is the same as position 1(because 13 - 12 = 1).

This is modular arithmetic! The "12" is called the modulus—it's when you wrap around. Two positions are the "same" if they're the same spot on the circle.

When we write 131(mod12)13 \equiv 1 \pmod{12}, we're saying "13 and 1 are the same position on a12-hour clock."

The cool part: you can add and multiply on this circular track, and everything still works! If you're at position 5 and a friend is at position 8, together you've gone 5+8=135 + 8 = 13 steps, which is position 1. If you go around the track 5 times, taking 8 steps each time, you go 5×8=405 \times 8 = 40 steps total, which lands you at position 4 (because 40=3×12+440 = 3 \times 12 + 4).


Reminder: ab(modn)a \equiv b \pmod{n} means "aa and bb show the same time on an nn-hour clock."


Properties of Congruence

Congruence modulo nn is an equivalence relation, meaning:

  1. Reflexive: aa(modn)a \equiv a \pmod{n} (every number is congruent to itself)
  2. Symmetric: If ab(modn)a \equiv b \pmod{n}, then ba(modn)b \equiv a \pmod{n}
  3. Transitive: If ab(modn)a \equiv b \pmod{n} and bc(modn)b \equiv c \pmod{n}, then ac(modn)a \equiv c \pmod{n}

Why this matters: These properties mean congruence partitions the integers into distinct equivalence classes (called residue classes). Every integer belongs to exactly one class.

For modulus nn, there are exactly nn residue classes: [0],[1],[2],,[n1][0], [1], [2], \ldots, [n-1].


The Residue Classes

For modulus 5, the residue classes are:

  • [0]={,10,5,0,5,10,15,}[0] = \{\ldots, -10, -5, 0, 5, 10, 15, \ldots\}
  • [1]={,9,4,1,6,11,16,}[1] = \{\ldots, -9, -4, 1, 6, 11, 16, \ldots\}
  • [2]={,8,3,2,7,12,17,}[2] = \{\ldots, -8, -3, 2, 7, 12, 17, \ldots\}
  • [3]={,7,2,3,8,13,18,}[3] = \{\ldots, -7, -2, 3, 8, 13, 18, \ldots\}
  • [4]={,6,1,4,9,14,19,}[4] = \{\ldots, -6, -1, 4, 9, 14, 19, \ldots\}

Every integer is in exactly one of these classes. We write Z5={0,1,2,3,4}\mathbb{Z}_5 = \{0, 1, 2, 3, 4\} for the set of residue classes mod 5.


Connections

  • Division Algorithm — the foundation of remainders
  • GCD and LCM — determines when modular division is possible
  • Fermats Little Theorem — uses modular exponentiation
  • Eulers Theorem — generalizes Fermat's theorem using modular arithmetic
  • Chinese Remainder Theorem — solves systems of congruences
  • Modular Inverses — division in modular arithmetic
  • RSA Cryptography — built entirely on modular arithmetic with large primes
  • Linear Congruences — solving axb(modn)ax \equiv b \pmod{n}
  • Quadratic Residues — which numbers are perfect squares mod nn

Practice Problems

  1. Verify that 1738(mod7)17 \equiv 38 \pmod{7} using both definitions
  2. Compute (45+67)mod11(45 + 67) \bmod 11 by reducing first
  3. Find (13×17)mod6(13 \times 17) \bmod 6 efficiently
  4. Calculate 950mod89^{50} \bmod 8 (hint: reduce the base first!)
  5. Prove that if ab(modn)a \equiv b \pmod{n}, then a2b2(modn)a^2 \equiv b^2 \pmod{n}
  6. Find all xx such that x21(mod8)x^2 \equiv 1 \pmod{8}

#flashcards/maths

What is the definition of congruence modulo n? :: Two integers aa and bb are congruent modulo nn (written ab(modn)a \equiv b \pmod{n}) if their difference is a multiple of nn, i.e., ab=kna - b = kn for some integer kk.

What is the equivalent definition of congruence using remainders?
ab(modn)a \equiv b \pmod{n} if and only if aa and bb leave the same remainder when divided by nn.

State the addition rule in modular arithmetic :: If ab(modn)a \equiv b \pmod{n} and cd(modn)c \equiv d \pmod{n}, then a+cb+d(modn)a + c \equiv b + d \pmod{n}.

State the multiplication rule in modular arithmetic
If ab(modn)a \equiv b \pmod{n} and cd(modn)c \equiv d \pmod{n}, then acbd(modn)ac \equiv bd \pmod{n}.

Why can we reduce numbers before operating in modular arithmetic? :: Because the addition and multiplication rules guarantee that (amodn)+(bmodn)(a+b)(modn)(a \bmod n) + (b \bmod n) \equiv (a+b) \pmod{n}, and similarly for multiplication. This makes calculations more efficient.

What is the danger with division in modular arithmetic?
Division is not always defined. You can only "divide" (multiply by a modular inverse) when the divisor and modulus are coprime, i.e., gcd(a,n)=1\gcd(a, n) = 1.

How do you handle negative numbers in modular arithmetic? :: Add multiples of the modulus until the result is in the range [0,n1][0, n-1]. For example, 34(mod7)-3 \equiv 4 \pmod{7} because 3+7=4-3 + 7 = 4.

What are the three properties that make congruence an equivalence relation?
Reflexive (aa(modn)a \equiv a \pmod{n}), Symmetric (if aba \equiv b then bab \equiv a), and Transitive (if aba \equiv b and bcb \equiv c then aca \equiv c).
How many residue classes exist for modulus n?
Exactly nn residue classes: [0],[1],[2],,[n1][0], [1], [2], \ldots, [n-1].

What is the clock face analogy for modular arithmetic? :: Modular arithmetic works like a clock: after reaching the modulus value, you wrap around to 0. For mod 12, after 11 comes 0 again. Two numbers are congruent if they point to the same position on the clock.

Quick calculation: what is 25mod725 \bmod 7?
25=3×7+425 = 3 \times 7 + 4, so 254(mod7)25 \equiv 4 \pmod{7}.
Quick calculation: what is (15×16)mod5(15 \times 16) \bmod 5?
150(mod5)15 \equiv 0 \pmod{5}, so (15×16)(0×16)0(mod5)(15 \times 16) \equiv (0 \times 16) \equiv 0 \pmod{5}.

Concept Map

wraps around at

division gives

groups numbers into

defined by

same

formalized as

preserved under

preserved under

repeated to get

enables

Modular Arithmetic

Modulus n

Remainder

Equivalence Classes

Congruence a equiv b mod n

a minus b equals kn

Addition Rule

Multiplication Rule

Power Rule

Applications cryptography, clocks

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Modular arithmetic ek aisa concept hai jahan numbersek certain value (modulus) ke bad wrap around ho jate hain, bilkul clock ki tarah. Jaise clock mein 12 ke baad fir se 1 aa jata hai, waise hi modular arithmetic mein bhi ek limit ke baad numbers cycle repeat karte hain.

Iska sabse important idea hai congruence: do numbers congruent hain (matlab same equivalence class mein hain) agar unka difference modulus ka multiple ho. For example, 17 aur 8 dono modulo 5 mein congruent hain kyunki 17 - 8 = 9, jo 5 ka multiple nahi hai... wait, 9/5 = 1 remainder 4, toh actually 17 aur 8 congruent nahi hain! Sahi example: 17 aur 12 congruent hain mod 5 kyunki 17 - 12 = 5. Yeh bhi dekh sakte hain remainders se: 17 ÷ 5 gives remainder 2, aur 12 ÷ 5 bhi remainder 2 deta hai.

Addition aur multiplication modular arithmetic mein behave karte hain waise hi jaise normal arithmetic mein, bas har step pe hum modulus se reduce kar sakte hain. Agar tum bade numbers ke sath kaam kar rahe ho, toh pehle unhe reduce karo, phir operate karo – yeh much easier aur efficient hai. Lekin dhyan rakhna, division modular arithmetic mein complicated hai – sirf tab divide kar sakte ho jab divisor aur modulus coprime hon (GCD = 1). Yeh concept cryptography mein bohot use hota hai, jahan hum bade numbers ke saath securely kaam karte hain bina actually unhe fully compute kiye.

Go deeper — visual, from zero

Test yourself — Number Theory (Intermediate)

Connections