Modular Arithmetic

17 = 5 (mod 12)
17 and 5 leave the same remainder when divided by 12

Modular arithmetic is arithmetic on a circle. Two numbers are congruent modulo n if they differ by a multiple of n. A clock does arithmetic mod 12: 10 hours after 5 o'clock is 3, not 15. This simple idea underlies all modern cryptography, hash functions, error-correcting codes, and much of number theory.

The mod 12 clock: addition wraps around
1 2 3 4 5 6 7 8 9 10 11 12 17 mod 12 = 5 17 = 1 × 12 + 5
Fermat's Little Theorem: the key to RSA encryption
a^(p-1) = 1 (mod p) whenever p is prime and p does not divide a VERIFICATION: p=7, computing a^6 mod 7 2^6 = 64 = 9x7 + 1 = 1 (mod 7) ✓ 3^6 = 729 = 104x7 + 1 = 1 (mod 7) ✓ 5^6 = 15625 = 2232x7 + 1 = 1 (mod 7) ✓ RSA uses this: encrypting then decrypting returns the original because (m^e)^d = m (mod n)
The integers mod n form a complete system: addition table for mod 5
ADDITION TABLE mod 5 {['+','0','1','2','3','4'].map((v,i) => `${v}`).join('')} {[0,1,2,3,4].map((row,ri) => { const y = 62+ri*18 return `${row}` + [0,1,2,3,4].map((col,ci) => { const val=(row+col)%5 const fill = val===0?'#c0392b':'#333' return `${val}` }).join('') }).join('')} Red zeros show where sums wrap around. The five elements {0,1,2,3,4} form a closed group under +.
Related topics
Primes Perfect Numbers Number Systems
Key facts about Modular Arithmetic

Modular arithmetic defines congruence: a is congruent to b mod n if n divides a-b. Gauss systematised it in 1801. It underlies all modern public-key cryptography: RSA encryption relies on Fermat's Little Theorem, which states that a^(p-1) is congruent to 1 mod p for any prime p not dividing a. Hash functions use modular operations to map large inputs to fixed-size outputs. The integers mod n form a complete ring, and when n is prime, a finite field.

Used in
Mathematics
💻Computer Sci
Music
🔐Cryptography
Physics
Engineering
🧬Biology
📊Statistics
📈Finance
🎨Art
🏛Architecture
🌌Astronomy
Chemistry
🦉Philosophy
🗺Geography
🌿Ecology
Want to test your knowledge?
Question
What does a = b (mod n) mean?
tap · space
1 / 10