Modular Arithmetic Simulator: Clock Math & Residue Patterns

simulator intermediate ~10 min
Loading simulation...
7³ mod 12 = 7 — the powers of 7 cycle mod 12 with period 2

7 cubed equals 343, which leaves remainder 7 when divided by 12. The powers of 7 modulo 12 cycle as 7, 1, 7, 1, ... with order 2.

Formula

a^n mod m via repeated squaring: O(log n) multiplications
φ(m) = m × ∏(p|m)(1 - 1/p) (Euler's totient)
a^φ(m) ≡ 1 (mod m) for gcd(a,m) = 1 (Euler's theorem)

Clock Arithmetic

Modular arithmetic is the mathematics of remainders — the same arithmetic you use when reading a clock. If it is 10 o'clock and you add 5 hours, you get 3, not 15. Formally, we say 15 ≡ 3 (mod 12). This simple idea, systematized by Gauss in his 1801 Disquisitiones Arithmeticae, became one of the most powerful tools in modern mathematics and computer science. This simulator visualizes residues on a clock face and traces multiplication patterns as geometric star polygons.

Powers and Orders

Raising a number to successive powers modulo m creates a cyclic sequence. The length of this cycle — called the multiplicative order of a mod m — is a key quantity. When the order equals Euler's totient φ(m), the base is a 'primitive root' and its powers visit every residue coprime to m. Euler's theorem guarantees a^φ(m) ≡ 1 (mod m) whenever gcd(a, m) = 1, providing a universal upper bound on the order.

Residue Patterns

The multiplication table modulo m reveals surprising geometric structure. Multiplying each residue by a fixed k and plotting on a circle creates star polygons when gcd(k, m) = 1, and degenerate patterns (fewer points, multiple overlapping orbits) when gcd(k, m) > 1. These patterns connect number theory to geometry and group theory — the residues coprime to m form a multiplicative group, and primitive roots are its generators.

Foundation of Cryptography

Nearly all modern public-key cryptography rests on modular arithmetic. RSA uses the fact that modular exponentiation is fast (via repeated squaring in O(log n) multiplications) while its inverse — the discrete logarithm — is computationally infeasible for large moduli. Diffie-Hellman key exchange, ElGamal encryption, and elliptic curve methods all exploit the rich algebraic structure of modular residue systems.

FAQ

What is modular arithmetic?

Modular arithmetic is a system where numbers 'wrap around' after reaching a fixed value called the modulus, like hours on a clock. Formally, a ≡ b (mod m) means m divides (a - b). It is fundamental to number theory, cryptography, and computer science.

What is Euler's totient function?

Euler's totient φ(m) counts integers from 1 to m that are coprime to m. For a prime p, φ(p) = p-1. Euler's theorem states a^φ(m) ≡ 1 (mod m) for gcd(a,m) = 1, generalizing Fermat's little theorem.

What is a primitive root?

A primitive root modulo m is an integer whose powers generate all residues coprime to m. Equivalently, its multiplicative order equals φ(m). Primitive roots exist when m is 1, 2, 4, p^k, or 2p^k for odd primes p.

How is modular arithmetic used in cryptography?

RSA encryption relies on modular exponentiation: computing a^e mod n is fast, but reversing it (discrete logarithm) is computationally infeasible for large n. Diffie-Hellman key exchange and elliptic curve cryptography also depend on modular arithmetic.

Sources

Embed

<iframe src="https://homo-deus.com/lab/number-theory/modular-arithmetic/embed" width="100%" height="400" frameborder="0"></iframe>
View source on GitHub