100 Doors

A classic computer science puzzle

The problem

There are 100 doors in a hallway, all initially closed.

A person walks through the hallway 100 times. On the first pass they toggle every door (1, 2, 3, …, 100). On the second pass, every second door (2, 4, 6, …). On the third, every third door. And so on.

Question: after all 100 passes, which doors are still open?

Show the answer

Only the perfect squares: 1, 4, 9, 16, 25, 36, 49, 64, 81 and 100.

Door n is toggled once for each of its divisors, so it ends open exactly when it has an odd number of them. Divisors normally come in pairs (d and n/d), which is even — unless n is a perfect square, where the square root pairs with itself and goes uncounted twice.

Pass 0 of 100 Open 0 Toggled this pass 0
Closed
Open
Toggled on this pass

Door n is toggled on every pass that divides it.