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?
Write for the number of prisoners, which is even, so each opens boxes. Number the prisoners like the boxes, and write for the number of the name in box . Then is a shuffle, a permutation of , and "an order nobody knows" means every one of the shuffles is equally likely.
The plan is the film's: prisoner opens box . If the name inside is , he opens box next, and so on, until he finds his own name or has opened boxes.
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
Whatever the shuffle, each man's choice is his own coin toss, independent of the others', so all pass with probability . For 100 that is 7.89 × 10⁻³¹: a decimal point, 30 zeros, then 789. One chance in 1.27 nonillion.
The source story called 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 ones in each row. Brègman's theorem bounds that by , and splitting the prisoners into two halves, one half opening boxes 1 to and the other the rest, reaches it. The best fixed plan wins with probability , which for 100 is 1 in 100891344545564193334812497256. Better than random picks, and still no chance.
Draw an arrow from each box to box , 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 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 boxes. Prisoner 's -th box is , the box steps round from box , and the name inside it is . That is his own name, , for the first time when is the length of the loop through box . His name is in the box that closes his loop, he reaches it on step and not before, and he never opens a box twice on the way. So he passes exactly when .
So all pass exactly when no loop is longer than . 100 separate fates have become one question about one shuffle.
Count the shuffles with a long loop. Fix . A shuffle with a loop of exactly boxes is made by choosing the boxes, joining them in a loop, and shuffling the other :
There are loops through given boxes because, starting from the smallest, the other can follow in any order. No shuffle is counted twice: two loops longer than would need more than 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 boxes", for different , never overlap. So the chance of a loop of exactly is , and
For 100, the plan fails with probability 0.6881721793, about 69%, and works the other 31%: 0.3118278207.
Fix prisoner . The shuffles in which his loop has exactly boxes are made by choosing, in order, the other boxes of the loop, in ways, and shuffling the remaining :
That does not depend on , so his loop length is equally likely to be any of , and he passes with probability exactly , 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
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.
Write and , the chance of failing. Since falls as grows, for every
Add these up for from to :
The left side tends to , so and the chance of going free tends to . This is the familiar read twice: . The right-hand inequality says more: the chance is above for every . And it falls steadily on the way down, because adding two prisoners changes the chance of failing by
So two prisoners have , an even chance, 100 have 31.2%, a thousand have 30.7%, and no number of them ever has less than 0.30685.
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 . Two facts:
Put together: no plan for the real game beats , and the loops reach it.
The film draws ten boxes, opened five at a time, because ten fit on a screen. Its first shuffle:
| Box | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| Name inside | 7 | 10 | 1 | 9 | 8 | 4 | 5 | 3 | 6 | 2 |
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:
| Box | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| Name inside | 4 | 10 | 7 | 3 | 2 | 1 | 9 | 6 | 8 | 5 |
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.
Any number of prisoners. The same formula, from two prisoners up. The small cases are counted by brute force over every shuffle:
| Prisoners | Shuffles | All go free | Chance |
|---|---|---|---|
| 2 | 2 | 1 | 0.500000 |
| 4 | 24 | 10 | 0.416667 |
| 6 | 720 | 276 | 0.383333 |
| 8 | 40,320 | 14,736 | 0.365476 |
| 10 | 3,628,800 | 1,285,920 | 0.354365 |
| 100 | 100! | 0.3118278207 | |
| 1000 | 1000! | 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 boxes, nothing in the proof changes: a loop longer than is still longer than half, so there is at most one, and the chance is . 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.
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:
| Quantity | Value |
|---|---|
| Chance all go free, exactly | 21740752665556690246055199895649405434183/69720375229712477164533808935312303556800 |
| As a decimal | 0.3118278207 |
| Chance the plan fails | 0.6881721793 |
For every above 50, up to 100, the count at is checked, as an exact integer, to be .
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 in of them, for every 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, and the zeros and digits of 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 exactly at . 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 is proved above; the script checks only that the thousand-prisoner value is within a thousandth of it.
They fail when the shuffle has a loop of 3 or 4, with chances and . So they go free with chance , which is 10 of the 24 shuffles, or 0.416667.
Choose the six boxes in ways, join them in a loop in ways, and shuffle the other four in ways: . That is , one in six of all shuffles.
Shuffle the box numbers themselves. Before they start, they secretly agree a random relabelling, so that prisoner starts at box and goes from name to box . 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%.
Half of them in both cases, since each man fails with chance 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.