↑ ↓ to move · enter to open

008 · Easy · Conditionals, modulo

fizzbuzz

Count from 1 to 100. Say Fizz for multiples of 3, Buzz for multiples of 5, and FizzBuzz for multiples of both. The program is a few lines, but it only works if the conditions come in the right order. Underneath it is modular arithmetic, which is why the whole output repeats every 15 numbers.

LAB 1

the stream

The program walks n from 1 upward and runs the same if/elif chain on every number. The chain stops at the first test that is true, so the order of the tests decides the output. Step through it, then switch to the buggy order and watch the FizzBuzz cells turn into wrong answers.

Fizz (F)Buzz (B)FizzBuzz (FB)wrong outputcurrent n
100
—
Current n
—
Output
0
Mod checks so far
0
Wrong outputs

This step—

The chaintimes each line ran


          
  • the line that answered this n
  • faded: tested and failed
  • never ran

Why the 15 test goes first

Every multiple of 15 is also a multiple of 3. If the 3 test comes first, it catches 15, 30, 45 and the rest before the FizzBuzz test sees them, and that line never runs. The most specific test has to come first.

Testing n % 15 == 0 is the same as testing n % 3 == 0 and n % 5 == 0, because 3 and 5 share no factor. Lab 2 shows why. The work is one pass with at most 3 checks per number: O(n).

LAB 2

modulo is a clock

n mod 3 is where a hand lands on a clock with 3 positions after n ticks. Fizz happens whenever the 3-clock points at 0, and Buzz whenever the 5-clock does. Both hands reach 0 together only every lcm(3, 5) = 15 ticks, so the output repeats in blocks of 15. Change the two clocks to see when that block gets shorter.

mod 3 · mod 5
3 5
Try

How to read this: the left dial is the Fizz clock and the right dial is the Buzz clock. Each tick moves both hands one notch. When a hand points at the top (0), that word is said; the strip below colors every number the same way as Lab 1.

  • Fizz
  • Buzz
  • FizzBuzz
  • the current n
—
n
—
(n mod 3, n mod 5)
15
Period = lcm
—
Pairs reached this period

This tick—

Every pair of hand positions3 × 5

  • both clocks at 0: FizzBuzz
  • now

Why the period is the lcm

n ≡ 0 (mod a) and n ≡ 0 (mod b) ⇔ n ≡ 0 (mod lcm(a, b))

The output for n depends only on the pair (n mod a, n mod b). That pair returns to (0, 0) after exactly lcm(a, b) ticks, and from there everything repeats. When a and b share no factor, lcm = a·b and every pair in the table shows up once per period. That is the Chinese remainder theorem. The lcm itself comes from the gcd, which Euclid's algorithm finds in 003 · Number theory.

When they share a factor, the hands can never reach some pairs, and the period is shorter than a·b. Try 4 and 6.

LAB 3

your own rules

Add a third rule, say 7 → Bazz, and the if/elif chain from lab 1 needs a branch for every combination: FizzBuzzBazz, FizzBazz, BuzzBazz and so on, each in the right place. A loop that appends each matching word to a string needs none of that. Edit the rules below and the grid rebuilds. Hover or tap a cell to read its output.

Hover or tap a cell.
  • cell color: the rule that matched (swatches below)
  • striped cell: more than one rule matched, stripes in rule order
  • current n
Try
105 Row length
—
Period = lcm of divisors
—
Word combos that occur
—
Mod checks so far
—
Branches an if/elif chain needs

This step—

Concatenate, don't nest


          

Counts in 1..105counted · predicted

A word appears once for every multiple of its divisor, so its count is ⌊N / d⌋. Rule order changes only the word order (FizzBuzz or BuzzFizz). No rule can hide another.