PuzzleReels
Sections

The Thousand Lockers

The maths · Episode 28 · 2:22 · gives the answer

The puzzle

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 s flips every locker whose number is a multiple of s, 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?

The maths, properly

Locker n is flipped once by each student s whose number divides n, because student s flips exactly the multiples of s. So locker n is flipped as many times as n has divisors. It starts shut, so it ends open exactly when it was flipped an odd number of times.

Route one: run it

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 s flips ⌊1000/s⌋ 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.

A small run first

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 studentlockers 1 to 10
11, 1, 1, 1, 1, 1, 1, 1, 1, 1
21, 0, 1, 0, 1, 0, 1, 0, 1, 0
31, 0, 0, 0, 1, 1, 1, 0, 0, 0
41, 0, 0, 1, 1, 1, 1, 1, 0, 0
51, 0, 0, 1, 0, 1, 1, 1, 0, 1
61, 0, 0, 1, 0, 0, 1, 1, 0, 1
71, 0, 0, 1, 0, 0, 0, 1, 0, 1
81, 0, 0, 1, 0, 0, 0, 0, 0, 1
91, 0, 0, 1, 0, 0, 0, 0, 1, 1
101, 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.

Route two: pair up the divisors

The divisors of n come in pairs. If d divides n, so does n/d, and they are a pair. Take n=12, whose divisors are 1, 2, 3, 4, 6, 12. They pair as

1×12,2×6,3×4.

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 d and n/d are the same number, which is when d×d=n, so n is a perfect square. Take n=36. Its divisors are 1, 2, 3, 4, 6, 9, 12, 18, 36, and they pair as

1×36,2×18,3×12,4×9,6×6.

The first four are pairs and make eight flips. The last is a single divisor, 6, 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.

Counting to a thousand

The squares up to 1000 are 12,22,…,k2 with k2≤1000. Now 312=961 and 322=1024, which is too big, so k=31. That is 31 squares, and 961 is the last open locker. In symbols the number of squares up to n is ⌊n⌋.

How the flips build up

The total number of flips by the end of student s rises quickly at first, because the early students flip many lockers. The script records the running total at some of the students:

after studentflips so far
11000
21500
52283
102927
504479
1005142
5006569
10007069

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.

Checking it

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 full run on 1000 lockers leaves exactly 31 open, and they are the squares;
  • for every corridor length from 1 to 1000, the open lockers are exactly the squares up to that length;
  • for every n from 1 to 1000, the number of divisors of n is odd exactly when n is a perfect square;
  • the run on 10 lockers leaves 1, 4, 9 open;
  • the total number of flips is 7069;
  • the divisors and pairs of 36 and 12 are as shown above, with 36 open and 12 shut.

The check of divisor counts runs up to 1000. For all larger n the pairing argument above is the proof, and it does not depend on size.

Where this shows up

  • Odd and even counts. A common trick is to count something twice, in pairs, and see what is left over. Here the leftover is the square root.
  • Perfect squares. Having an odd number of divisors is another way to say a number is a perfect square. It is one of the standard facts of elementary number theory.
  • Parity of a toggle. Whenever something is switched over and over, only whether the number of switches is odd or even matters, so the whole history can be replaced by a count.

Try it yourself

1. Which lockers are open after 100 students and 100 lockers?

The perfect squares up to 100: 1,4,9,16,25,36,49,64,81,100. There are ten of them.

2. Locker 30 is flipped how many times, and what is its final state?

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.

3. How many lockers would be open if there were 10,000 lockers and students?

The squares up to 10,000 are 12 to 1002, so 100 lockers are open.