PuzzleReels
Sections

The Hundred Prisoners and the Boxes

The maths · Episode 20 · 3:44 · gives the answer

The puzzle

100 prisoners, and 100 boxes in a row, numbered 1 to 100. Inside the boxes, one to a box and in an order nobody knows, are the prisoners' names.

One at a time, each prisoner goes into the room and opens 50 boxes. If he finds his own name he passes, puts everything back exactly as it was, and tells the others nothing. If all 100 pass, all 100 go free. If one fails, nobody does. They may agree a plan beforehand, and after that they are on their own.

Each man has a one-in-two chance. What chance do all 100 have?

The maths, properly

Write n for the number of prisoners, which is even, so each opens n/2 boxes. Number the prisoners like the boxes, and write σ(i) for the number of the name in box i. Then σ is a shuffle, a permutation of 1,…,n, and "an order nobody knows" means every one of the n! shuffles is equally likely.

The plan is the film's: prisoner k opens box k. If the name inside is j, he opens box j next, and so on, until he finds his own name or has opened n/2 boxes.

Route one: the plans that die

This is the film's first route, and the point of it is that two sensible plans are hopeless.

Everyone opens boxes 1 to 50. The names in those boxes are 50 different names, so exactly 50 prisoners pass, whatever the shuffle. The chance that all pass is zero.

Everyone opens 50 boxes at random. His name is in one box, and a random set of half the boxes contains it with probability

(n−1n/2−1)(nn/2)=n/2n=12

Whatever the shuffle, each man's choice is his own coin toss, independent of the others', so all n pass with probability 2−n. For 100 that is 7.89 × 10⁻³¹: a decimal point, 30 zeros, then 789. One chance in 1.27 nonillion.

The source story called 2−n the ceiling for every plan in which a man's boxes are fixed in advance. It is not, and the film does not say it. A fixed plan wins on the shuffles that put every name in its owner's chosen set, and the count of those is the permanent of a 0-1 matrix with n/2 ones in each row. Brègman's theorem bounds that by ((n/2)!)2, and splitting the prisoners into two halves, one half opening boxes 1 to n/2 and the other the rest, reaches it. The best fixed plan wins with probability 1/(nn/2), which for 100 is 1 in 100891344545564193334812497256. Better than random picks, and still no chance.

Route two: stop thinking about boxes

Draw an arrow from each box i to box σ(i), the box with the number of the name inside. Every box has one arrow out, and since every name is in exactly one box, one arrow in. Start anywhere and follow the arrows. There are only n boxes, so you must come back to a box you have seen, and the first one you come back to is where you started: any other box would then have two arrows in. So the arrows fall into loops, and every box is on exactly one.

A prisoner passes exactly when his loop has at most n/2 boxes. Prisoner k's j-th box is σj−1(k), the box j−1 steps round from box k, and the name inside it is σj(k). That is his own name, k, for the first time when j is the length L of the loop through box k. His name is in the box that closes his loop, he reaches it on step L and not before, and he never opens a box twice on the way. So he passes exactly when L≤n/2.

So all n pass exactly when no loop is longer than n/2. 100 separate fates have become one question about one shuffle.

Count the shuffles with a long loop. Fix k>n/2. A shuffle with a loop of exactly k boxes is made by choosing the k boxes, joining them in a loop, and shuffling the other n−k:

(nk)·(k−1)!·(n−k)!=n!k!(n−k)!·(k−1)!·(n−k)!=n!k

There are (k−1)! loops through k given boxes because, starting from the smallest, the other k−1 can follow in any order. No shuffle is counted twice: two loops longer than n/2 would need more than n boxes, so a shuffle has at most one, and it is the one the count chose. For the same reason the events "the long loop has k boxes", for different k, never overlap. So the chance of a loop of exactly k is 1/k, and

P(fail)=∑k=n/2+1n1kP(all free)=1−∑k=n/2+1n1k

For 100, the plan fails with probability 0.6881721793, about 69%, and works the other 31%: 0.3118278207.

Each man is still a coin toss

Fix prisoner k. The shuffles in which his loop has exactly j boxes are made by choosing, in order, the other j−1 boxes of the loop, in (n−1)(n−2)⋯(n−j+1) ways, and shuffling the remaining n−j:

(n−1)(n−2)⋯(n−j+1)·(n−j)!=(n−1)!

That does not depend on j, so his loop length is equally likely to be any of 1,…,n, and he passes with probability exactly 12, the same as with random picks. What changed is how the passes are shared out. When the plan fails, one long loop fails every man on it, so a lost prison loses more than 50 men at once. On average the number who fail is still

∑k=n/2+1nk·1k=n2

the same as under random picks. The loops do not make anyone likelier to pass. They make the failures arrive together, so that the rest of the time there are none.

Why the answer hardly depends on n

Write m=n/2 and Sm=1m+1+…+12m, the chance of failing. Since 1/x falls as x grows, for every k≥2

∫kk+1dxx<1k<∫k−1kdxx

Add these up for k from m+1 to 2m:

ln2m+1m+1<Sm<ln2mm=ln2

The left side tends to ln2, so Sm→ln2 and the chance of going free tends to 1−ln2. This is the familiar Hn≈lnn read twice: Hn−Hn/2≈lnn−ln(n/2)=ln2. The right-hand inequality says more: the chance is above 1−ln2 for every n. And it falls steadily on the way down, because adding two prisoners changes the chance of failing by

Sm+1−Sm=12m+1+12m+2−1m+1=12m+1−12m+2>0

So two prisoners have 1−12, an even chance, 100 have 31.2%, a thousand have 30.7%, and no number of them ever has less than 0.30685.

No plan does better

The film does not claim this, and the script does not check it. It is a theorem of Eugene Curtin and Max Warshauer (2006), and the idea fits in a paragraph.

Change the game. In the new game the prisoners go in one at a time, every box anyone opens stays open, and a prisoner whose name is already showing does nothing. Anyone else opens boxes until his name appears, and they win if nobody needs more than n/2. Two facts:

  1. Any plan for the real game does at least as well in the new one. In the new game, a prisoner just runs his real-game plan, reading already-open boxes for free. On any shuffle where he finds his name within n/2 openings in the real game, he does so here with at most as many new openings. So every shuffle the plan wins in the real game, it wins in the new one.
  2. In the new game every plan wins with the same chance, and it is the loops' chance. Whatever has been opened so far, the unopened boxes hold the unseen names in a uniformly random order. So when a prisoner with r unseen names left starts searching, each box he opens shows his name with chance one over the number still unseen, and his search lasts j boxes with chance 1/r for each j from 1 to r, whichever boxes he picks. The loop lengths of a random shuffle are built the same way: the loop through a given box has each length from 1 to r with chance 1/r, by the count above, and the rest is a random shuffle of what is left. So the search lengths have exactly the distribution of the loop lengths, and the new game is won with the chance that no loop is longer than n/2.

Put together: no plan for the real game beats 1−∑1/k, and the loops reach it.

A worked case: the film's ten boxes

The film draws ten boxes, opened five at a time, because ten fit on a screen. Its first shuffle:

Box12345678910
Name inside71019845362

Box 7 holds name 5, the film's first arrow. All ten arrows make three loops: 1, 7, 5, 8, 3; then 4, 9, 6; then 2, 10. Prisoner 7 opens boxes 7, 5, 8, 3, 1 and finds the names 5, 8, 3, 1, 7: his own on the last box of his loop. The longest loop has 5 boxes, no more than the five allowed, so in this shuffle all ten pass.

The second shuffle:

Box12345678910
Name inside41073219685

Its loops are 1, 4, 3, 7, 9, 8, 6, of 7 boxes, and 2, 10, 5. Prisoner 7 opens 7, 9, 8, 6, 1, finds 9, 8, 6, 1, 4, and is out of turns. His name was in box 3, the box that closes his loop, two steps further on. Everyone on the long loop fails the same way, and only the 3 on the short loop pass.

Over all 3,628,800 shuffles of ten boxes, 1,285,920 have no loop longer than five: a chance of 0.3543650794, about 35.4%. The longest loop has exactly 6, 7, 8, 9 or 10 boxes with chances 1/6, 1/7, 1/8, 1/9, 1/10, exactly as the count says.

Generalising

Any number of prisoners. The same formula, from two prisoners up. The small cases are counted by brute force over every shuffle:

PrisonersShufflesAll go freeChance
2210.500000
424100.416667
67202760.383333
840,32014,7360.365476
103,628,8001,285,9200.354365
100100!0.3118278207
10001000!30.7%

A thousand prisoners opening 500 boxes each still go free 30.7% of the time, and the limit is 0.30685.

More boxes each. If each prisoner may open m≥n/2 boxes, nothing in the proof changes: a loop longer than m is still longer than half, so there is at most one, and the chance is 1−∑k=m+1n1/k. With fewer than half, two long loops can share a shuffle, the count has to correct for the overlaps, and the chance drops fast. The script computes neither.

Checking it

scripts/prisoners.mjs asserts all of this before writing the numbers the film and this page use.

The exact answer. The sum is done in exact fractions with whole numbers of any size, and checked to the digits shown:

QuantityValue
Chance all go free, exactly21740752665556690246055199895649405434183/69720375229712477164533808935312303556800
As a decimal0.3118278207
Chance the plan fails0.6881721793

For every k above 50, up to 100, the count (nk)(k−1)!(n−k)! at n=100 is checked, as an exact integer, to be n!/k.

Every shuffle of a few boxes. For 2, 4, 6, 8 and 10 boxes the script goes through every shuffle and checks that the number with no long loop matches the formula and that the longest loop is exactly k in n!/k of them, for every k above half. Up to eight boxes it also walks the plan literally, man by man, in every shuffle, and checks that each man passes exactly when his loop is short: 96, 4320 and 322,560 walks for four, six and eight.

The plans that die. Opening the first half passes exactly half the men in every shuffle of 4, 6 and 8 boxes, and in 10,000 seeded shuffles of 100. Random picks are checked to pass one man in two, 2n and the zeros and digits of 2−n are checked as exact integers, and 10,000 seeded prisons of each plan produce 0 wins. For the best fixed plan the script tries every plan for four prisoners by brute force, and checks ((n/2)!)2/n!=1/(nn/2) exactly at n=100. It does not check Brègman's theorem, which is what says nothing fixed does better at 100.

The simulation. 1,000,000 seeded prisons of 100, the plan played on each: 311,644 go free, a rate of 0.311644 against the exact 0.3118278207. The script requires the two to agree within four and a half standard errors. Ten boxes and a thousand prisoners are simulated too, and land on their exact values. The 60 prisons on the film's screen are a consecutive run from that same simulation, each walked man by man, and every lost one loses more than 50 men.

The film's shuffles are generated from seeds, and the script checks both walks of prisoner 7 and who passes in each. It also checks that the narration says the numbers the maths says.

What is not checked. That no plan beats the loops is Curtin and Warshauer's theorem, sketched above; the script tests no other adaptive plan. The limit 1−ln2 is proved above; the script checks only that the thousand-prisoner value is within a thousandth of it.

Where this shows up

  • The puzzle was posed by Anna Gál and Peter Bro Miltersen in 2003, in a paper on the complexity of data structures. Eugene Curtin and Max Warshauer proved in 2006 that the loop plan is the best possible.
  • Cycles of random permutations. That a given element's cycle has a uniformly random length, and that a long cycle has length k with chance 1/k, are the first steps in the theory of the cycle structure of random permutations. The longest cycle of a large random shuffle takes up, on average, a fixed fraction of it, the Golomb–Dickman constant.
  • Correlation as a resource. The plan cannot make any one prisoner more likely to pass. It can only arrange for the passes to come together. The same idea is behind hat puzzles in which players guess their own hat colour: each guess is right half the time, and the art is in making the wrong ones coincide.

Try it yourself

1. Four prisoners, two boxes each. What is their chance with the loops?

They fail when the shuffle has a loop of 3 or 4, with chances 13 and 14. So they go free with chance 1−13−14=512, which is 10 of the 24 shuffles, or 0.416667.

2. Check the count for ten boxes and a loop of exactly six.

Choose the six boxes in (106)=210 ways, join them in a loop in 5!=120 ways, and shuffle the other four in 4!=24 ways: 210×120×24=604800. That is 10!/6, one in six of all shuffles.

3. The warden hears about the plan and chooses the shuffle himself, as one loop through all the boxes. What can the prisoners do?

Shuffle the box numbers themselves. Before they start, they secretly agree a random relabelling, so that prisoner k starts at box π(k) and goes from name j to box π(j). They are now following the loops of the shuffle "box, then the box the relabelling gives the name inside", which is uniformly random whatever the warden did. Their chance is back to 31.2%.

4. On average, how many prisoners fail with random picks, and how many with the loops?

Half of them in both cases, since each man fails with chance 12 either way. With random picks that half is spread over every prison. With the loops it is all or nothing: no failures at all 31% of the time, and more than 50 at once otherwise.