Bulb Switcher: only the squares stay lit
Problem. There are n bulbs, all off. In round i (from 1 to n), you
toggle every i-th bulb. After all n rounds, how many bulbs are on?
n = 3 → 1 (rounds leave [on, off, off])
Only bulbs 1, 4, and 9 survive — every perfect square, and nothing else:
\begin{tikzpicture}[font=\small,
on/.style={draw,thick,circle,minimum size=0.8cm,fill=yellow!45},
off/.style={draw,thick,circle,minimum size=0.8cm,fill=black!5}]
\node[on] at (0,0) {1}; \node[off] at (1,0) {2}; \node[off] at (2,0) {3};
\node[on] at (3,0) {4}; \node[off] at (4,0) {5}; \node[off] at (5,0) {6};
\node[off] at (6,0) {7}; \node[off] at (7,0) {8}; \node[on] at (8,0) {9};
\node[font=\scriptsize, text=black!60] at (4,-0.85) {yellow = on after 9 rounds};
\end{tikzpicture}
The one idea: toggles come in pairs, except on squares
Bulb i is toggled once in round d for every divisor d of i — round
d touches exactly the multiples of d. So bulb i ends on if and only if
it has an odd number of divisors:
\[ \text{on}(i) \iff d(i)\ \text{is odd} \]
Now pair each divisor d with its complement i/d. The pairs are all
distinct — unless a divisor pairs with itself, which happens exactly when
\(d = i/d\), i.e. \(d^2 = i\):
\[ d(i)\ \text{is odd} \iff i = k^2\ \text{for some } k \]
Every non-square’s divisors split into perfect pairs (even count, ends off); every square has those same pairs plus one loner in the middle (odd count, ends on). Counting lit bulbs is therefore counting squares:
\[ \text{answer} = \lfloor\sqrt{n}\rfloor \]
Why simulation explodes
The direct simulation keeps an array and replays every round:
on = [False] * (n + 1)
for i in range(1, n + 1):
for j in range(n):
if (j + 1) % i == 0:
on[j + 1] = not on[j + 1]
That is \(O(n^2)\) toggles and \(O(n)\) extra space — and the constraint allows
\(n = 10^9\), so this is \(10^{18}\) operations before breakfast. The simulation
replays structure the math already knows: the only question about bulb i
is the parity of its divisor count.
First rung: count divisors per bulb
Use the idea but not the pairing: for each bulb, count divisors up to \(\sqrt{i}\) (each divisor below the root pairs with one above it):
def count_divisors(i):
c = 0
d = 1
while d * d <= i:
if i % d == 0:
c += 1 if d * d == i else 2
d += 1
return c
sum(1 for i in range(1, n + 1) if count_divisors(i) % 2 == 1)
Correct, and the d * d == i branch is the pairing argument in code form —
but it costs \(O(\sqrt{i})\) per bulb, \(O(n\sqrt{n})\) total, still hopeless at
\(n = 10^9\). The pairing insight deserves better than being re-derived per
bulb: apply it once, globally, and the whole problem collapses to
counting squares.
The optimal version: count the squares
How many \(k\) satisfy \(k^2 \le n\)? Exactly \(\lfloor\sqrt{n}\rfloor\):
import math
def bulb_switch(n):
# bulb i ends on iff i is a perfect square: count k with k*k <= n
return math.isqrt(n)
math.isqrt is exact integer arithmetic — no float rounding to worry about,
\(O(1)\) time, \(O(1)\) space.
Dry run on n = 9
Step 1 — the first two rounds. Round 1 flips everything on; round 2 flips the evens back off. Already the pattern peeks through: odd rounds matter differently than even ones, because parity is all a bulb remembers.
\begin{tikzpicture}[font=\scriptsize,
on/.style={draw,thick,circle,minimum size=0.55cm,fill=yellow!45},
off/.style={draw,thick,circle,minimum size=0.55cm,fill=black!5}]
\node[font=\scriptsize\ttfamily, anchor=east] at (-0.3,0) {round 1};
\foreach \x in {0,...,8} { \node[on] at (0.6+\x*0.85,0) {}; }
\node[font=\scriptsize\ttfamily, anchor=east] at (-0.3,-0.85) {round 2};
\node[on] at (0.6,-0.85) {}; \node[off] at (1.45,-0.85) {};
\node[on] at (2.3,-0.85) {}; \node[off] at (3.15,-0.85) {};
\node[on] at (4.0,-0.85) {}; \node[off] at (4.85,-0.85) {};
\node[on] at (5.7,-0.85) {}; \node[off] at (6.55,-0.85) {};
\node[on] at (7.4,-0.85) {};
\node[font=\scriptsize, text=black!60] at (4.0,-1.55) {round 1 lights all 9; round 2 kills the evens};
\end{tikzpicture}
Step 2 — every bulb’s toggles, paired. Read each row as divisor pairs —
round d toggles bulb i exactly when d divides i:
| bulb | toggled in rounds (divisors) | count | final |
|---|---|---|---|
| 1 | 1 | 1 | on |
| 2 | 1, 2 | 2 | off |
| 3 | 1, 3 | 2 | off |
| 4 | 1, 2, 4 | 3 | on |
| 5 | 1, 5 | 2 | off |
| 6 | 1, 2, 3, 6 | 4 | off |
| 7 | 1, 7 | 2 | off |
| 8 | 1, 2, 4, 8 | 4 | off |
| 9 | 1, 3, 9 | 3 | on |
Bulb 6 pairs up evenly — \(1 \times 6\), \(2 \times 3\) — while bulb 9 has the pair \(1 \times 9\) plus \(3 \times 3\), where 3 pairs with itself (blue ring):
\begin{tikzpicture}[font=\small,
p/.style={draw,thick,circle,minimum size=0.6cm,font=\small},
a/.style={->,thick,blue}]
\node[p] at (0,0) {1}; \node[p] at (1,0) {2};
\node[p] at (2,0) {3}; \node[p] at (3,0) {6};
\draw[a] (0,0.32) to[bend left=25] (3,0.32);
\draw[a] (1,0.32) to[bend left=25] (2,0.32);
\node[font=\scriptsize] at (1.5,-0.65) {6: pairs up, even, off};
\node[p] at (5.5,0) {1}; \node[p] at (6.5,0) {3}; \node[p] at (7.5,0) {9};
\draw[blue,thick] (6.5,0) circle (0.42);
\draw[a] (5.5,0.32) to[bend left=25] (7.5,0.32);
\node[font=\scriptsize] at (6.5,-0.65) {9: 3 pairs with itself, on};
\end{tikzpicture}
Step 3 — collect the survivors. The lit bulbs are exactly \(1\), \(4\), \(9\) — the squares \(1^2, 2^2, 3^2\). There are 3 of them, and \(3 = \lfloor\sqrt{9}\rfloor\):
\begin{tikzpicture}[font=\small,
s/.style={draw,thick,circle,minimum size=0.62cm,fill=green!30},
n/.style={draw,thick,circle,minimum size=0.62cm}]
\node[s] at (0,0) {1}; \node[n] at (0.9,0) {2}; \node[n] at (1.8,0) {3};
\node[s] at (2.7,0) {4}; \node[n] at (3.6,0) {5}; \node[n] at (4.5,0) {6};
\node[n] at (5.4,0) {7}; \node[n] at (6.3,0) {8}; \node[s] at (7.2,0) {9};
\node[font=\scriptsize] at (3.6,-0.8) {squares $\le 9$: $1^2, 2^2, 3^2$ $\to$ answer 3};
\end{tikzpicture}
The finished artifact, so you can verify without re-reading the diagrams:
bulb: 1 2 3 4 5 6 7 8 9
final: 1 0 0 1 0 0 0 0 1 ← three on, answer 3
The code in Go
import "math"
func bulbSwitch(n int) int {
// bulb i ends on iff i is a perfect square: count k with k*k <= n
r := int(math.Sqrt(float64(n)))
for (r+1)*(r+1) <= n {
r++
}
for r*r > n {
r--
}
return r
}
Same logic as isqrt — float square root, then at most a step or two of
integer correction so large n can never round the wrong way. Same comment,
same variable, no extra tricks.
Complexity
| Approach | Time | Extra space |
|---|---|---|
| Round-by-round simulation | \(O(n^2)\) | \(O(n)\) |
| Divisor counting per bulb | \(O(n\sqrt{n})\) | \(O(1)\) |
| Counting squares (\(k^2 \le n\)) | \(O(\sqrt{n})\) | \(O(1)\) |
| Integer square root | \(O(1)\) | \(O(1)\) |
The ladder’s moral: each rung reuses the previous rung’s insight one level higher — toggles, then divisors, then the pairing, then pure arithmetic.
Edge cases
- No bulbs (
n = 0): nothing to toggle, answer 0 —isqrt(0) = 0with no special case. - One bulb (
n = 1): toggled once in round 1, stays on — and 1 is \(1^2\), so the formula agrees. - Perfect-square
n(e.g.n = 9 → 3): the last bulb is itself on, which is why the bound is \(k^2 \le n\) and not \(< n\). - Max constraint (
n = 10^9): \(\lfloor\sqrt{10^9}\rfloor = 31622\), since \(31622^2 = 999950884 \le 10^9 < 1000014129 = 31623^2\) — and this is exactly where floatsqrtwithout the correction loop can round wrong.
A follow-up asks about distinct bulb configurations after a few rounds on many bulbs — same toggle model, different counting, and the pairing argument above is the first thing to reach for.