Enter the world of clock arithmetic: congruences, modular operations, and the Chinese Remainder Theorem
Modular arithmetic is arithmetic on a clock: count past the top and you wrap back to zero. That simple idea runs modern cryptography.
Pick an operation and a modulus, and watch the arc travel around the dial. Notice where it wraps past the top back to 0.
Positions 0 to 11 around the clock. Blue = operand a, Gold = result. The arc traces b steps clockwise from a.
Modular arithmetic is the mathematics of remainders. When we say a ≡ b (mod n), we mean a and b leave the same remainder when divided by n. It's like a clock: after 12 comes 1 again.
Far from being a curiosity, modular arithmetic is the foundation of modern cryptography, computer science, and much of algebraic number theory. The integers modulo n form rich algebraic structures that connect directly to group theory.
Visualize modular arithmetic as positions on a clock face. Addition moves you clockwise, and when you pass the modulus, you wrap back around to 0. Multiplication creates interesting patterns of jumps around the dial.
Change the modulus and watch every integer fall into exactly one class. Adding or multiplying any members of two classes always lands in the same class.
Each integer is colored by its residue class mod 5. Numbers with the same color are congruent.
Modular arithmetic partitions the integers into congruence classes. Every integer belongs to exactly one class mod n. This partition is compatible with addition and multiplication — you can compute with classes instead of individual numbers.
Change n and highlight the units: the rows that contain a 1. Notice they are exactly the numbers that share no factor with n.
| × | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
| 2 | 0 | 2 | 4 | 6 | 1 | 3 | 5 |
| 3 | 0 | 3 | 6 | 2 | 5 | 1 | 4 |
| 4 | 0 | 4 | 1 | 5 | 2 | 6 | 3 |
| 5 | 0 | 5 | 3 | 1 | 6 | 4 | 2 |
| 6 | 0 | 6 | 5 | 4 | 3 | 2 | 1 |
Cell colors use HSL hues proportional to the result value. Filter rows/columns by choosing a highlight mode above.
The multiplication table mod n reveals the algebraic structure. The units (elements with multiplicative inverses) form a group under multiplication — this is (Z/nZ)*, a central object in number theory and a direct connection to Group Theory.
Build a system of congruences with coprime moduli and watch the number line light up. The solutions repeat every product of the moduli.
Number line (0–60):
Solutions in range: 8, 23, 38, 53. The pattern repeats every 15 integers.
The CRT guarantees a unique solution modulo the product of pairwise coprime moduli. Highlighted cells on the number line show all solutions in the displayed range.
Given a system of congruences with coprime moduli, there is always a unique solution modulo the product. This ancient result (attributed to Sun Tzu, ~3rd century) is both theoretically deep and computationally essential.
Enter a base, exponent, and modulus, then step through the bits of the exponent. Each step squares, and multiplies in only when the bit is 1.
Repeated squaring computes be mod m in O(log e) multiplications by scanning the binary digits of the exponent from least to most significant.
Computing ab mod m naively requires b multiplications. The repeated squaring algorithm uses the binary expansion of b to do it in only log(b) steps — essential for cryptography where exponents have hundreds of digits.
Spot a mistake or have an idea?