Math for Programming / Part 2: Math inside the computer

Remainders: the math of wrapping around

The leftover from a division problem turns out to be one of the most useful numbers in programming. It is how clocks, turn orders, and patterns repeat.

Lesson 9 of 13About 10 minutesBuilds on: long division

The part that is left over

Divide 17 cookies among 5 friends and each gets 3, with 2 left over. That leftover is the remainder, and programmers use it constantly. Most languages write it with the percent sign: 17 % 5 is 2. Mathematicians say “17 mod 5 is 2,” short for modulo.

Clock arithmetic

A clock is the classic example. If it is 10 o’clock, then 5 hours later it is not 15 o’clock; it is 3. We add, then keep only the remainder after dividing by 12: (10 + 5) mod 12 = 3. Numbers that wrap around like this appear wherever something repeats.

Worked example: days of the week

Number the days Sunday = 0, Monday = 1, … Saturday = 6. Today is Thursday (4). What day is it 10 days from now? (4 + 10) mod 7 = 14 mod 7 = 0, which is Sunday. And 100 days from now? (4 + 100) mod 7 = 104 mod 7 = 6, a Saturday.

Figure

Choose a number and a modulus.

Wrapping around a list

Games and apps often cycle through a list: whose turn is it, which song plays next, which frame of a walking animation to show. If there are n items numbered 0 to n − 1, the next index is (i + 1) % n. After the last item, the remainder sends you back to 0 automatically, with no special case needed.

GDScript
var players := ["Ana", "Ben", "Chen", "Dee"]
var turn := 0
func next_turn():
    turn = (turn + 1) % players.size()   # 3 -> 0
    print(players[turn], "'s turn")

Even, odd, and patterns

A number is even when n % 2 == 0. That simple test makes striped table rows, alternating colors, and checkerboards. On a grid, the square at (row, column) is dark when (row + column) % 2 == 1. Remainders by other numbers make longer patterns: every third enemy drops a coin when count % 3 == 0.

Common mistake

Negative numbers. In GDScript, JavaScript, and C, -1 % 5 is −1, not 4. That breaks “go to the previous item” at index 0. Godot provides posmod(-1, 5), which returns 4, and wrapi(value, min, max) for wrapping within a range. Python’s % already returns 4.

Bigger ideas built on remainders

Remainders are everywhere in computer science. Hash tables, the structure behind dictionaries, turn a key into a big number and then use the remainder to pick a storage slot. Many check digits on ID numbers are remainders that catch typing mistakes. Some random number generators are built from multiplication and remainders. Even internet encryption rests on arithmetic with remainders of enormous numbers.

Where this comes from in school math

Remainders come from long division in elementary school and return in Algebra II with polynomial division. Divisibility rules, factors, and multiples, the number sense behind many logic puzzles, are all statements about remainders.

Where you will see it

  • Turn order and playlists: cycling back to the start.
  • Time: converting 125 seconds into 2 minutes and 5 seconds with ÷ and %.
  • Grids: turning a single index into a row and column.
  • Security and data: hashing, checksums, and encryption.

Exercises

What is 47 % 6?

6 × 7 = 42, so the remainder is 5.

A game shows 250 seconds. How many minutes and seconds is that?

250 ÷ 60 = 4 whole minutes, and 250 % 60 = 10 seconds: 4:10.

A grid is 8 cells wide. Cell number 29 is in which row and column (counting from 0)?

Row = 29 ÷ 8 rounded down = 3. Column = 29 % 8 = 5.