The riddle is so counterintuitive that it still seems wrong even if you know the answer. You'd think it's an almost impossible number. I feel like you're about to hit me with some truth bombs. I mean, if you're trying to create controversy and you're trying to confuse people, you're gonna succeed. There are a bunch of YouTube videos about it, but I find all of them either incorrect or incomplete, so in this video I'm going to dive deeper and explain it fully.
Here is the setup. Say there are 100 prisoners, numbered one to a hundred. Slips of paper containing each of their numbers are randomly placed in a hundred boxes. In a sealed room, one at a time, each prisoner is allowed to enter the room and open any 50 of the hundred boxes, searching for their number. Afterwards, they must leave the room exactly as they found it, and they can't communicate in any way with the other prisoners. If all 100 prisoners find their own number during their turn in the room, they will all be freed, but if even one of them fails to find their number, they will all be executed. The prisoners are allowed to strategize before any of them goes into the room. So what is their best strategy?
If they each search for their own number randomly, then each prisoner has a 50 percent chance of finding it, so the probability that all 100 prisoners find their numbers is one-half times a half times a half, a hundred times, or a half to the power of a hundred. This is equal to point zero zero zero zero zero zero zero zero thirty zeros and then an eight. To put this probability into perspective, two people have a better chance of picking out the same grain of sand from all the beaches and deserts on Earth than of escaping this way. But what if I told you that with the right strategy, there's a way to raise their chances to nearly one in three? It improves their odds over random chance by nearly 30 orders of magnitude. That's like taking a millimeter and scaling it up to the diameter of the observable universe. But they can only coordinate this strategy beforehand, correct? Is this true? Yes. Teach me this. This is not a trick question. The solution just involves an incredible feature of math.
So what is this mathematical strategy? If you don't already know the answer, feel free to pause the video here and try it for yourself. If you don't come up with it, don't worry, you're in good company. Even the person who came up with this riddle, computer scientist Peter Borgstrom Milterson, didn't even think of this strategy until a colleague pointed it out. Milterson ultimately published this problem in a paper where he generously left the solution as an exercise for the reader.
So here is the solution. Pretend you are one of the prisoners. When you go into the room, open the box with your number on it. The number on the slip inside probably won't be yours, but that's okay. Go to the box with that number on it, look at the number inside, then go to the box with that number on it, and so on. Keep doing this until you find the slip with your number. If you find your number, that essentially tells you to go back to the box where you started. It closes the loop of numbers you've been following. But if you've found your number, then you're done. You can stop and leave the room. This simple strategy gives over a 30 percent chance that all the prisoners will find their number. The entire pool has a 30 percent chance that everyone can find their number, a 31 percent chance of success. What? But how does it work?
The first thing to notice is that all boxes become part of a closed loop. The simplest loop would be a box that contains its own number. If you're prisoner number one and you go to box one, and it contains slip one, then you're done. Your number was part of a loop of one. But you could also have a loop of two, say box one points to box seven, and box seven points back to box one. Or you could have a loop of three or four or five, or any length, all the way up to one hundred. The longest loop you could have would connect all the numbers in a single loop. But more generally, any random arrangement of the slips in these boxes will result in a mixture of some shorter and some longer loops. When you start with a box labeled with your number, you are guaranteed to be on the loop that includes your slip.
So the thing that determines whether or not you find your slip is the length of the loop. If your number is part of a loop that is shorter than 50, then you will definitely find your slip. But if your number is part of a loop that is 51 or longer, you're in trouble. You won't find it before you've exhausted the 50 boxes you're allowed to search. When you open the box labeled with your number, you are in fact starting at the farthest point on the loop from your slip. You want to know where the slip that points to this box is, but to find it you have to follow the loop of numbers all the way around to the end. That means if the prisoners follow this strategy and the longest loop is 51, not just one or two prisoners will fail to find their number. All 51 on this loop won't make it. They make it to the box just before the box with their slip, but they have to stop searching there. So the probability that all of the prisoners succeed is just the probability that a random arrangement of a hundred numbers contains no loops longer than 50.
Now, I promised that this probability would come out to around one in three. But how do we calculate it? Well, imagine writing down all the different ways that you could connect a hundred boxes to form a loop of length 100. You could have box one points to box two, box two points to box three, box three to box four, and so on all the way to 100, and then box 100 would point back to box one. Or you could have something random: box 5 points to box 99, box 99 to box 17, and so on. Let's pick the last one: it is 63, and box 63 points back to box 5. So how many different arrangements of these hundred boxes, or permutations, could you have? For the first box, I have a hundred different boxes that I could choose from. For the second box, because I've already used one, I can only pick from 99 boxes. The next one I can pick from 98 boxes, and so on down to the very last box, where I don't really have a choice. There's only one box left I could put in the last position. So the total number of different permutations would be 100 times 99 times 98 times 97, all the way down to 1. That is just 100 factorial. There are 100 factorial different ways that you could create a loop of a hundred boxes.
But what we can't forget is that these are not just lines of numbers, they are loops. So some of these lines that look different are actually the same loop. For example, two, three, four, five, and so on up to a hundred, and then one, is the same thing as one, two, three, four, five, to a hundred. You can rearrange the way you write these numbers a hundred different ways, but they all represent the same loop. So the total number of unique loops of length 100 is 100 factorial divided by 100.
So what is the probability that any random arrangement of 100 boxes will contain a loop of length 100? It's just equal to the number of unique such loops that we just calculated, 100 factorial over 100, divided by the total number of ways that you could put 100 slips in 100 boxes, which is 100 factorial. So the answer is one over a hundred. There is a one percent chance that a random arrangement of slips results in a loop of length 100. This is a general result. The probability that you get a loop of length 99 is 1 over 99. The probability that you get a loop of length 98 is 1 over 98. So the probability that there is a loop longer than 50 is 1 over 51 plus 1 over 52 plus 1 over 53, and so on. Add all these up, and it equals 0.69. There is a 69 percent chance of failure for the prisoners, meaning a 31 percent chance of success, where the longest loop is 50 or shorter.
I still find it difficult to believe. This feels a bit like magic. Using the loop strategy, all the prisoners are more likely to find their numbers than even just two prisoners choosing at random. So using the loop strategy, what is the probability that each prisoner alone finds their number? It is still 50 percent. Each prisoner can still only open half the boxes, so their individual chance is still one half. But these probabilities are no longer independent of each other. Imagine running this experiment a thousand times. If everyone is guessing randomly, you'd expect that on most runs around 50 prisoners would find their number. On lucky runs the number would be a bit higher, on unlucky runs a bit lower. But using the loop strategy, all of the prisoners would find their numbers 31 percent of the time, and 69 percent of the time fewer than 50 find their number. The prisoners all win together or the majority loses together. That's how this strategy works.
Why are you assuming that your number will always be on the loop that you're on? I still understand that this is a key question, right? I feel like it's possible to start and go on a complete loop and not come back to your own number, because you got on the wrong loop, and then you have to get on another loop. So I don't know that I buy this. Right, right, right, right.
Now, the big question everyone asks is: how do you know that if you start with the box with your number on it, you are guaranteed to be on the loop that contains your slip? Well, if you think about it, the slip that says 73, if anyone sees that, they will definitely go to the box with the number 73. So the slip and box with the same number essentially form a unit. They're like a little Lego brick. Then every slip is hidden inside another box. So as I start laying out slips and boxes randomly, you can see that there's no way that we can end up with a dead end. It's not like you can just get to a box and then stop, because every box contains a slip that points at another box. So the only way for you to see only boxes when you walk into the room is for every slip to be contained within a box, and that necessarily will mean that we are forming loops. So when I start with box 73, I must eventually find slip 73, because then and only then will I be directed to go back to box 73, which closes the loop.
Who is the warden of this prison? What kind of sadistic mathematical warden are you dealing with here? This is awful. Now, what if there is a sympathetic prison guard who sneaks into the room before any of the prisoners go in? Well, then they can guarantee success for the prisoners by swapping the contents of just two boxes. That's because there can be at most one loop that is longer than 50, and you can break it in half just by swapping the contents of two boxes. Now I have two separate loops that are each shorter than 50.
But what if there was a malicious guard who figured out that the prisoners were going to use this loop strategy? Well, then they could put the numbers in boxes to ensure they formed a loop longer than 50. In this case, are the prisoners doomed? Surprisingly, no. They can counter by arbitrarily renumbering the boxes. They could, for example, add five to each box number. The loops are set both by the locations of the slips and by the box numbers. Renumbering the boxes is essentially the same as redistributing the slips, so the problem is back to a random arrangement of loops, meaning the prisoners are back to their 31 percent chance of survival.
Now, what happens if you increase the number of prisoners? Fun fact: nobody knows if, as you have more and more prisoners, it's going towards a limit, or it will eventually go down to zero, or what. That is, my friend Matt Parker and I think what he meant to say is, we know exactly what happens as you increase the number of prisoners. With a thousand prisoners, each allowed to check 500 boxes, you might expect their chance of success to drop dramatically. But you can calculate it like we did before, and it comes out to 30.74 percent, only half a percentage point lower than 400 prisoners. For 1 million prisoners, the probability of success is 30.685 percent, which is only a little higher than 41 billion. Of course, their bigger problem would be the time it takes to open all the boxes. So your probability of winning this game does indeed approach a limit.
So what is that limit? The formula we've been using is 1 minus the chance of failure, which is the series 1 over 51 plus 1 over 52 and so on, up to 1 over 100. We can depict this series as the sum of areas of rectangles, and there is a curve that follows the heights of these blocks. That curve is 1 over x. The area under that curve from 50 to 100 approximates the area of all the rectangles, and as the number of prisoners goes to infinity, it becomes a better and better approximation. So to find the probability of failure, we can just take the integral of 1 over x from n to 2n, and we find that it's equal to the natural logarithm of 2. This gives a probability of success of one minus the natural log of two, which is about 30.7 percent.
The bottom line is that no matter how many prisoners you have, you'll always end up with above a 30 percent chance of escaping using this strategy, and that feels really wrong. At first it seemed essentially impossible for all 100 prisoners to find their numbers, but now we're seeing that you could have a hundred, a million, or any arbitrarily large number of prisoners with at least a 30 percent chance that they all find their numbers. The beauty of the loop strategy is linking everyone's outcomes together. Instead of each prisoner walking in with their own 50-50 shot, following the same loops means that they have the exact same chance of finding their number as everyone else in their loop. Once the boxes and slips are arranged, that chance is set at either one hundred percent or zero percent. With this strategy, you can't ever get close to winning with only a few people missing their numbers. You can only fail hard or succeed completely.
Now, if you like solving puzzles, even outside of life-threatening prison situations, you'll love Brilliant, the sponsor of this video. Brilliant is a website and app that builds problem-solving skills and guides you through engaging, interactive lessons in math and science. They have great courses on tons of topics, from statistics to astrophysics to logic. Now, if you liked this riddle and you want more just like it, I'd recommend their Perplexing Probability course, featuring other seemingly impossible odds and even another mathematically inclined prison warden. They've also got a Joy of Problem Solving course to take you through some of their most delightful math puzzles. Every lesson builds on what you learned previously to give you an in-depth understanding. With any course you take, your knowledge is constantly developed and tested through interactive experiments and quizzes, and if you get stuck, there's always a helpful hint. Head over to brilliant.org/veritasium to check out all these courses and test your instincts. After learning about the 100 prisoners riddle, if you click through right now, Brilliant is offering 20 percent off an annual premium subscription to the first 200 people to sign up. So I want to thank Brilliant for supporting Veritasium, and I want to thank you for watching.
