← Math problems

The 100 prisoners problem Each one has 50%. All together have 31%.

Each of 100 prisoners gets only 50 tries to find their number among 100 boxes. Searching at random, all of them succeed with probability 7.9·10⁻³¹, which is practically never. But there is a simple strategy that wins almost a third of the time. Check it with a game, a simulation and four explanations.

7.9·10⁻³¹chance with random search
31.2%chance with the loop strategy

The puzzle

A hundred prisoners, a hundred boxes, one chance

The computer scientists Anna Gál and Peter Bro Miltersen first studied the problem in 2003. It looks hopeless: each prisoner finds their number with probability 1/2 whatever they do. Yet together they can win almost a third of the time.

  1. 1

    Prisoners and boxes

    A prison holds 100 prisoners numbered 1 to 100. A room holds 100 boxes with the same numbers on their lids.

  2. 2

    Numbers inside

    The warden puts one slip with a prisoner's number in each box. The slips are shuffled at random, and each number is in exactly one box.

  3. 3

    The search

    The prisoners enter the room one at a time. Each may open 50 boxes to look for their own number. Then they leave, and the room is put back as it was.

  4. 4

    The win condition

    If every one of the 100 prisoners finds their number, all go free. If even one fails, all stay in prison. They may agree on a plan in advance, but cannot communicate once it starts.

Intuition says

"Each one succeeds with chance 1/2, so together it's (1/2)¹⁰⁰. That's zero"

For random search that's true. But the prisoners can agree on a strategy that links their results: either they all succeed together, or many fail at once. Then the overall chance is 31.2%.

The loop strategy

  • Open the box with your own number.
  • Read the number on the slip inside.
  • Open the box with that number, and repeat.

The slips are placed at random, and the warden doesn't know the plan. What happens if he does is in round 5.

Round 1 · Guess

What are the prisoners' best odds?

Suppose the prisoners pick the best possible strategy. What is the probability that all 100 go free? Lock in your answer before reading on.

1%
0%25%50%75%100%
Round 2 · Play a prisoner

20 boxes, 10 tries

Let's shrink the prison: 20 prisoners, 20 boxes, each opens 10. You are one of the prisoners. Look for your number with the loop strategy or open any boxes you like. Then run all 20 prisoners and see whether they go free.

Your number7
Tries left10
Your games–

All 20 prisoners

Round 3 · Simulation

10,000 random arrangements

The computer shuffles 100 numbers into boxes thousands of times and finds the longest loop in each arrangement. The prisoners win when it is at most 50 long. The bars are the simulation; the line is the exact formula.

Length of the longest loop

≤ 50: everyone goes free ≥ 51: the prisoners lose exact formula
Simulation · all go free
–
Exact formula
–

What about 1,000 or a million prisoners?

exact formula simulation, 2,000 arrangements per dot limit 1 − ln 2

Each prisoner opens half of the boxes. More prisoners means slightly lower odds, but they never fall below 1 − ln 2 ≈ 30.69%. Even a million prisoners win almost a third of the time.

Round 4 · Four explanations

Why the loop works

The strategy doesn't change any single prisoner's chance: it stays 1/2. It changes how those chances are linked to each other.

An arrangement is made of loops

An arrow from each box to the number inside

Exactly one arrow leaves each box and exactly one enters it, because each number is in exactly one box. So the arrows always close up into loops. Mathematicians call them the cycles of the permutation.

Your loop

You find your number if your loop is at most 50 long

You start at the box with your number. It sits on some loop, and you walk along it. The last box of the loop points back to yours, so it holds your number. You always find your number when the loop closes. The only question is whether 50 tries are enough.

So the prisoners win if the arrangement has no loop longer than 50. There can be at most one long loop: two loops of 51 don't fit into 100 boxes.

Results are linked

All win together, or many lose together

Random: each one independent7.9·10⁻³¹chance all 100 succeed
Loop: fates linked31,2%chance all 100 succeed

With either strategy, 50 prisoners fail on average. But with the loop strategy the failures bunch up in the same arrangements: everyone on a long loop fails together. When the prisoners lose, about 73 of them fail on average. In exchange, in 31% of arrangements nobody fails.

Counting long loops

The chance of a loop of length k is 1/k

For k from 51 to 100, the chance that the arrangement has a loop of length exactly k is 1/k. These events don't overlap, since there's only one long loop. So the chance of losing is 1/51 + 1/52 + … + 1/100 ≈ 68.8%.

For formula lovers

The exact answer and its limit

Permutations with a loop of length k > n/2 = C(n, k) · (k − 1)! · (n − k)! = n! / k
P(lose) = 1/51 + 1/52 + … + 1/100 = H100 − H50 ≈ 0,6882
P(all free) = 1 − (H100 − H50) ≈ 0,3118
n → ∞:  Hn − Hn/2 → ln 2  ⇒  P → 1 − ln 2 ≈ 0,3069
P(random) = (1/2)100 ≈ 7.9·10⁻³¹

Here Hn = 1 + 1/2 + … + 1/n is a harmonic number. To build a loop of length k, choose k boxes, join them in a circle in (k − 1)! ways, and arrange the rest any way you like. This strategy is the best possible: no other plan gives better odds.

Round 5 · Harder versions

Other numbers of tries and a malicious warden

What if the prisoners may open more or fewer boxes? And what should they do if the warden knows their plan and places the numbers on purpose?

n prisoners, each opens k boxes

With the loop strategy
At random

Chance by number of tries

loop strategy at random

The malicious warden

The warden has learned about the loop strategy. He places the numbers so that all 100 boxes form one big loop. Then no prisoner finds their number in 50 tries.

All free, 10,000 runs
Longest loop the prisoners see

What if they get 60 tries?

100 prisoners. Press "Check": 10,000 simulated arrangements show the exact formula is right.

Tries eachAll free (exact)
Bonus · Fun facts

Things to tell your friends

2003

A puzzle from a research paper

Anna Gál and Peter Bro Miltersen described the problem in a paper on data structures at the ICALP 2003 conference. Their version used coloured strips of paper, not prisoners. Their Aarhus University colleague Sven Skyum pointed them to the loop strategy.

2006

It can't be beaten

Eugene Curtin and Max Warshauer proved that the loop strategy is optimal: no other plan gives the prisoners better odds. The proof appeared in The Mathematical Intelligencer in a paper called "The locker puzzle".

YouTube

The puzzle that went viral

The Veritasium channel brought the puzzle to a wide audience. The video called it a riddle that seems impossible even once you know the answer.

9.33·10¹⁵⁷

That's how many arrangements exist

100 numbers can be placed in 100 boxes in 100! ways. That number has 158 digits. In about 31% of them the longest loop is at most 50.

≈ 73

They lose as a crowd

When the loop strategy fails, about 73 prisoners fail on average: everyone on the long loop. With random search about 50 fail, but almost never zero.

30.69%

A million prisoners is fine

However many prisoners there are, if each opens half of the boxes, the chance that all go free never drops below 1 − ln 2 ≈ 30.69%. For 100 prisoners it's 31.18%; for 1,000 it's 30.74%.

Discussion

What do you think?

Share your own explanation, an objection or a variant of the puzzle. Commenting needs a GitHub account.

Newsletter

New puzzles, explanations and discussions

I keep adding new puzzles, new ways to explain them and write-ups of the discussions they start. Leave your email and I'll write when something new is out. No spam, one-click unsubscribe.

The sign-up form will appear here soon.