Modular Arithmetic Basics: Clock Math for CS Students

Number theory · Updated July 2026

You already do modular arithmetic every day. If it is 9:00 and a meeting is in 5 hours, you say 2:00, not 14:00 — you wrapped around a 12-hour clock. Modular arithmetic is the formal version of that wrap-around, and it turns out to be one of the most practically important ideas in a discrete math course: it underlies hash tables, checksums, random number generators, and essentially all modern cryptography. This guide covers the notation, the rules you may use freely, the Euclidean algorithm, and the one operation that trips everyone up: division.

Congruence: the notation that scares people for no reason

We write a ≡ b (mod n) — read "a is congruent to b modulo n" — to mean that a and b leave the same remainder when divided by n. Equivalently, n divides a − b. So 17 ≡ 5 (mod 12) because both leave remainder 5, and 23 ≡ 3 (mod 10).

The power of congruence is that it behaves like equality for addition, subtraction, and multiplication. If a ≡ b (mod n) and c ≡ d (mod n), then:

Practically, this means you may reduce early and often. Asked for the last digit of 7¹⁰⁰, you never compute 7¹⁰⁰; you work mod 10 and reduce at every step: 7² = 49 ≡ 9, 7⁴ ≡ 9² = 81 ≡ 1, so 7¹⁰⁰ = (7⁴)²⁵ ≡ 1²⁵ = 1. Last digit: 1. Reducing early keeps every intermediate number small — the same trick that makes modular exponentiation feasible for the enormous numbers used in cryptography.

GCD and the Euclidean algorithm

The greatest common divisor gcd(a, b) is the largest integer dividing both a and b. Computing it by factoring is hopeless for large numbers, but a 2,300-year-old method makes it fast. The Euclidean algorithm rests on one fact: gcd(a, b) = gcd(b, a mod b). Replace the pair with the smaller pair, repeat, and when the remainder hits 0, the other number is the gcd.

gcd(252, 198): 252 = 1·198 + 54 → 198 = 3·54 + 36 → 54 = 1·36 + 18 → 36 = 2·18 + 0. So gcd = 18.

Four quick steps, no factoring. The extended Euclidean algorithm runs the same computation while tracking the quotients, producing integers x and y with ax + by = gcd(a, b) (Bézout's identity). That bookkeeping looks like an exam-only ritual, but it is exactly how modular inverses are computed — see below. Related facts worth keeping at hand: lcm(a, b) = ab / gcd(a, b), and two numbers are coprime when their gcd is 1.

Division is the trap

Here is where modular arithmetic diverges from ordinary algebra. You may not freely divide both sides of a congruence. From 6 ≡ 36 (mod 10) you cannot cancel 6 to get 1 ≡ 6 (mod 10) — that is false. Division must be replaced by multiplication by a modular inverse: a number a⁻¹ with a·a⁻¹ ≡ 1 (mod n).

And inverses do not always exist: a has an inverse mod n exactly when gcd(a, n) = 1. That is the deep reason the extended Euclidean algorithm matters — when gcd(a, n) = 1, Bézout gives ax + ny = 1, so ax ≡ 1 (mod n), and x is the inverse. For example, the inverse of 7 mod 26 is 15, since 7 × 15 = 105 = 4 × 26 + 1. This exact computation is how affine ciphers are decrypted and it sits at the heart of RSA key generation.

Where CS actually uses this

Habits that prevent lost points

How Discretica helps

Discretica's number theory module covers divisibility, primes, modular arithmetic, GCD, LCM, and the Euclidean algorithm. Two of its interactive tools map directly onto this guide: the Modular Arithmetic tool handles addition, subtraction, multiplication, powers, and inverses, and the GCD & LCM Calculator shows the Euclidean and extended Euclidean steps — so you can trace exactly where a hand computation went wrong. A Base Converter for binary, octal, decimal, and hexadecimal rounds out the number-crunching toolkit, and practice problems with hints and step-by-step solutions let you drill until the wrap-around thinking is automatic. Works offline, no account needed. Free to start on iOS and Android.