Numbers and Arithmetic

Modular Arithmetic Explained Through Clocks and Calendars

Learn arithmetic that wraps around, using clocks and weekdays, then apply it to huge powers, check digits and simple equations.

Key takeaways

  • Two integers are congruent modulo n when their difference is a multiple of n.
  • You can add, subtract and multiply congruences, and reduce numbers at any stage to keep them small.
  • Division is only safe when the number you divide by shares no common factor with the modulus.
  • Weekdays, clock times, check digits and parts of modern cryptography all rest on remainders.

Modular arithmetic is arithmetic where numbers wrap around after reaching a fixed size, called the modulus. On a 12-hour clock, 5 hours after 9 o'clock is 2 o'clock, because 14 and 2 differ by a full turn of 12.

The same idea tells you what weekday falls 100 days from now, finds the last digit of an enormous power and catches typing errors in identification numbers. It keeps only the remainder, and that is often all you need.

The definition

Pick a positive whole number n, the modulus. Two integers a and b are congruent modulo n if a − b is divisible by n. We write

a ≡ b (mod n)
14 ≡ 2 (mod 12)      since 14 − 2 = 12
38 ≡ 3 (mod 7)       since 38 − 3 = 35
−3 ≡ 9 (mod 12)      since −3 − 9 = −12

Equivalently, a and b leave the same remainder when divided by n, using remainders from 0 to n − 1. The three-bar symbol ≡ was introduced by Carl Friedrich Gauss in his 1801 book on number theory, the Disquisitiones Arithmeticae.

Modulo n, every integer belongs to exactly one of n classes, represented by 0, 1, ..., n − 1. Modulo 2 these are simply the even and odd numbers.

Clocks

A 12-hour clock works modulo 12, with 12 playing the role of 0. Suppose it is 7 o'clock. What time will it show in 100 hours?

Divide: 100 = 8 × 12 + 4. The eight full turns change nothing, so you add 4, and the clock shows 11. A 24-hour clock works modulo 24, which also tracks day or night: 100 = 4 × 24 + 4.

Going backward is the same. Thirty hours before 3 o'clock is 3 − 30 = −27. Add 36 (three turns of 12) to get 9. Negative results are never a problem; you add multiples of the modulus until you land in the range you want.

The week as arithmetic modulo 7
6 Saturday Then back to 0 5 Friday Five days on 4 Thursday Four days on 0 Sunday Start of the count 1 Monday One day on 2 Tuesday Two days on 3 Wednesday Three days on 1 2 3 4 5 6 7 0 Sunday Start of the count 1 1 Monday One day on 2 2 Tuesday Two days on 3 3 Wednesday Three days on 4 4 Thursday Four days on 5 5 Friday Five days on 6 6 Saturday Then back to 0 7

Calendars

Weekdays repeat every 7 days, so count modulo 7. If today is Tuesday, what day is it in 100 days? Since 100 = 14 × 7 + 2, move forward 2 days to Thursday.

A common year has 365 days, and 365 ≡ 1 (mod 7). That is why a given date usually falls one weekday later the following year. A leap year has 366 ≡ 2, so dates that span February 29 jump two days.

Here is a worked example. January 1, 2026 was a Thursday. What weekday is July 4, 2026?

  1. Days before July in 2026: 31 + 28 + 31 + 30 + 31 + 30 = 181.
  2. July 4 is day 181 + 4 = 185 of the year, which is 184 days after January 1.
  3. 184 = 26 × 7 + 2, so move 2 days past Thursday.
  4. The answer is Saturday.

Month lengths can be reduced too. A 31-day month shifts the weekday by 3, a 30-day month by 2, and a 28-day February by 0.

Leap years and the long calendar cycle

The leap year rule of the Gregorian calendar is itself a set of remainder tests. A year is a leap year if it is divisible by 4, except that century years must also be divisible by 400. So 2000 was a leap year and 1900 was not.

leap  if  year ≡ 0 (mod 400)
      or  year ≡ 0 (mod 4) and year is not ≡ 0 (mod 100)

A span of 400 years contains 97 leap years, giving 400 × 365 + 97 = 146,097 days. That number is exactly 7 × 20,871. The weekdays and dates therefore line up again after every 400 years.

Remainders also explain why your birthday drifts across the week. Each ordinary year pushes it forward one day, and each intervening February 29 pushes it one more. Over four consecutive years containing one leap day, the total shift is 5 days. Calendars repeat on shorter cycles for many stretches, but a skipped leap day at a century year breaks those shorter patterns. When you compute dates before the Gregorian reform, which different countries adopted at different times, you also need to know which calendar was in use.

The rules of calculation

Congruences behave much like equations. If a ≡ b (mod n) and c ≡ d (mod n), then

a + c ≡ b + d (mod n)
a − c ≡ b − d (mod n)
a × c ≡ b × d (mod n)

The multiplication rule is easy to check. Write a = b + jn and c = d + kn for whole numbers j and k. Then ac = bd + n(bk + dj + jkn), which differs from bd by a multiple of n.

This means you may shrink numbers before you work with them. To find the remainder of 1,234 × 5,678 on division by 9, replace each factor by its remainder: 1,234 leaves 1 and 5,678 leaves 8. The product leaves 1 × 8 = 8. That is the basis of casting out nines.

It also explains divisibility rules. Since 10 ≡ 1 (mod 9), every power of ten is also 1, so a number and its digit sum leave the same remainder modulo 9. Since 10 ≡ −1 (mod 11), alternating digit sums test divisibility by 11.

Taming huge powers

What is the last digit of 7¹⁰⁰? Work modulo 10 and watch the powers of 7.

7¹ ≡ 7
7² ≡ 49 ≡ 9
7³ ≡ 9 × 7 = 63 ≡ 3
7⁴ ≡ 3 × 7 = 21 ≡ 1
the cycle has length 4, and 100 is a multiple of 4
7¹⁰⁰ ≡ 1  (mod 10)

The last digit is 1, even though the full number has dozens of digits. Likewise, 2³ = 8 ≡ 1 (mod 7), and 100 = 3 × 33 + 1, so 2¹⁰⁰ ≡ 2 (mod 7).

For general moduli, computers use repeated squaring, reducing after every multiplication so the numbers never grow large. A classical shortcut is Fermat's little theorem: if p is prime and a is not divisible by p, then a to the power p − 1 is congruent to 1 modulo p. With p = 7 and a = 3, 3⁶ = 729 = 104 × 7 + 1.

Where division goes wrong

You cannot always cancel. Modulo 6, we have 2 × 4 = 8 ≡ 2 and 2 × 1 = 2, so 2 × 4 ≡ 2 × 1. Yet 4 and 1 are not congruent modulo 6.

The trouble is that 2 and 6 share a factor. Dividing by a is legitimate when gcd(a, n) = 1, meaning a and n have no common factor above 1. In that case a has a multiplicative inverse, a number that multiplies with it to give 1.

a (mod 7) 1 2 3 4 5 6
Inverse 1 4 5 2 3 6

For example, 3 × 5 = 15 ≡ 1 (mod 7). So to solve 3x ≡ 4 (mod 7), multiply both sides by 5: x ≡ 20 ≡ 6. Check: 3 × 6 = 18 ≡ 4.

When the modulus is prime, every nonzero class has an inverse, and the arithmetic forms a field, as introduced in rings and fields. Other cases need the methods for solving linear congruences.

Using remainders to prove things impossible

Because remainders respect addition and multiplication, they can show that an equation has no whole-number solution at all, without searching forever.

Consider squares modulo 4. Every integer is congruent to 0, 1, 2 or 3, and their squares are 0, 1, 4 and 9, which reduce to 0, 1, 0 and 1. So a square leaves remainder 0 or 1. A sum of two squares can therefore leave only 0, 1 or 2.

It follows that no number of the form 4k + 3 is a sum of two squares. For instance, 1,000,003 leaves remainder 3 on division by 4, since 1,000,000 is a multiple of 4. You know without testing a single pair that no two squares add up to it.

The converse does not hold: leaving remainder 0, 1 or 2 does not guarantee that a number is such a sum. The number 21 leaves remainder 1 but cannot be written that way. The full criterion is explained in which numbers are sums of two squares.

Combining two cycles

Suppose a number leaves remainder 2 when divided by 3 and remainder 3 when divided by 5. List the numbers of the first kind: 2, 5, 8, 11, 14. The first that also leaves 3 modulo 5 is 8. The next is 23, since the pattern repeats every 15.

This is a small case of the Chinese remainder theorem: if two moduli share no common factor, every pair of remainders occurs exactly once in each block whose length is their product. Calendar calculations that interlock cycles of different lengths use the same idea.

A short practice set

Try these, reducing early to keep the numbers small.

  • What is 123 + 456 modulo 10? Only last digits matter: 3 + 6 = 9.
  • What time is it 50 hours after 5:00 on a 24-hour clock? Since 50 ≡ 2 (mod 24), it is 7:00, two days later.
  • What is the remainder of 99 × 98 on division by 100? Here 99 ≡ −1 and 98 ≡ −2, so the product is congruent to 2.
  • What is the remainder of 5²⁰ on division by 4? Since 5 ≡ 1 (mod 4), every power of 5 is 1.

Using a negative representative, as in the third item, is often the quickest route. Any member of a class may stand in for any other, so choose the one closest to zero.

Check digits

Many identifiers end with a digit computed from the others. The 13-digit book number, ISBN-13, multiplies the digits alternately by 1 and 3 and requires the total to be 0 modulo 10. A single mistyped digit changes the total by 1 or 3 times a nonzero difference between −9 and 9, which is never a multiple of 10, so every single-digit error is detected.

Some swaps of neighboring digits slip through. Schemes that use a prime modulus, such as the older ISBN-10 with modulus 11, catch more. See how check digits catch errors.

Cycles elsewhere

Anything periodic invites modular thinking. Musical pitch classes repeat every 12 semitones. Round-robin schedules rotate teams. Computers store fixed-width integers modulo a power of two, which explains why a counter can wrap back to zero. Some simple pseudorandom number generators repeatedly multiply and add modulo a large number.

Public-key cryptography also relies on it, using very large moduli and powers that are easy to compute but hard to reverse without secret information. A classroom version appears in RSA with small primes. Such small examples teach the structure and offer no security; real systems should rely on well-reviewed libraries.

Where remainders do the work
Arithmetic mod n Keep only the remainder Computers Counters wrap at powers of 2 Cryptography Powers with huge moduli Clocks Hours wrap at 12 or 24 Calendars Weekdays wrap at 7 Check digits Catch typing slips Arithmetic mod n Keep only the remainder Clocks Hours wrap at 12 or 24 Calendars Weekdays wrap at 7 Check digits Catch typing slips Cryptography Powers with huge moduli Computers Counters wrap at powers of 2

Common mix-ups

Confusing the relation and the operation. The statement a ≡ b (mod n) says two numbers are in the same class. Many programming languages have an operator, often %, that returns a remainder. For negative inputs, languages differ: −3 % 12 is 9 in some and −3 in others.

Reducing exponents with the same modulus. In a power, you may reduce the base modulo n, but not the exponent. Modulo 7, 2⁸ is not the same as 2¹, even though 8 ≡ 1: 2⁸ = 256 ≡ 4, while 2¹ = 2. Exponents repeat with their own cycle length.

Forgetting the modulus. The claim 14 ≡ 2 is true modulo 12 and false modulo 7. Always say which.

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 38 articles in Numbers and Arithmetic →

Latest news on this topic