Locker Puzzle — Definition, Formula & Example
The locker puzzle is a classic brain teaser in which students walk down a hallway opening and closing lockers by a counting rule. The surprise is that only the lockers numbered with perfect squares stay open at the end.
Lockers numbered to start closed. For each , student toggles (opens if closed, closes if open) every locker whose number is a multiple of . Locker is toggled exactly times, where is the number of positive divisors of . It ends open if and only if is odd, which happens exactly when is a perfect square. So lockers end open.
Key Formula
Where:
- = The number of lockers, which is also the number of students
- = The floor function, which rounds down to the nearest whole number
Worked Example
Problem: A school hallway has 50 closed lockers and 50 students. Student 1 toggles every locker. Student 2 toggles lockers 2, 4, 6, and so on. Student toggles every multiple of . After all 50 students pass, which lockers are open?
Step 1: Student touches locker only when divides . So locker is toggled once for each of its divisors. A locker that starts closed ends open only after an odd number of toggles.
Step 2: Divisors usually come in pairs. Locker 12 has three pairs, so it gets 6 toggles and ends closed. Locker 16 has the pair , where the same number appears twice. That number counts only once, so locker 16 has an odd count of 5 and ends open.
Step 3: Only perfect squares have an unpaired divisor, so only they end open. List the perfect squares up to 50.
Answer: Seven lockers are open: 1, 4, 9, 16, 25, 36, and 49.
Why It Matters
The puzzle is a gentle introduction to number theory. It shows that a number has an odd count of divisors exactly when it is a perfect square. Versions of it appear often in contests such as MATHCOUNTS and AMC 8, where the trick is to recognize the divisor-pairing argument instead of simulating every student.
Common Mistakes
Mistake: Guessing that prime-numbered lockers stay open because so few students touch them.
Correction: A prime has exactly two divisors, and . Student 1 opens it and student closes it. Count divisors instead of guessing. The parity of that count, odd or even, decides the final state.
