Quant Memo
Core

Light-Bulb Switching Problems

A bulb ends up on or off depending on how many times it's toggled, and the toggle count is a divisor-counting question in disguise — which is why perfect squares are the odd ones out.

Prerequisites: How to Attack a Brainteaser

This puzzle looks like it needs simulation — 100 bulbs, 100 passes, track every flip — but the toggle count for each bulb reduces to a single number-theoretic fact once you stop thinking about passes and start thinking about divisors.

The puzzle

100 light bulbs, numbered 1 to 100, all start off. There are 100 people. Person kk walks along the row and toggles the switch on every bulb whose number is a multiple of kk (so person 1 toggles every bulb, person 2 toggles every even-numbered bulb, and so on up to person 100, who toggles only bulb 100). After all 100 people have gone, which bulbs are on?

Reframe the question. Bulb nn starts off, and its final state depends only on how many times it gets toggled — an odd number of toggles leaves it on, an even number leaves it off (it started off, and each toggle flips the state). Person kk toggles bulb nn exactly when kk divides nn. So the number of times bulb nn gets toggled equals the number of positive divisors of nn.

When is the divisor count odd? Divisors of nn normally pair up: if dd divides nn, so does n/dn/d, and these are two different numbers (dn/dd \ne n/d) unless d=n/dd = n/d, i.e. d=nd = \sqrt n. So divisors come in pairs except when nn is a perfect square, in which case n\sqrt n pairs with itself and contributes only one divisor to the count instead of two.

The answer. Bulb nn ends up on exactly when nn has an odd number of divisors, which happens exactly when nn is a perfect square. Among 1 to 100, that's 1,4,9,16,,1001, 4, 9, 16, \dots, 100 — the 10 perfect squares end up lit, and the other 90 bulbs are off.

divisors of 36 (a perfect square) 136 218 312 49 6 pairs only with itself 8 paired divisors + 1 unpaired = 9, an odd count
Every divisor of a non-square pairs off with a distinct partner; a square's square root has no partner, leaving an odd total and a bulb that ends up on.

"Toggled an odd number of times" is a divisor-counting question, and divisor counts are odd only for perfect squares — because divisors pair up as (d,n/d)(d, n/d) except when d=n/dd = n/d. Any puzzle phrased as "person kk acts on multiples of kk" reduces to this same pairing argument.

Don't simulate 100 people toggling 100 bulbs by hand. Reframe "how many times is bulb nn touched" as "how many divisors does nn have," and the parity argument answers the whole puzzle in three lines.

The reframing habit — trading a simulation for a closed-form count over divisors — is the same move behind sieve-based algorithms (like the Sieve of Eratosthenes) that avoid checking every number against every other number individually.

Related concepts

Practice in interviews

Further reading

  • Engel, Problem-Solving Strategies (ch. 1)
ShareTwitterLinkedIn