A corridor has 1000 lockers, numbered 1 to 1000, all shut. 1000 students walk down it in turn. Student 1 flips every locker, opening them all. Student 2 flips every second locker, so 2, 4, 6 and so on. Student 3 flips every third. Student flips every locker whose number is a multiple of , and student 1000 flips only locker 1000. A flip opens a shut locker and shuts an open one. When the last student has passed, which lockers are open?
Locker is flipped once by each student whose number divides , because student flips exactly the multiples of . So locker is flipped as many times as has divisors. It starts shut, so it ends open exactly when it was flipped an odd number of times.
The script plays the whole thing out: for each student, flip each of that student's lockers. There are 7069 flips in total across the 1000 students, because student flips lockers and those add up to 7069. At the end 31 lockers are open: 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, and so on up to 961. They are exactly the perfect squares up to 1000, and there are no others.
The script also runs every corridor length from 1 to 1000 lockers, not only 1000, and the open lockers are always the squares up to that length.
Running it finds the pattern. It does not say why the pattern holds, or whether it carries on beyond 1000.
With only 10 lockers, the film shows the whole thing. Each row below is the state of lockers 1 to 10 after that student has passed, with 1 for open and 0 for shut:
| after student | lockers 1 to 10 |
|---|---|
| 1 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 |
| 2 | 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 |
| 3 | 1, 0, 0, 0, 1, 1, 1, 0, 0, 0 |
| 4 | 1, 0, 0, 1, 1, 1, 1, 1, 0, 0 |
| 5 | 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 |
| 6 | 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 |
| 7 | 1, 0, 0, 1, 0, 0, 0, 1, 0, 1 |
| 8 | 1, 0, 0, 1, 0, 0, 0, 0, 0, 1 |
| 9 | 1, 0, 0, 1, 0, 0, 0, 0, 1, 1 |
| 10 | 1, 0, 0, 1, 0, 0, 0, 0, 1, 0 |
The lockers left open are 1, 4, 9. Two guesses fail at once. It is not half the lockers, and it is not the primes: locker 7 is opened by student 1, shut by student 7, and no one else touches it, so every prime ends shut.
The divisors of come in pairs. If divides , so does , and they are a pair. Take , whose divisors are 1, 2, 3, 4, 6, 12. They pair as
Three pairs is six flips: an even number, so the locker is shut again. The same happens for any number whose divisors all pair up with a different partner.
The pairing fails only when and are the same number, which is when , so is a perfect square. Take . Its divisors are 1, 2, 3, 4, 6, 9, 12, 18, 36, and they pair as
The first four are pairs and make eight flips. The last is a single divisor, , standing alone, which is one more flip, nine in all, and the locker is open.
In general: a number that is not a square has an even number of divisors, and a perfect square has an odd number, because exactly one divisor, the square root, has no partner. So a locker is open at the end exactly when its number is a square. That is the claim, and it holds for every number of lockers, not only for 1000.
The squares up to 1000 are with . Now and , which is too big, so . That is 31 squares, and 961 is the last open locker. In symbols the number of squares up to is .
The total number of flips by the end of student rises quickly at first, because the early students flip many lockers. The script records the running total at some of the students:
| after student | flips so far |
|---|---|
| 1 | 1000 |
| 2 | 1500 |
| 5 | 2283 |
| 10 | 2927 |
| 50 | 4479 |
| 100 | 5142 |
| 500 | 6569 |
| 1000 | 7069 |
The first student alone does 1000 flips, and the last student does one. The total is the sum of the number of divisors of each locker number, counted the other way round.
scripts/lockers.mjs asserts the claims below before it writes maths.json, and its --check fails if the file is stale. It checks that:
The check of divisor counts runs up to 1000. For all larger the pairing argument above is the proof, and it does not depend on size.
The perfect squares up to 100: . There are ten of them.
The divisors of 30 are 1, 2, 3, 5, 6, 10, 15 and 30. That is eight, so locker 30 is flipped eight times and is shut.
The squares up to 10,000 are to , so 100 lockers are open.