Skip to content

The 100 prisoners problem

Posted on:

A puzzle that sounds impossible at first reading: 100 prisoners are numbered 11 through 100100. There are 100100 sealed boxes in a room, each containing one of the numbers 11 through 100100, randomly assigned (each used exactly once). Each prisoner enters the room one at a time, opens at most 5050 boxes, and must find the box containing their own number. If even one prisoner fails, all 100100 are executed. The prisoners may agree on a strategy beforehand but may not communicate once the procession starts.

Random guessing gives each prisoner a 50/100=1/250/100 = 1/2 chance of finding their number, so the probability of all succeeding is (1/2)1008×1031(1/2)^{100} \approx 8 \times 10^{-31}. Effectively zero.

A specific strategy succeeds with probability 31.18%\approx 31.18\%.

The strategy

Number the boxes 11 through 100100. Prisoner ii first opens box ii. They read the number inside, say j1j_1. Next they open box j1j_1, read j2j_2, then box j2j_2, and so on, until they either find their own number ii or have opened 5050 boxes.

That is the entire strategy.

Where the success rate comes from

The contents of the boxes define a permutation π\pi of {1,,100}\{1, \ldots, 100\}, where π(i)\pi(i) is the number in box ii. Prisoner ii, following the strategy, traverses the cycle of π\pi that contains ii:

i    π(i)    π2(i)        πk(i)=i,i \;\to\; \pi(i) \;\to\; \pi^2(i) \;\to\; \cdots \;\to\; \pi^k(i) = i,

where kk is the length of that cycle. Prisoner ii finds their own number at step kk, which fits in the 5050-box budget iff k50k \le 50.

Because every prisoner uses the same strategy, all prisoners succeed iff every cycle of π\pi has length at most 5050. The fate of all 100100 prisoners is determined by a single property of one random permutation: whether its longest cycle exceeds 5050.

The probability

For a uniformly random permutation of {1,,n}\{1, \ldots, n\}, the probability that there is at least one cycle of length exactly \ell is 1/1/\ell when >n/2\ell > n/2. The counting argument: the number of permutations of {1,,n}\{1, \ldots, n\} that contain a fixed \ell-element subset arranged as a single cycle is (1)!(n)!(\ell - 1)! \cdot (n - \ell)!. The number of such \ell-subsets is (n)\binom{n}{\ell}. Multiplying and dividing by n!n! gives 1/1/\ell.

When >n/2\ell > n/2, at most one cycle of that length can exist (two would overflow the permutation). So the events “permutation has a cycle of length \ell” for distinct >n/2\ell > n/2 are disjoint, and

Pr[L>k]  =  =k+1n1  =  HnHk,k>n/2,\Pr[L > k] \;=\; \sum_{\ell = k+1}^{n} \frac{1}{\ell} \;=\; H_n - H_k, \qquad k > n/2,

where LL is the length of the longest cycle and Hm=1+1/2++1/mH_m = 1 + 1/2 + \cdots + 1/m is the mm-th harmonic number. As nn \to \infty with k=n/2k = n/2, this approaches ln20.6931\ln 2 \approx 0.6931, so

Pr[success]  =  1Pr[L>50]  n  1ln2    0.3069.\Pr[\text{success}] \;=\; 1 - \Pr[L > 50] \;\xrightarrow[n \to \infty]{}\; 1 - \ln 2 \;\approx\; 0.3069.

For n=100n = 100 specifically, the exact value is 1(H100H50)0.31181 - (H_{100} - H_{50}) \approx 0.3118.

The strategy converts the prisoners’ problem from (1/2)100(1/2)^{100} chaos into a single 31%31\% check on the cycle structure of a random permutation.

Press new trial to draw a random permutation. The grid below shows what each prisoner would find inside each box. Boxes are colored by which cycle of the permutation they belong to. The cycle outlined in red is the longest one. The trial succeeds if and only if no cycle exceeds 5050. Press + 1000 trials to accumulate statistics.

trials: 0successes: 0empirical: theoretical: 31.18%longest cycle this trial:

A note on optimality: this is also (asymptotically) the best possible strategy. No strategy can succeed with probability much above 1ln21 - \ln 2 in the large-nn limit (Curtin and Warshauer, 2006). The cycle-following strategy is essentially as good as the prisoners can do.

References



Previous Post
Why hypercubes look spiky
Next Post
Optimal message passing on sparse graphs