Skip to content
The Lumenary
Grades 6–8Ages 11–14 60 min

Clock arithmetic: remainders and modular arithmetic

On a clock, 9 + 5 = 2. This is not a mistake. It is a whole new arithmetic, and it is the arithmetic of weekdays, calendars, barcodes and secret codes.

Formally verified · 16 LeanComputationally tested · 2 PythonAI-drafted · awaiting human reviewMathematically reviewed: Awaiting human review

Your guides: Chloe, Margaret, Gideon

MotivationH

What time will it be?

Four questions that look unrelated:

  1. The overnight train from Mumbai leaves at 21:00 and the journey takes 30 hours. At what time does it arrive?
  2. 1 January 2026 was a Thursday. What day of the week was it 100 days later?
  3. In Dar es Salaam, a friend says she will meet you at saa moja asubuhi — “hour one in the morning”. When should you be there?
  4. What is the last digit of 720267^{2026}, a number with more than 1700 digits?

Every one of them is answered the same way: throw away whole cycles and keep the remainder. A day has 24 hours, a week has 7 days, a clock face has 12 hours, and last digits repeat every 10. This lesson turns that trick into an arithmetic of its own, proves why it works, and finds something surprising hiding in its multiplication tables.

IntuitionH

Wrapping the number line around a clock

Take the number line 0,1,2,3,…0, 1, 2, 3, \dots and wind it around a circle with 12 equally spaced marks. The numbers 0,1,…,110, 1, \dots, 11 go once around; then 1212 lands on top of 00, 1313 on top of 11, and so on.

0123456789101112, 24, 36, …14, 26, 38, …21, 33, 45, …
The number line wound around a 12-hour clock. Numbers that land on the same mark leave the same remainder on division by 12: 2, 14, 26, 38, … all land on 2.

Two numbers land on the same mark exactly when they differ by a whole number of trips around — a multiple of 12. So to find where a number lands, divide by 12 and keep the remainder. Adding means walking forwards around the circle. Nothing else about the number matters.

DefinitionsH

Remainders and arithmetic modulo n

You know division with remainder: for whole numbers aa and n≥1n \ge 1 there is exactly one way to write

a=q⋅n+rwith 0≤r≤n−1.a = q \cdot n + r \qquad \text{with } 0 \le r \le n - 1.

Definition— a mod n

The remainder rr above is called aa modulo nn, written a mod na \bmod n. The number nn is the modulus. It is always one of 0,1,…,n−10, 1, \dots, n - 1.

For example 38 mod 12=238 \bmod 12 = 2 because 38=3⋅12+238 = 3 \cdot 12 + 2, and 100 mod 7=2100 \bmod 7 = 2 because 100=14⋅7+2100 = 14 \cdot 7 + 2.

Definition— addition and multiplication modulo n

To add modulo nn, add as usual and take the remainder on division by nn. To multiply modulo nn, multiply as usual and take the remainder. The numbers 0,1,…,n−10, 1, \dots, n - 1 with these two operations are called the integers modulo nn.

When aa and bb leave the same remainder on division by nn, mathematicians write a≡b(modn)a \equiv b \pmod n and say “aa is congruent to bb modulo nn”. So 38≡2(mod12)38 \equiv 2 \pmod{12} and 9+29≡2(mod12)9 + 29 \equiv 2 \pmod{12}. It is the notation of the clock: “lands on the same mark”.

Here are two complete multiplication tables. Look along each row.

× mod 6012345
0000000
1012345
2024024
3030303
4042042
5054321
× mod 501234
000000
101234
202413
303142
404321

Modulo 5, every non-zero row is a shuffle of 0,1,2,3,40, 1, 2, 3, 4: each remainder appears exactly once. Modulo 6 the rows of 22, 33 and 44 repeat themselves, and strange things happen: 2⋅3≡02 \cdot 3 \equiv 0 although neither factor is 00.

Definition— multiplicative partner

A remainder aa has a partner modulo nn if some bb satisfies a⋅b≡1(modn)a \cdot b \equiv 1 \pmod n. Then multiplying by bb undoes multiplying by aa, just as multiplying by 13\tfrac{1}{3} undoes multiplying by 33.

Modulo 5: 2⋅3=6≡12 \cdot 3 = 6 \equiv 1, so 22 and 33 are partners, and 4⋅4=16≡14 \cdot 4 = 16 \equiv 1. Modulo 6, only 11 and 55 have partners — the rows of 2,3,42, 3, 4 never contain a 11.

Worked examplesH

Worked examples

Example— The overnight train

Leave at 21:00, travel 30 hours. On a 24-hour clock: 21+30=51=2⋅24+321 + 30 = 51 = 2 \cdot 24 + 3, so 51 mod 24=351 \bmod 24 = 3. The train arrives at 03:00. The quotient 22 is useful too: it is two days later.

Example— One hundred days after a Thursday

A week has 7 days, so only 100 mod 7100 \bmod 7 matters: 100=14⋅7+2100 = 14 \cdot 7 + 2. Fourteen whole weeks change nothing; the extra 22 days move Thursday to Saturday. (1 January 2026 was a Thursday, so 11 April 2026 was a Saturday.)

Example— Swahili time

In much of East Africa the hours of the day are traditionally counted from about sunrise, which near the equator is close to 6 a.m. all year round. So 7 a.m. is saa moja (“hour one”), noon is saa sita (“hour six”) and 1 p.m. is saa saba (“hour seven”). To convert a time on a 12-hour clock, add 66 modulo 1212: 7+6=13≡17 + 6 = 13 \equiv 1, and 1+6=71 + 6 = 7. So saa moja asubuhi is 7 a.m. (A clock face writes 1212 where we write 00, but it is the same mark.)

Example— Reduce first, then calculate

What is 38⋅47 mod 938 \cdot 47 \bmod 9? Instead of multiplying, reduce first: 38=4⋅9+238 = 4 \cdot 9 + 2 and 47=5⋅9+247 = 5 \cdot 9 + 2, so the answer is 2⋅2=42 \cdot 2 = 4. Check: 38⋅47=1786=198⋅9+438 \cdot 47 = 1786 = 198 \cdot 9 + 4. ✓ The next section proves this shortcut is always allowed.

Example— Negative numbers on the clock

Going back 77 hours from 00 on a 5-hour clock lands on 33. In symbols, −7=(−2)⋅5+3-7 = (-2) \cdot 5 + 3, so −7 mod 5=3-7 \bmod 5 = 3. The remainder must be one of 0,…,40, \dots, 4, so the answer is not −2-2.

Example— Non-example: mod is not division

17 mod 517 \bmod 5 is 22 (the remainder), not 3.43.4 (the quotient as a decimal) and not 33 (the whole-number quotient).

ProofH

Why reducing first always works

The shortcut “throw away multiples of nn whenever you like” rests on two theorems.

Theorem— Theorem 1 (sums)

For all whole numbers aa, bb and n≥1n \ge 1:

(a+b) mod n=((a mod n)+(b mod n)) mod n.(a + b) \bmod n = \big((a \bmod n) + (b \bmod n)\big) \bmod n.

Proof

Divide with remainder: a=q1n+r1a = q_1 n + r_1 and b=q2n+r2b = q_2 n + r_2, where r1=a mod nr_1 = a \bmod n and r2=b mod nr_2 = b \bmod n. Adding,

a+b=(q1+q2) n+(r1+r2).a + b = (q_1 + q_2)\, n + (r_1 + r_2).

So a+ba + b is a multiple of nn plus r1+r2r_1 + r_2. Adding a multiple of nn means walking a whole number of times around the clock, which does not change where you land: if r1+r2=qn+rr_1 + r_2 = q n + r with 0≤r≤n−10 \le r \le n - 1, then a+b=(q1+q2+q) n+ra + b = (q_1 + q_2 + q)\,n + r, and by the uniqueness of division with remainder, (a+b) mod n=r=(r1+r2) mod n(a + b) \bmod n = r = (r_1 + r_2) \bmod n.

∎

Theorem— Theorem 2 (products)

For all whole numbers aa, bb and n≥1n \ge 1:

(a⋅b) mod n=((a mod n)⋅(b mod n)) mod n.(a \cdot b) \bmod n = \big((a \bmod n) \cdot (b \bmod n)\big) \bmod n.

Proof

With the same notation, multiply out:

a⋅b=(q1n+r1)(q2n+r2)=(q1q2n+q1r2+r1q2) n+r1r2.a \cdot b = (q_1 n + r_1)(q_2 n + r_2) = \big(q_1 q_2 n + q_1 r_2 + r_1 q_2\big)\, n + r_1 r_2.

So a⋅ba \cdot b is a multiple of nn plus r1r2r_1 r_2, and as before its remainder is the remainder of r1r2r_1 r_2.

∎

Together these say: the remainder of a sum or product depends only on the remainders of its parts. That is exactly why the clock arithmetic of the last section is consistent — and why you may reduce at any moment in a long calculation.

Theorem— Theorem 3 (remainders of powers repeat)

For any whole numbers aa and n≥1n \ge 1, the sequence a0 mod n, a1 mod n, a2 mod n,…a^0 \bmod n,\ a^1 \bmod n,\ a^2 \bmod n, \dots eventually repeats in a cycle.

Proof

By Theorem 2, ak+1 mod n=((ak mod n)⋅a) mod na^{k+1} \bmod n = \big((a^k \bmod n) \cdot a\big) \bmod n: each remainder is determined by the one before. There are only nn possible remainders, so among the first n+1n + 1 terms two must be equal (the pigeonhole principle). From the first repeat on, the sequence must go round the same loop for ever, because each term determines the next.

∎

Example— The last digit of a huge power

Last digits are remainders modulo 10. The powers of 7 end in 7,9,3,1,7,9,3,1,…7, 9, 3, 1, 7, 9, 3, 1, \dots because 74=2401≡1(mod10)7^4 = 2401 \equiv 1 \pmod{10}. Then 74k=(74)k≡1k=17^{4k} = (7^4)^k \equiv 1^k = 1 by Theorem 2, and

72026=74⋅506+2=(74)506⋅72≡1⋅49≡9(mod10).7^{2026} = 7^{4 \cdot 506 + 2} = (7^4)^{506} \cdot 7^2 \equiv 1 \cdot 49 \equiv 9 \pmod{10}.

So 720267^{2026} ends in 9 — found without computing a 1713-digit number.

Example— Casting out nines

Since 10=9+110 = 9 + 1, we have 10≡1(mod9)10 \equiv 1 \pmod 9, so 10k≡1k=1(mod9)10^k \equiv 1^k = 1 \pmod 9 for every kk (Theorem 2 again). A number such as 5678=5⋅103+6⋅102+7⋅10+85678 = 5 \cdot 10^3 + 6 \cdot 10^2 + 7 \cdot 10 + 8 therefore leaves the same remainder on division by 9 as its digit sum 5+6+7+8=265 + 6 + 7 + 8 = 26, and 26 mod 9=826 \bmod 9 = 8. Accountants and merchants used this for centuries to catch mistakes: 1234⋅5678=70066521234 \cdot 5678 = 7006652 is plausible because the digit sums give 1⋅8=81 \cdot 8 = 8 and 7+0+0+6+6+5+2=26≡87 + 0 + 0 + 6 + 6 + 5 + 2 = 26 \equiv 8. (A check like this can catch an error but never prove an answer right — see check digits.)

ProofH

Which multiplications can be undone?

Look again at the tables. Modulo 6, only 11 and 55 have partners; modulo 5, every non-zero remainder does. Modulo 12 (try it in the explorer below) only 1,5,7,111, 5, 7, 11 do. What decides?

Theorem— Theorem 4 (a common factor blocks a partner)

If aa and nn have a common factor d>1d > 1, then aa has no partner modulo nn: there is no bb with a⋅b mod n=1a \cdot b \bmod n = 1.

Proof

Suppose a⋅b=qn+1a \cdot b = q n + 1 for some whole numbers bb and qq. Then 1=a⋅b−qn1 = a \cdot b - q n. Now dd divides aa, so it divides a⋅ba \cdot b; and dd divides nn, so it divides qnq n. Hence dd divides their difference, which is 11. But no whole number greater than 11 divides 11. This contradiction shows no such bb exists.

∎

So 2,3,4,6,8,9,102, 3, 4, 6, 8, 9, 10 have no partners modulo 1212, and 2,4,5,6,82, 4, 5, 6, 8 have none modulo 1010. The converse is also true — if aa and nn have no common factor greater than 1, then aa has a partner modulo nn — but its proof needs Euclid's algorithm, which you will meet in divisibility and the gcd. Here we prove the most important case.

Theorem— Theorem 5 (prime clocks)

If pp is a prime number, every remainder a∈{1,2,…,p−1}a \in \{1, 2, \dots, p - 1\} has a partner modulo pp.

Proof

We use Euclid's lemma: if a prime divides a product of two whole numbers, it divides one of them (proved in divisibility and the gcd).

Look at the row of aa in the multiplication table: the remainders of a⋅1,a⋅2,…,a⋅(p−1)a \cdot 1, a \cdot 2, \dots, a \cdot (p - 1).

  • None of them is 00: if pp divided a⋅xa \cdot x with 1≤x≤p−11 \le x \le p - 1, it would divide aa or xx, but both lie strictly between 00 and pp.
  • No two of them are equal: if a⋅xa \cdot x and a⋅ya \cdot y left the same remainder with x>yx > y, then pp would divide the difference a⋅(x−y)a \cdot (x - y), so pp would divide aa or x−yx - y; but 1≤x−y≤p−21 \le x - y \le p - 2.

So the row contains p−1p - 1 different non-zero remainders. There are only p−1p - 1 non-zero remainders, so every one of them appears — in particular 11. The xx with a⋅x mod p=1a \cdot x \bmod p = 1 is the partner.

∎

The same reasoning explains the zero products. If n=r⋅sn = r \cdot s with 1<r,s<n1 \lt r, s \lt n, then r⋅s≡0(modn)r \cdot s \equiv 0 \pmod n although r,s≢0r, s \not\equiv 0. Modulo a prime that cannot happen, by Euclid's lemma. Prime clocks behave like ordinary fractions: you can divide by anything except 00.

Formal mathematicsF

Clock arithmetic in Lean

In Lean, a % n is the remainder of a on division by n. First, division with remainder and the range of a remainder (both quoted from Lean's library), and the train and weekday examples, which Lean checks by computing:

div_add_modLean 4Applies a library theoremFormally verified
/-- **Division with remainder.** Every `a` is `n` times its quotient plus its remainder:
`n · (a / n) + a % n = a`. This applies core Lean's `Nat.div_add_mod`. -/
theorem div_add_mod (a n : Nat) : n * (a / n) + a % n = a := Nat.div_add_mod a n
Axioms used: propextformal/Lumenary/ModularArithmetic.leanTry it in the Lean web editor
mod_ltLean 4Applies a library theoremFormally verified
/-- A remainder on division by `n > 0` is always one of `0, 1, …, n - 1`: `a % n < n`.
This applies core Lean's `Nat.mod_lt`. -/
theorem mod_lt (a : Nat) {n : Nat} (hn : 0 < n) : a % n < n := Nat.mod_lt a hn
Axioms used: No axiomsformal/Lumenary/ModularArithmetic.leanTry it in the Lean web editor
clock_exampleLean 4Proved here from basic factsFormally verified
/-- On a 12-hour clock, 29 hours after 9 o'clock it is 2 o'clock: `(9 + 29) mod 12 = 2`. -/
theorem clock_example : (9 + 29) % 12 = 2 := by decide
Axioms used: No axiomsformal/Lumenary/ModularArithmetic.leanTry it in the Lean web editor
week_exampleLean 4Proved here from basic factsFormally verified
/-- Number the days Monday = 0, Tuesday = 1, …, Sunday = 6. Then 100 days after a Monday is
day `100 mod 7 = 2`, a Wednesday. -/
theorem week_example : (0 + 100) % 7 = 2 := by decide
Axioms used: No axiomsformal/Lumenary/ModularArithmetic.leanTry it in the Lean web editor

Theorems 1 and 2 are proved exactly as in the human proofs: split a and b into quotient and remainder, regroup, and use the fact that adding a multiple of n does not change a remainder (Nat.mul_add_mod). The calc blocks are the displayed equations of the proofs.

add_mod_of_divisionLean 4Proved here from basic factsFormally verified
/-- **The remainder of a sum depends only on the remainders.**
`(a + b) mod n = ((a mod n) + (b mod n)) mod n`.
The lesson's proof: write `a = n·q₁ + r₁` and `b = n·q₂ + r₂`. Then
`a + b = n·(q₁ + q₂) + (r₁ + r₂)`, and adding a multiple of `n` never changes a remainder
(`Nat.mul_add_mod`). -/
theorem add_mod_of_division (a b n : Nat) : (a + b) % n = (a % n + b % n) % n := by
  -- The two divisions with remainder.
  have ha := Nat.div_add_mod a n
  have hb := Nat.div_add_mod b n
  -- Regroup: `a + b` is a multiple of `n` plus `r₁ + r₂`.
  have hsum : a + b = n * (a / n + b / n) + (a % n + b % n) := by
    calc a + b = (n * (a / n) + a % n) + (n * (b / n) + b % n) := by rw [ha, hb]
      _ = n * (a / n + b / n) + (a % n + b % n) := by rw [Nat.mul_add]; omega
  rw [hsum, Nat.mul_add_mod]
Axioms used: propext, Quot.soundformal/Lumenary/ModularArithmetic.leanTry it in the Lean web editor
mul_mod_of_divisionLean 4Proved here from basic factsFormally verified
/-- **The remainder of a product depends only on the remainders.**
`(a · b) mod n = ((a mod n) · (b mod n)) mod n`.
Proof: with `a = n·q₁ + r₁` and `b = n·q₂ + r₂`, expanding gives
`a · b = n·(q₁·b + r₁·q₂) + r₁·r₂`, a multiple of `n` plus `r₁ · r₂`. -/
theorem mul_mod_of_division (a b n : Nat) : (a * b) % n = (a % n * (b % n)) % n := by
  have ha := Nat.div_add_mod a n
  have hb := Nat.div_add_mod b n
  have hprod : a * b = n * (a / n * b + a % n * (b / n)) + a % n * (b % n) := by
    calc a * b = (n * (a / n) + a % n) * b := by rw [ha]
      _ = n * (a / n) * b + a % n * b := Nat.add_mul _ _ _
      _ = n * (a / n) * b + a % n * (n * (b / n) + b % n) := by rw [hb]
      _ = n * (a / n * b + a % n * (b / n)) + a % n * (b % n) := by
        rw [Nat.mul_add (a % n), Nat.mul_add n, Nat.mul_assoc, ← Nat.add_assoc,
          Nat.mul_left_comm (a % n) n]
  rw [hprod, Nat.mul_add_mod]
Axioms used: propextformal/Lumenary/ModularArithmetic.leanTry it in the Lean web editor
add_mod_libraryLean 4Applies a library theoremFormally verified
/-- The same fact for sums, quoted from core Lean's library (`Nat.add_mod`). Compare
`add_mod_of_division`, which proves it here from division with remainder. -/
theorem add_mod_library (a b n : Nat) : (a + b) % n = (a % n + b % n) % n := Nat.add_mod a b n
Axioms used: propextformal/Lumenary/ModularArithmetic.leanTry it in the Lean web editor

Zero products, Theorem 4, and a use of it:

zero_product_mod_twelveLean 4Proved here from basic factsFormally verified
/-- On a 12-hour clock a product can be `0` although neither factor is:
`3 · 4 = 12 ≡ 0 (mod 12)` while `3 mod 12 = 3` and `4 mod 12 = 4`. -/
theorem zero_product_mod_twelve : (3 * 4) % 12 = 0 ∧ 3 % 12 ≠ 0 ∧ 4 % 12 ≠ 0 := by decide
Axioms used: No axiomsformal/Lumenary/ModularArithmetic.leanTry it in the Lean web editor
no_partner_of_common_factorLean 4Proved here from basic factsFormally verified
/-- **A common factor blocks a partner.** If some `d > 1` divides both `a` and `n`, then no `b`
has `a · b mod n = 1`.
Proof: `d` divides `a · b`, and `d` divides `n`, so `d` divides the remainder `a · b mod n`
(the remainder is `a · b` minus a multiple of `n`). If that remainder were `1`, then `d` would
divide `1`, so `d = 1`, contradicting `d > 1`. -/
theorem no_partner_of_common_factor {a n d : Nat} (hd : 1 < d) (hda : d ∣ a) (hdn : d ∣ n)
    (b : Nat) : (a * b) % n ≠ 1 := by
  intro h
  -- `d` divides `a · b` …
  have hab : d ∣ a * b := Nat.dvd_mul_right_of_dvd hda b
  -- … so it divides the remainder of `a · b` on division by `n`, which is `1`.
  have h1 : d ∣ (a * b) % n := (Nat.dvd_mod_iff hdn).mpr hab
  rw [h] at h1
  have : d = 1 := Nat.eq_one_of_dvd_one h1
  omega
Axioms used: propext, Quot.soundformal/Lumenary/ModularArithmetic.leanTry it in the Lean web editor
four_no_partner_mod_tenLean 4Proved here from basic factsFormally verified
/-- `4` has no partner modulo `10`: `4` and `10` share the factor `2`. This is
`no_partner_of_common_factor` with `d = 2`. -/
theorem four_no_partner_mod_ten (b : Nat) : (4 * b) % 10 ≠ 1 :=
  no_partner_of_common_factor (d := 2) (by decide) (by decide) (by decide) b
Axioms used: propext, Quot.soundformal/Lumenary/ModularArithmetic.leanTry it in the Lean web editor

Lean has clock arithmetic built in: Fin 12 is the set of numbers 0,…,110, \dots, 11, and its * multiplies and then takes the remainder on division by 12. For one particular clock, decide can simply try every case — 144 products for the 12-hour clock:

partners_mod_twelveLean 4Proved here from basic factsFormally verified
/-- On a 12-hour clock exactly `1, 5, 7, 11` have a multiplicative partner. Lean's `Fin 12`
is clock arithmetic: its numbers are `0, …, 11` and `*` multiplies and takes the remainder.
`decide` checks all 144 products. -/
theorem partners_mod_twelve :
    ∀ a : Fin 12, (∃ b : Fin 12, a * b = 1) ↔ (a = 1 ∨ a = 5 ∨ a = 7 ∨ a = 11) := by
  decide
Axioms used: propext, Quot.soundformal/Lumenary/ModularArithmetic.leanTry it in the Lean web editor
partners_mod_sevenLean 4Proved here from basic factsFormally verified
/-- Modulo the prime `7`, every non-zero remainder has a partner
(`1·1`, `2·4`, `3·5`, `6·6` all leave remainder `1`). `decide` checks every case. -/
theorem partners_mod_seven : ∀ a : Fin 7, a ≠ 0 → ∃ b : Fin 7, a * b = 1 := by decide
Axioms used: propext, Quot.soundformal/Lumenary/ModularArithmetic.leanTry it in the Lean web editor
row_is_shuffle_mod_sevenLean 4Proved here from basic factsFormally verified
/-- Modulo `7`, multiplying by a non-zero `a` never sends two different remainders to the
same place: each row of the multiplication table is a shuffle of `0, …, 6`. (This is the
property check digits rely on.) -/
theorem row_is_shuffle_mod_seven :
    ∀ a x y : Fin 7, a ≠ 0 → a * x = a * y → x = y := by decide
Axioms used: propextformal/Lumenary/ModularArithmetic.leanTry it in the Lean web editor

These three are checked case by case for one modulus each; Theorem 5 for every prime is not formalised here (it needs Euclid's lemma, which is formalised in the lesson on divisibility). Finally, the repeating patterns, proved for all kk by induction — the formal version of “and so on for ever”:

pow_three_mod_sevenLean 4Proved here from basic factsFormally verified
/-- The powers of `2` modulo `7` repeat `1, 2, 4, 1, 2, 4, …`. In particular
`2^(3k) mod 7 = 1` for every `k`.
Proof by induction: `2^(3(k+1)) = 2^(3k) · 8`, and `8 mod 7 = 1`, so by
`mul_mod_of_division` the remainder is `1 · 1 = 1`. -/
theorem pow_three_mod_seven (k : Nat) : 2 ^ (3 * k) % 7 = 1 := by
  induction k with
  | zero => decide
  | succ k ih =>
    calc 2 ^ (3 * (k + 1)) % 7 = (2 ^ (3 * k) * 8) % 7 := by
          rw [Nat.mul_succ, Nat.pow_add]
      _ = (2 ^ (3 * k) % 7 * (8 % 7)) % 7 := mul_mod_of_division _ _ _
      _ = 1 := by rw [ih]
Axioms used: propextformal/Lumenary/ModularArithmetic.leanTry it in the Lean web editor
last_digit_seven_powLean 4Proved here from basic factsFormally verified
/-- The last digit of `7^(4k + 2)` is always `9`: the last digits of `7, 49, 343, 2401, …`
repeat `7, 9, 3, 1`. Proof: `7^4 = 2401` leaves remainder `1` on division by `10`, so
`7^(4k) mod 10 = 1` by induction, and `7^2 = 49`. For example, `7^2026` ends in `9`. -/
theorem last_digit_seven_pow (k : Nat) : 7 ^ (4 * k + 2) % 10 = 9 := by
  have h4 : ∀ j : Nat, 7 ^ (4 * j) % 10 = 1 := by
    intro j
    induction j with
    | zero => decide
    | succ j ih =>
      calc 7 ^ (4 * (j + 1)) % 10 = (7 ^ (4 * j) * 2401) % 10 := by
            rw [Nat.mul_succ, Nat.pow_add]
        _ = (7 ^ (4 * j) % 10 * (2401 % 10)) % 10 := mul_mod_of_division _ _ _
        _ = 1 := by rw [ih]
  calc 7 ^ (4 * k + 2) % 10 = (7 ^ (4 * k) * 49) % 10 := by rw [Nat.pow_add]
    _ = (7 ^ (4 * k) % 10 * (49 % 10)) % 10 := mul_mod_of_division _ _ _
    _ = 9 := by rw [h4 k]
Axioms used: propextformal/Lumenary/ModularArithmetic.leanTry it in the Lean web editor
ten_pow_mod_nineLean 4Proved here from basic factsFormally verified
/-- `10^k mod 9 = 1` for every `k`, because `10 = 9 + 1` leaves remainder `1`. This is why a
number and the sum of its digits leave the same remainder on division by `9`
("casting out nines"). -/
theorem ten_pow_mod_nine (k : Nat) : 10 ^ k % 9 = 1 := by
  induction k with
  | zero => decide
  | succ k ih =>
    calc 10 ^ (k + 1) % 9 = (10 ^ k * 10) % 9 := by rw [Nat.pow_succ]
      _ = (10 ^ k % 9 * (10 % 9)) % 9 := mul_mod_of_division _ _ _
      _ = 1 := by rw [ih]
Axioms used: propextformal/Lumenary/ModularArithmetic.leanTry it in the Lean web editor

In Mathlib, the integers modulo nn form the ring ZMod n, congruence is Nat.ModEq, and the partner of a number coprime to nn is built by ZMod.unitOfCoprime.

ComputationC

Every clock up to 30

Partners and zero products on every clock up to 30PythonExact computationExhaustive finite checkComputationally tested
"""Clock arithmetic by machine: reduce-first rules, partners and zero products.

For every modulus n from 2 to 30 the experiment builds the multiplication
table modulo n and finds which remainders a have a partner b with
a * b mod n = 1. It compares that list with the remainders that share no
factor greater than 1 with n, and it searches for "zero products" a * b = 0
with neither factor 0. It also checks the reduce-first rules for sums and
products on a finite range. Everything is exact integer arithmetic over finite
ranges: exhaustive evidence for those n, not a proof for every n.
"""

import math

def partners(n):
    """Remainders a in 0..n-1 that have some b with a * b mod n == 1."""
    if n < 1:
        raise ValueError("n must be positive")
    return [a for a in range(n) if any(a * b % n == 1 for b in range(n))]

def partner_of(a, n):
    """The b in 0..n-1 with a * b mod n == 1, or None if there is none."""
    for b in range(n):
        if a * b % n == 1:
            return b
    return None

def coprime_remainders(n):
    """Remainders a in 0..n-1 with gcd(a, n) == 1 (no common factor > 1)."""
    return [a for a in range(n) if math.gcd(a, n) == 1 and n > 1]

def zero_products(n):
    """Pairs (a, b) of non-zero remainders with a * b mod n == 0."""
    return [(a, b) for a in range(1, n) for b in range(1, n) if a * b % n == 0]

def reduce_first_failures(n_max, a_max):
    """Count cases and failures of the sum and product rules (n = 1..n_max)."""
    cases = failures = 0
    for n in range(1, n_max + 1):
        for a in range(a_max):
            for b in range(a_max):
                cases += 1
                if (a + b) % n != (a % n + b % n) % n:
                    failures += 1
                if (a * b) % n != (a % n) * (b % n) % n:
                    failures += 1
    return cases, failures

def is_prime(n):
    """Trial division (n >= 2)."""
    return n >= 2 and all(n % d for d in range(2, math.isqrt(n) + 1))

def main():
    cases, bad = reduce_first_failures(30, 120)
    print(f"Reduce-first rules, a, b < 120, n <= 30: {cases} cases, {bad} failures")
    print()
    print("Who has a partner?")
    for n in (10, 11, 12):
        pairs = ", ".join(f"{a}*{partner_of(a, n)}" for a in partners(n))
        print(f"  mod {n:<2}: {pairs}")
    same = all(partners(n) == coprime_remainders(n) for n in range(2, 31))
    print(f"  partners = remainders coprime to n, for n = 2..30: {same}")
    full = [n for n in range(2, 31) if len(partners(n)) == n - 1]
    print("  n for which every non-zero remainder has a partner:")
    print(f"    {full}")
    primes = [n for n in range(2, 31) if is_prime(n)]
    print(f"  these n are exactly the primes up to 30: {full == primes}")
    print()
    print("Zero products (a * b = 0 with a, b non-zero):")
    for n in (7, 10, 12):
        zs = zero_products(n)
        shown = ", ".join(f"{a}*{b}" for a, b in zs if a <= b)
        print(f"  mod {n:<2}: {shown or 'none'}")
    no_zero = [n for n in range(2, 31) if not zero_products(n)]
    print(f"  n <= 30 with no zero products: {no_zero}")
    print()
    print("Epistemic status: exhaustive, exact evidence for the moduli and ranges")
    print("shown, nothing more. The reduce-first rules and 'a common factor blocks")
    print("a partner' are proved in the lesson for every n; 'coprime implies a")
    print("partner' is proved later with Euclid's algorithm.")

if __name__ == "__main__":
    main()
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

The output agrees with Theorems 4 and 5 and with the converse we have not proved yet: the remainders with partners are exactly those sharing no factor with nn, and the moduli with no zero products are exactly the primes. For n≤30n \le 30 that is exhaustive evidence; for larger nn it is the theorems that do the work.

Cycles of remainders: powers and calendarsPythonExact computationExhaustive finite checkComputationally tested
"""Repeating patterns: last digits of powers, weekdays and calendar cycles.

Remainders of powers must eventually repeat, because there are only n
possible remainders. The experiment finds the repeating cycle of base^k mod n
for some bases (for n = 10 these are last digits), uses a cycle to get the
last digit of a huge power without computing the power, and checks the answer
against Python's exact pow. It then steps two calendar cycles forward day by
day until they line up again (the Chinese 10-stem and 12-branch cycle, and the
Maya 260-day and 365-day counts) and compares with the least common multiple.
All arithmetic is exact; each check covers only the cases printed.
"""

import math

def power_cycle(base, n):
    """Return (start, cycle) for the remainders base^k mod n, k = 0, 1, 2, ...

    ``start`` is the first k where the repeating part begins and ``cycle``
    lists one full period of remainders.
    """
    if n < 1:
        raise ValueError("n must be positive")
    seen = {}
    values = []
    r = 1 % n
    k = 0
    while r not in seen:
        seen[r] = k
        values.append(r)
        r = r * base % n
        k += 1
    start = seen[r]
    return start, values[start:]

def remainder_from_cycle(base, exponent, n):
    """base^exponent mod n read off the cycle (no huge power is computed)."""
    start, cycle = power_cycle(base, n)
    if exponent < start:
        return pow(base, exponent, n)
    return cycle[(exponent - start) % len(cycle)]

def realign(p, q):
    """Step a p-cycle and a q-cycle together; first day > 0 both are at 0."""
    day, a, b = 0, 0, 0
    while True:
        day, a, b = day + 1, (a + 1) % p, (b + 1) % q
        if a == 0 and b == 0:
            return day

WEEKDAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
            "Saturday", "Sunday"]

def weekday_after(start, days):
    """Name of the weekday ``days`` days after the weekday named ``start``."""
    return WEEKDAYS[(WEEKDAYS.index(start) + days) % 7]

def main():
    print("Cycles of remainders of powers (k = 0, 1, 2, ...):")
    for base, n in [(2, 10), (3, 10), (7, 10), (2, 7), (4, 12)]:
        start, cycle = power_cycle(base, n)
        print(f"  {base}^k mod {n:<2}: starts repeating at k = {start}, "
              f"cycle {cycle}")
    predicted = remainder_from_cycle(7, 2026, 10)
    exact = pow(7, 2026) % 10
    digits = len(str(pow(7, 2026)))
    print(f"Last digit of 7^2026 from the cycle: {predicted}; exact: {exact}")
    print(f"  (7^2026 has {digits} digits; the cycle needed only 4 numbers)")
    print()
    print("Weekdays: 1 January 2026 was a Thursday.")
    for days in (7, 100, 365):
        print(f"  {days:>3} days later: {weekday_after('Thursday', days)}")
    print()
    print("Two cycles running side by side, stepped one at a time:")
    for p, q, name in [(10, 12, "Chinese stems and branches (years)"),
                       (260, 365, "Maya tzolk'in and haab' (days)")]:
        print(f"  {name}:")
        print(f"    {p} and {q} realign after {realign(p, q)} steps;"
              f" lcm({p}, {q}) = {math.lcm(p, q)}")
    print()
    print("Epistemic status: exact computations, each checked only for the")
    print("cases printed. That powers must repeat is a proof (there are only n")
    print("remainders); that a cycle of length 4 continues forever for 7^k mod 10")
    print("is proved in the lesson from 7^4 = 2401 and the product rule.")

if __name__ == "__main__":
    main()
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

The Chinese calendar names years by pairing one of 10 heavenly stems with one of 12 earthly branches, both advancing each year; the pairs repeat after 6060 years, not 120120, because 6060 is the first number that is a multiple of both 1010 and 1212. The Maya ran a 260-day count and a 365-day count side by side; the pair of dates repeats only after 18 98018\,980 days, about 52 years — the Calendar Round.

Explore

Explore: clocks and calendars

In the Add tab, choose the number of hours kk, a starting time and an amount to add; the hand jumps to the remainder. Try k=24k = 24 and add 3030 (the overnight train), then k=7k = 7 (a week) and add 1616. In the Multiply tab, look for a 11 in each row. Which rows have one for k=7k = 7, k=11k = 11 and k=13k = 13? For k=10k = 10 and k=12k = 12? Press the button to reveal the pattern, then check it against Theorems 4 and 5.

The clock laboratory

On a clock with k hours, counting wraps around. Explore adding and multiplying.

01234567891011

9 + 5 = 14 ≡ 2 (mod 12)

14 hours forward on a 12-hour clock lands on 2: the remainder when 14 is divided by 12.

A calendar is a number grid with 7 columns. Every number in one column leaves the same remainder on division by 7. Mark the multiples of 7 and the multiples of 3: which columns do the multiples of 3 visit, and when does the pattern repeat?

A calendar is clock arithmetic

Mark the multiples of a number and look for patterns. Add a second number to find common multiples.

  • multiple of 3
  • multiple of 7
  • common multiple (both)

There are 28 multiples of 3 from 1 to 84.

Common multiples of 3 and 7: 21, 42, 63, 84. The smallest one, 21, is the least common multiple.

Tap a number to see how it is related.

Exercises

Exercises

1. Clock arithmetic: remainders and modular arithmetic

Warm-upHuman

A long-distance bus leaves Nairobi at 17:00 and the journey takes 30 hours. On the 24-hour clock, at what hour does it arrive? (Give a number from 0 to 23.)

2. Clock arithmetic: remainders and modular arithmetic

Warm-upHuman

Today is Tuesday. What day of the week will it be in 45 days?

Choose one

3. Clock arithmetic: remainders and modular arithmetic

CoreHuman

Without multiplying the numbers out, find (53⋅86) mod 9(53 \cdot 86) \bmod 9.

4. Clock arithmetic: remainders and modular arithmetic

CoreHuman

What is −23 mod 6-23 \bmod 6? (Remember that a remainder modulo 66 is one of 0,1,…,50, 1, \dots, 5.)

5. Clock arithmetic: remainders and modular arithmetic

CoreHuman

Which of these remainders have a multiplicative partner modulo 1010? (Choose all that apply.)

Choose all that apply

6. Clock arithmetic: remainders and modular arithmetic

CoreHuman

Find the partner of 33 modulo 1111: the number bb from 00 to 1010 with 3b mod 11=13b \bmod 11 = 1.

7. Clock arithmetic: remainders and modular arithmetic

CoreHuman

Amara claims: “If a⋅b mod n=0a \cdot b \bmod n = 0, then a mod n=0a \bmod n = 0 or b mod n=0b \bmod n = 0.” For which modulus does a counterexample exist?

Choose one

8. Clock arithmetic: remainders and modular arithmetic

StretchHuman

What is the last digit of 320263^{2026}?

9. Clock arithmetic: remainders and modular arithmetic

CoreFormal

In Lean, a % n is the remainder of a on division by n. Which statement says that the remainder of a product depends only on the remainders of the factors (Theorem 2)?

Choose one

10. Clock arithmetic: remainders and modular arithmetic

StretchComputational

Write a function partners(n) that returns the list of remainders a in range(n) that have a multiplicative partner modulo n — some b in range(n) with a * b % n == 1. Then use it to compare partners(12) with partners(13).

Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.
Common misconceptionsH

Common misconceptions

Misconception

“Modular arithmetic is only about 12-hour clocks.” Any modulus works: 24 for hours, 7 for weekdays, 10 for last digits, 9 for casting out nines, 11 for book numbers, 60 for minutes and for the Chinese calendar, 2 for even and odd. The 12-hour clock is just the most familiar picture.

Misconception

“If a⋅b mod n=0a \cdot b \bmod n = 0, then aa or bb must be 0 mod n0 \bmod n.” False in general: 3⋅4=12≡0(mod12)3 \cdot 4 = 12 \equiv 0 \pmod{12}, but neither 33 nor 44 is 00 modulo 12. The statement is true when nn is prime — that is Euclid's lemma.

Misconception

“A remainder can be any number.” A remainder on division by nn is always one of 0,1,…,n−10, 1, \dots, n - 1. So 25 mod 1225 \bmod 12 is 11, not 1313, and −7 mod 5-7 \bmod 5 is 33, not −2-2. (Some calculators and programming languages give −2-2 for −7 mod 5-7 \bmod 5; they use a different convention for negative numbers. Python gives 33.)

Misconception

“Every number has a partner, like fractions.” Only remainders with no common factor with nn have partners. Modulo 12, 22 has none: 2⋅b2 \cdot b is always even, and an even number never leaves remainder 11 on division by 1212.

ViewpointsH

Other ways of seeing

Algebra— A clock is a group

The hours of a 12-hour clock under addition form a cyclic group of order 12: adding is associative, 00 does nothing, every hour aa has an opposite 12−a12 - a that undoes it, and everything is generated by repeatedly adding 11. The remainders with multiplicative partners form a group under multiplication too: modulo 12 it is {1,5,7,11}\{1, 5, 7, 11\}, in which every element is its own partner. Modulo a prime pp, all p−1p - 1 non-zero remainders form a group, and the integers modulo pp are a field — a number system in which you can add, subtract, multiply and divide by anything non-zero. See groups and congruences.

Geometry— Turning a regular polygon

Turn a regular 12-sided polygon about its centre by 30∘30^\circ. Turning by a⋅30∘a \cdot 30^\circ and then by b⋅30∘b \cdot 30^\circ is turning by (a+b)⋅30∘(a + b) \cdot 30^\circ, and a turn by 12⋅30∘=360∘12 \cdot 30^\circ = 360^\circ is no turn at all. So the rotations of the polygon are the 12-hour clock: addition modulo nn is the geometry of rotating a regular nn-gon. Winding the number line around a circle, as in our picture, is the same idea: every whole number becomes an angle, and numbers differing by a multiple of nn give the same angle. See symmetry.

Computation— Secrets and fast powers

Computers do arithmetic modulo nn all the time, because the numbers never grow larger than nn. Theorem 2 lets a computer find ak mod na^k \bmod n for enormous kk by repeated squaring, reducing after every step — a few thousand multiplications for a number with hundreds of digits. That fast calculation is the engine of the public-key systems that protect online banking, such as RSA (published in 1977) and Diffie–Hellman key exchange (1976). See cryptography.

Set-theoretic connectionH

Remainder families as sets

Dividing by nn sorts every whole number — and every integer — into exactly nn families: the set of numbers with remainder 00, the set with remainder 11, and so on up to remainder n−1n - 1. Modulo 2 the families are the even and the odd numbers. These sets do not overlap and together they contain every integer: they form a partition. Theorems 1 and 2 say something remarkable about them: if you add a member of one family to a member of another, the family of the answer does not depend on which members you picked. That is why we can add and multiply the families themselves. The Remainder families trail explores this; at university it becomes the idea of an equivalence relation and a quotient (see equivalence relations).

Wonder Trails

Wonder Trails

History & global context

History and global context

  • China. The Sunzi Suanjing (Master Sun's Mathematical Manual, between the 3rd and 5th centuries CE) asks for a number of things that leaves remainder 2 when counted by threes, 3 when counted by fives and 2 when counted by sevens (answer: 23). In 1247 Qin Jiushao's Mathematical Treatise in Nine Sections gave a general method for such problems, today called the Chinese remainder theorem. Calendar-makers needed exactly this kind of calculation to find when several astronomical cycles line up.
  • India. Āryabhaṭa (499 CE) described the kuṭṭaka (“pulveriser”), a method for solving equations such as ax+c=bya x + c = b y in whole numbers — in modern terms, finding partners modulo bb. Brahmagupta (628 CE) and later Indian astronomers refined it.
  • Mesoamerica. The Maya combined a 260-day ritual count (13 numbers × 20 day-names) with a 365-day year; the same combination of dates recurs after 18,980 days, about 52 years.
  • East Asia. The 60-fold cycle of heavenly stems and earthly branches was used to count days in Shang-dynasty China more than 3000 years ago, and later to name years.
  • Europe. Carl Friedrich Gauss introduced the notation a≡b(modn)a \equiv b \pmod n and the word modulus in his Disquisitiones Arithmeticae (1801), turning a collection of tricks into a theory.
  • East Africa. Swahili timekeeping, counting hours from sunrise, is a living example of shifting a clock by a constant modulo 12.
Further reading

Further reading

  • Joseph H. Silverman, A Friendly Introduction to Number Theory (1997; 4th edition 2012), chapters on congruences — very readable for a strong secondary-school student.
  • Øystein Ore, Number Theory and Its History (1948), for the history from Sunzi to Gauss.
  • George Gheverghese Joseph, The Crest of the Peacock: Non-European Roots of Mathematics (1991; 3rd edition 2011), on Chinese remainder problems, the Indian kuṭṭaka and Maya calendars.
  • Carl Friedrich Gauss, Disquisitiones Arithmeticae (1801; English translation by Arthur A. Clarke, 1966), where congruences began as a theory.