Lably
Skip to content

Modular arithmetic

Computation modulo a fixed integer

  • 14 min read
  • 9 chapters

Learn it on Lably

Modular arithmetic modulo m consists of systematically replacing the results of additions, multiplications, and subtractions by the remainder of the division by m. A remarkable property of modular arithmetic is that the result of a computation does not depend on whether the division by m is performed after each operation, only once at the end of the computation, or at the end of the computation and after some intermediate results—typically when an intermediate result becomes too large.

Motivating example

A familiar setting exhibiting modular arithmetic is the hour hand on a 12-hour clock. If the hour hand points to 7 now, then 8 hours later it will point to 3. Ordinary addition would result in 7 + 8 = 15, but 15 reads as 3 on the clock face. This is because the hour hand makes one rotation every 12 hours and the hour number starts over when the hour hand passes 12. We say that 15 is congruent to 3 modulo 12, and we write 15 ≡ 3 (mod 12), so 7 + 8 ≡ 3 (mod 12).

Similarly, if one waits 8 hours and then 8 more hours (thus 16 hours in total), the clock will show the same time change as if one waited 4 hours. This is reflected by the identity 2 × 8 ≡ 4 (mod 12). After a wait of exactly 12 hours, the hour hand will be right where it started, so 12 acts as 0; one writes 12 ≡ 0 (mod 12).

Sign in to keep reading

8 more chapters are free with a Lably account.

More in Basics