Fiveable

🧠Thinking Like a Mathematician Unit 3 Review

QR code for Thinking Like a Mathematician practice questions

3.5 Modular arithmetic

3.5 Modular arithmetic

Written by the Fiveable Content Team • Last updated August 2025
Written by the Fiveable Content Team • Last updated August 2025
🧠Thinking Like a Mathematician
Unit & Topic Study Guides

Modular arithmetic is a system where integers "wrap around" after reaching a fixed value called the modulus. It's foundational to number theory and abstract algebra, and it shows up constantly in cryptography, computer science, and even everyday tasks like reading a clock.

The core notation is ab(modm)a \equiv b \pmod{m}, read as "a is congruent to b modulo m." This means aa and bb leave the same remainder when divided by mm. The system works with both positive and negative integers, and it divides all integers into neat groups called equivalence classes based on those remainders.

Fundamentals of modular arithmetic

Definition and notation

Modular arithmetic operates on a finite set of integers that "wrap around" after reaching a specified value called the modulus. Think of a clock: after 12, you don't go to 13, you go back to 1. That's mod 12 in action.

The notation ab(modm)a \equiv b \pmod{m} means that mm divides the difference aba - b. For example, 172(mod5)17 \equiv 2 \pmod{5} because 172=1517 - 2 = 15, and 55 divides 1515 evenly.

  • Integers are grouped into equivalence classes based on their remainders when divided by the modulus
  • Negative integers work too: 34(mod7)-3 \equiv 4 \pmod{7} because 34=7-3 - 4 = -7, which is divisible by 77

Congruence relation

Two integers are congruent modulo mm when they share the same remainder upon division by mm. This congruence relation is actually an equivalence relation because it satisfies three properties:

  • Reflexive: aa(modm)a \equiv a \pmod{m} for any integer aa
  • Symmetric: if ab(modm)a \equiv b \pmod{m}, then ba(modm)b \equiv a \pmod{m}
  • Transitive: if ab(modm)a \equiv b \pmod{m} and bc(modm)b \equiv c \pmod{m}, then ac(modm)a \equiv c \pmod{m}

Because congruence is an equivalence relation, it preserves additive and multiplicative structure. You can add, subtract, or multiply both sides of a congruence and the relationship holds.

Modular equivalence classes

An equivalence class collects all integers that are congruent to each other modulo mm. Formally:

[a]m={xZ:xa(modm)}[a]_m = \{x \in \mathbb{Z} : x \equiv a \pmod{m}\}

For mod 3, the equivalence classes are [0]3={,6,3,0,3,6,}[0]_3 = \{\ldots, -6, -3, 0, 3, 6, \ldots\}, [1]3={,5,2,1,4,7,}[1]_3 = \{\ldots, -5, -2, 1, 4, 7, \ldots\}, and [2]3={,4,1,2,5,8,}[2]_3 = \{\ldots, -4, -1, 2, 5, 8, \ldots\}.

  • Each class contains infinitely many integers
  • The classes partition the integers: every integer belongs to exactly one class, and no two classes overlap
  • You can pick any element from a class as its representative, which is what makes modular arithmetic so useful for simplifying calculations

Properties of modular arithmetic

Modular arithmetic shares many algebraic properties with standard arithmetic. These properties give the system a predictable structure that's essential for proofs and applications.

Closure property

Performing addition or multiplication on elements within a modular system always produces a result in the same system.

  • Addition: (a+b)modm(a + b) \bmod m always falls in the range [0,m1][0, m-1]
  • Multiplication: (a×b)modm(a \times b) \bmod m also stays within [0,m1][0, m-1]

This guarantees that you never "leave" the system when computing, which is critical for applications like cryptography where you need results to stay within a fixed range.

Associative and commutative properties

Both associativity and commutativity hold for addition and multiplication:

  • Associativity:
    • ((a+b)+c)(a+(b+c))(modm)((a + b) + c) \equiv (a + (b + c)) \pmod{m}
    • ((a×b)×c)(a×(b×c))(modm)((a \times b) \times c) \equiv (a \times (b \times c)) \pmod{m}
  • Commutativity:
    • (a+b)(b+a)(modm)(a + b) \equiv (b + a) \pmod{m}
    • (a×b)(b×a)(modm)(a \times b) \equiv (b \times a) \pmod{m}

These let you rearrange terms freely in modular expressions, which is what makes techniques like fast modular exponentiation (square-and-multiply) possible.

Distributive property

Multiplication distributes over addition:

a×(b+c)(a×b)+(a×c)(modm)a \times (b + c) \equiv (a \times b) + (a \times c) \pmod{m}

This works just like in standard arithmetic and lets you expand or factor modular expressions. It applies to both positive and negative integers within the system.

Identity elements

  • Additive identity: 00, since a+0a(modm)a + 0 \equiv a \pmod{m} for all aa
  • Multiplicative identity: 11, since a×1a(modm)a \times 1 \equiv a \pmod{m} for all aa

These identities behave the same regardless of the modulus and are essential for defining inverses.

Operations in modular arithmetic

Addition and subtraction

The key rule for modular addition: you can reduce each operand before adding.

(a+b)modm=((amodm)+(bmodm))modm(a + b) \bmod m = ((a \bmod m) + (b \bmod m)) \bmod m

Subtraction works by adding the additive inverse. The additive inverse of aa modulo mm is the number bb where a+b0(modm)a + b \equiv 0 \pmod{m}. For example, the additive inverse of 33 mod 77 is 44, because 3+4=70(mod7)3 + 4 = 7 \equiv 0 \pmod{7}.

Results always fall within [0,m1][0, m-1], preserving the cyclic structure. Everyday examples include calculating what day of the week it'll be 10 days from now (mod 7 arithmetic).

Definition and notation, LinearAlgebraMod | Wolfram Function Repository

Multiplication

Same reduce-first principle applies:

$$$(a \times b) \bmod m = ((a \bmod m) \times (b \bmod m)) \bmod m$$

This is extremely useful when working with large numbers. For instance, to compute 47×63(mod5)47 \times 63 \pmod{5}, you can first reduce: 47247 \equiv 2 and 633(mod5)63 \equiv 3 \pmod{5}, so the answer is 2×3=61(mod5)2 \times 3 = 6 \equiv 1 \pmod{5}. Much easier than multiplying 2961 and then dividing.

Modular multiplication forms the backbone of many cryptographic algorithms, including RSA encryption.

Exponentiation

Modular exponentiation computes abmodma^b \bmod m. For large exponents, you don't compute aba^b first and then reduce. Instead, the square-and-multiply algorithm breaks the exponent into binary and repeatedly squares and reduces:

  1. Write the exponent bb in binary
  2. Start with result = 1
  3. For each bit (left to right): square the result and reduce mod mm; if the bit is 1, also multiply by aa and reduce mod mm

This keeps intermediate values small and runs in O(logb)O(\log b) multiplications, making it feasible to compute things like 7256mod137^{256} \bmod 13 without ever handling astronomically large numbers.

Fermat's Little Theorem (covered below) provides another shortcut for modular exponentiation when the modulus is prime.

Division and multiplicative inverses

Division in modular arithmetic is trickier than the other operations. You can only "divide" by aa modulo mm when gcd(a,m)=1\gcd(a, m) = 1 (i.e., aa and mm are coprime).

The multiplicative inverse of aa modulo mm is a number bb such that ab1(modm)ab \equiv 1 \pmod{m}. For example, the inverse of 33 mod 77 is 55, because 3×5=151(mod7)3 \times 5 = 15 \equiv 1 \pmod{7}.

  • Not every number has a multiplicative inverse. For instance, 22 has no inverse mod 66 because gcd(2,6)=21\gcd(2, 6) = 2 \neq 1
  • When the inverse exists, it can be found using the extended Euclidean algorithm
  • Multiplicative inverses are essential for solving linear congruences and implementing cryptographic protocols

Applications of modular arithmetic

Cryptography basics

Modular arithmetic is the mathematical engine behind public-key cryptography. In RSA, for example, encryption and decryption both rely on modular exponentiation with very large numbers (hundreds of digits). The security comes from the fact that certain modular operations are easy to perform but extremely hard to reverse, like factoring the product of two large primes.

The Diffie-Hellman key exchange also depends on modular exponentiation, allowing two parties to establish a shared secret over an insecure channel.

Error detection in coding

  • ISBN validation: The check digit of a 13-digit ISBN is computed using mod 10 arithmetic, catching common transcription errors
  • Credit card numbers: The Luhn algorithm uses mod 10 to verify that a card number hasn't been mistyped
  • Cyclic redundancy checks (CRC): These treat data as polynomials and use modular division to detect transmission errors

Calendar systems

Modular arithmetic is natural for anything cyclical. Days of the week repeat every 7 days, so day-of-week calculations are mod 7.

Zeller's congruence is a formula that uses modular arithmetic to determine the day of the week for any date in history. Leap year rules also involve modular checks: a year is a leap year if it's divisible by 4, except for years divisible by 100, unless they're also divisible by 400.

Computer science applications

  • Hash functions often use mod to map data to a fixed range of indices (e.g., index = key mod table_size)
  • Memory addressing uses modular arithmetic for circular buffers and wrap-around allocation
  • Pseudorandom number generators frequently use linear congruential methods: xn+1=(axn+c)modmx_{n+1} = (ax_n + c) \bmod m

Solving modular equations

Linear congruences

A linear congruence has the form axb(modm)ax \equiv b \pmod{m}. Here's how to determine if it's solvable and find the solutions:

  1. Compute d=gcd(a,m)d = \gcd(a, m)
  2. If dd does not divide bb, there is no solution
  3. If dd divides bb, there are exactly dd solutions modulo mm
  4. Divide through by dd: solve adxbd(modmd)\frac{a}{d}x \equiv \frac{b}{d} \pmod{\frac{m}{d}}
  5. Find the multiplicative inverse of ad\frac{a}{d} modulo md\frac{m}{d} (using the extended Euclidean algorithm) and multiply both sides by it

Example: Solve 6x4(mod10)6x \equiv 4 \pmod{10}. Here gcd(6,10)=2\gcd(6, 10) = 2, and 22 divides 44, so solutions exist. Dividing through: 3x2(mod5)3x \equiv 2 \pmod{5}. The inverse of 33 mod 55 is 22 (since 3×2=613 \times 2 = 6 \equiv 1), so x4(mod5)x \equiv 4 \pmod{5}. The two solutions mod 10 are x=4x = 4 and x=9x = 9.

Definition and notation, Permutation group - Knowino

Chinese remainder theorem

The Chinese Remainder Theorem (CRT) solves systems of simultaneous congruences when the moduli are pairwise coprime:

xa1(modm1),xa2(modm2),,xak(modmk)x \equiv a_1 \pmod{m_1}, \quad x \equiv a_2 \pmod{m_2}, \quad \ldots, \quad x \equiv a_k \pmod{m_k}

The theorem guarantees a unique solution modulo m1×m2××mkm_1 \times m_2 \times \cdots \times m_k.

Example: Find xx such that x2(mod3)x \equiv 2 \pmod{3} and x3(mod5)x \equiv 3 \pmod{5}. Testing values: x=8x = 8 works (8=2×3+28 = 2 \times 3 + 2 and 8=1×5+38 = 1 \times 5 + 3). The unique solution mod 15 is x8(mod15)x \equiv 8 \pmod{15}.

CRT is used in secret sharing schemes, fast Fourier transforms, and for breaking large computations into smaller parallel ones.

Fermat's little theorem

If pp is prime and aa is not divisible by pp, then:

ap11(modp)a^{p-1} \equiv 1 \pmod{p}

For example, 26=641(mod7)2^6 = 64 \equiv 1 \pmod{7} (since 77 is prime and 727 \nmid 2).

This is powerful for simplifying large exponents. To compute 2100(mod7)2^{100} \pmod{7}: since 261(mod7)2^6 \equiv 1 \pmod{7}, write 2100=(26)1624116162(mod7)2^{100} = (2^6)^{16} \cdot 2^4 \equiv 1^{16} \cdot 16 \equiv 2 \pmod{7}.

Fermat's Little Theorem generalizes to Euler's theorem for composite moduli: aϕ(m)1(modm)a^{\phi(m)} \equiv 1 \pmod{m} when gcd(a,m)=1\gcd(a, m) = 1, where ϕ(m)\phi(m) is Euler's totient function. Both results are fundamental to primality testing and RSA cryptography.

Modular arithmetic vs standard arithmetic

Similarities and differences

Both systems share the core algebraic properties: associativity, commutativity, and distributivity all hold. The key differences:

  • Standard arithmetic works with the infinite set of integers; modular arithmetic works with a finite set {0,1,2,,m1}\{0, 1, 2, \ldots, m-1\}
  • Division is always defined (except by zero) in standard arithmetic, but in modular arithmetic it's only defined when the divisor is coprime to the modulus
  • Standard integers have a natural ordering (3<5<73 < 5 < 7); in modular arithmetic, "less than" and "greater than" don't carry meaningful information
  • Modular arithmetic often produces more compact representations, which is why it's so useful computationally

Cyclic nature of modular systems

The defining feature of modular arithmetic is its wrap-around behavior. After m1m - 1, the next value is 00 again. This creates a cyclic structure, like the hours on a clock.

This cyclic property introduces concepts that don't exist in standard arithmetic, like the order of an element: the smallest positive integer kk such that ak1(modm)a^k \equiv 1 \pmod{m}. It also makes modular arithmetic the natural tool for modeling anything periodic.

Limitations of modular arithmetic

  • Not all elements have multiplicative inverses, so division is restricted
  • There's no meaningful way to say one residue is "bigger" than another
  • Only integers are represented; irrational and real numbers don't fit into the system
  • Reducing a number mod mm loses information: both 55 and 1212 become 55 mod 77, and you can't recover which original value you started with
  • Choosing the wrong modulus can cause collisions (distinct values mapping to the same residue), which matters in applications like hashing

Advanced concepts

Primitive roots

A primitive root modulo nn is an integer gg whose powers generate every element coprime to nn. Formally, gg is a primitive root mod nn if the set {g1,g2,,gϕ(n)}\{g^1, g^2, \ldots, g^{\phi(n)}\} modulo nn gives all integers from 11 to n1n-1 that are coprime to nn.

  • Primitive roots exist for all prime moduli, but not for all composite moduli. Specifically, they exist for moduli of the form 1,2,4,pk,1, 2, 4, p^k, and 2pk2p^k where pp is an odd prime.
  • The number of primitive roots mod pp (for prime pp) is ϕ(p1)\phi(p-1)
  • Primitive roots are central to the Diffie-Hellman key exchange and discrete logarithm problems

Quadratic residues

An integer aa is a quadratic residue modulo nn if the equation x2a(modn)x^2 \equiv a \pmod{n} has a solution. If no solution exists, aa is a quadratic non-residue.

For a prime pp, exactly half of the nonzero residues are quadratic residues and half are non-residues. The Legendre symbol (ap)\left(\frac{a}{p}\right) encodes this: it equals 11 if aa is a QR mod pp, 1-1 if not, and 00 if pap \mid a.

Quadratic residues connect to deep results like the law of quadratic reciprocity and have applications in primality testing, factorization, and probabilistic encryption.

Modular exponentiation algorithms

Efficient modular exponentiation is critical for practical cryptography, where exponents can be hundreds of digits long.

  • Binary exponentiation (square-and-multiply): Processes the exponent bit by bit, requiring at most 2log2(b)2\log_2(b) modular multiplications to compute abmodma^b \bmod m
  • Montgomery multiplication: Replaces expensive division operations with cheaper bit-shift operations by working in a special "Montgomery form," making repeated modular multiplications faster
  • These algorithms are what make RSA and similar systems computationally feasible, and ongoing research continues to optimize them for specific hardware