Mathwords logoMathwords

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 11 to nn start closed. For each k=1,2,…,nk = 1, 2, \dots, n, student kk toggles (opens if closed, closes if open) every locker whose number is a multiple of kk. Locker mm is toggled exactly d(m)d(m) times, where d(m)d(m) is the number of positive divisors of mm. It ends open if and only if d(m)d(m) is odd, which happens exactly when mm is a perfect square. So ⌊n⌋\lfloor \sqrt{n} \rfloor lockers end open.

Key Formula

Open lockers=⌊n⌋\text{Open lockers} = \lfloor \sqrt{n} \rfloor
Where:
  • nn = The number of lockers, which is also the number of students
  • ⌊⋅⌋\lfloor \cdot \rfloor = 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 kk toggles every multiple of kk. After all 50 students pass, which lockers are open?
Step 1: Student kk touches locker mm only when kk divides mm. So locker mm is toggled once for each of its divisors. A locker that starts closed ends open only after an odd number of toggles.
toggles on locker m=number of divisors of m\text{toggles on locker } m = \text{number of divisors of } m
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 4×44 \times 4, where the same number appears twice. That number counts only once, so locker 16 has an odd count of 5 and ends open.
12: 1⋅12, 2⋅6, 3⋅4  ⇒  616: 1⋅16, 2⋅8, 4⋅4  ⇒  512:\ 1\cdot 12,\ 2\cdot 6,\ 3\cdot 4 \;\Rightarrow\; 6 \qquad 16:\ 1\cdot 16,\ 2\cdot 8,\ 4\cdot 4 \;\Rightarrow\; 5
Step 3: Only perfect squares have an unpaired divisor, so only they end open. List the perfect squares up to 50.
1, 4, 9, 16, 25, 36, 49⌊50⌋=71,\ 4,\ 9,\ 16,\ 25,\ 36,\ 49 \qquad \lfloor \sqrt{50} \rfloor = 7
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 pp has exactly two divisors, 11 and pp. Student 1 opens it and student pp closes it. Count divisors instead of guessing. The parity of that count, odd or even, decides the final state.

Related Terms

  • Handshake Problem — Another classic puzzle solved by counting pairs cleverly
  • Lucky Number — Also acts on every kth number in a list
  • Missing Dollar Paradox — Recreational puzzle where careful counting beats intuition
  • Perfect Square — Only these locker numbers remain open at the end
  • Divisor — Each locker is toggled once per divisor of its number
  • Parity — Open or closed depends on whether the toggle count is odd