Blog: Expanded Universe

The cycle of prisoners puzzle

I learned about this cool math puzzle, from the classic prisoners-with-limited-communication genre,1 about five years ago. One reason it stuck with me is that, in the Discord server where we discussed it, we came up with two radically different solutions; at the time, I remarked "it's like we're solving different problems". It's fairly difficult (I think it took me a few hours), but very rewarding:

You are in a prison with some unknown (finite) number of other prisoners, each isolated in your own room. Every day, the prison warden chooses an unknown cycle containing all prisoners, including yourself (the cycle may or may not vary between days). Each prisoner then simultaneously sends one bit to the next prisoner in the cycle.

Before the first day, you may send a single strategy email that every other prisoner will read (so every prisoner other than yourself must be following the same strategy). Other than this email and the bits, no communication is allowed.

The warden will set you all free if you can determine the number of prisoners. Can you guarantee doing so in finite time?

Fine print

Sources

I didn't look very hard for sources, but the person who shared this with me cited the xkcd fora, a claim also made by lomont.org. Unfortunately the xkcd link there doesn't work for me right now. This puzzle has also been posted to Puzzling StackExchange and /r/mathriddles. Solutions equivalent to both ones I present below have been posted.

Spoiler padding

Solutions below the Sierpinski gasket!

                                #
                               # #
                              #   #
                             # # # #
                            #       #
                           # #     # #
                          #   #   #   #
                         # # # # # # # #
                        #               #
                       # #             # #
                      #   #           #   #
                     # # # #         # # # #
                    #       #       #       #
                   # #     # #     # #     # #
                  #   #   #   #   #   #   #   #
                 # # # # # # # # # # # # # # # #
                #                               #
               # #                             # #
              #   #                           #   #
             # # # #                         # # # #
            #       #                       #       #
           # #     # #                     # #     # #
          #   #   #   #                   #   #   #   #
         # # # # # # # #                 # # # # # # # #
        #               #               #               #
       # #             # #             # #             # #
      #   #           #   #           #   #           #   #
     # # # #         # # # #         # # # #         # # # #
    #       #       #       #       #       #       #       #
   # #     # #     # #     # #     # #     # #     # #     # #
  #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #
 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

Upper bounding the number of prisoners

Both solutions I know of start by determining an upper bound N on the number of prisoners (and making it common knowledge).

For each positive integer n, we will attempt to establish that there are at most 2n prisoners with the following procedure:

Solution 1 ("linear algebra")

This solution will use a subroutine we'll call "announcing", which is easier to explain on its own. If each prisoner can determine whether they satisfy some property P themselves. we can "announce" to make it common knowledge whether any prisoner satisfying P exists. This is similar to the core step of the upper bounding procedure. To announce whether anybody satisfies P:

For the main strategy, we'll maintain an ordered list of disjoint nonempty groups of prisoners, which together contain every prisoner (including yourself). At all times, every prisoner will know how many groups there are and which group they're in. During each phase of the strategy, we'll try to either split a group into two smaller groups, or gather enough information to uniquely determine every group size.

In the beginning, there will be two groups, the first containing yourself and the second containing everybody else.

The main phase of this strategy proceeds as follows:

Since all groups are nonempty, there can't be more groups than prisoners, so the main phase can't restart indefinitely. Suppose it terminates when we have a list of m groups. Then, for every proper nontrivial subset of group S, we know a distinct subset of groups S with equally many prisoners: specifically, the subset of groups that announced receiving 1 during the S loop in the main phase. (Since the cycle includes all prisoners, it must go from some prisoner in S to some prisoner not in S somewhere, so S and S can't be identical.)

This is a large system of 2m2 linear equations. We also know that the first group, the one with only you in it, has size 1. So we can attempt to solve this system for the size of every group, and from there, the total number of prisoners. The last thing we need to prove is that the solution is unique.

I'll present two proofs of this:

Proof by contradiction

Suppose our system of linear equations has more than one solution. Let A and B be different solutions. Without loss of generality, assume that some group is larger in solution A than in solution B. Let S be the subset of all such groups (that are larger in solution A than in solution B). S can't contain your group, so it's a proper nontrivial subset of groups, so we know a distinct subset of groups S that has the same number of prisoners as S in either solution.

Let D=SS, and D=SS. Then D and D also have the same number of prisoners in either solution, and both are nonempty. However, this is impossible! Because of how S was chosen, every group in D has more prisoners in solution A than in solution B, but no group in D does.

Constructive proof

This proof is longer and the core step is basically the same as in the proof by contradiction, but it might feel more satisfying.

Let the group sizes be x1,,xm, where we assume your group is group 1 so that x1=1.

We will write each individual variable xi as a linear combination of (some subset of) x1,,xm, and then, proceeding from j=m down to 2, eliminate every use of xj in our linear combinations. Initially, we can write down the trivial xi=xi for every i. At the end of this process, we'll have expressed every variable x1,,xm as a linear combination of x1=1 only, which means we'll have uniquely determined every group size.

Now, to eliminate xj for j>1, we first need to write down xj as a linear combination of (some subset of) x1,,xj1. To do this, let S be the set of i such that the currently written-down linear combination for xi includes xj with a positive coefficient; this is a proper nontrivial subset of {1,,m} because it includes j itself and excludes 1. Then, look at the equation the main phase generated that corresponds to S, which is some equation of the form

iSxi=iSxi

for SS. After canceling the terms that appear on both sides, we know that, if we replace every variable with its currently written-down linear combination, we'll get a linear equation in terms of only x1,,xj where the coefficient of xj is positive on the left side and nonpositive on the right. This means we can solve this equation for xj in terms of x1,,xj1, exactly as desired.

Once we have a linear combination for xj, we can plug it into every other linear combination we have written down to rewrite all of them in terms of x1,,xj1.

As previously described, by repeating this process until i=2, we'll have written every variable x1,,xm in terms of only x1=1, so we can determine all group sizes and the number of prisoners.

Solution 2 ("coin passing")

We're going to give each prisoner other than yourself an imaginary stack of N2 coins.2 Sending a 1 will represent passing a coin to the next prisoner in the cycle; receiving a 1 will represent receiving a coin. Prisoners will keep track of how many coins they have at all times.

At a high level, this strategy's goal is to get most of the coins passed to you. If you can guarantee you've received all but <N2 of the coins, then you can just divide the number of coins you have by N2 and round up to get the number of other prisoners (and then add 1 for yourself).

Every day there will be a "threshold" T, and every prisoner other than yourself will pass a coin to somebody else iff they started the day with more than T coins. You will never pass a coin to anybody else, only receive coins to count them.

We claim that the strategy above has the desired effect if you just loop T from 0 to N1 for a total of N5 loops (N6 days).

Intuitively, this works because whenever T hits a number of coins that nobody has, the coins will become more evenly distributed, until nobody has N or more coins.3 To prove this rigorously, consider the quantity "the sum of squares of number of coins held by each prisoner (excluding yourself)". We claim that this quantity only stays the same or decreases each day, and will decrease at least once every time T loops from 0 to N1:

At the beginning, the sum of squares is pN4 where p is the number of prisoners other than yourself, which is less than N5. The sum of squares can't decrease below 0, so after N5 loops, we can guarantee that no prisoner (other than yourself) has N or more coins. So, as described initially, you can count how many coins you've collected, divide by N2, and round up to determine the number of other prisoners.

  1. I don't actually know a comprehensive resources of puzzles of this type. There are a few collected at sigh.github.io (among other kinds of puzzles).

  2. Many constant factors can be optimized out of my choices of parameters. I went for the simplest mathematical expressions.

  3. The most coins the warden can prevent you from getting in steady state is 1+2++(N1), in a state where one prisoner has 1 coin, another has 2, and so on. To preserve this state, on a day with threshold T, the warden can arrange all prisoners with >T coins contiguously with the prisoner with T+1 coins at the start, followed by all prisoners with T coins contiguously with the prisoner with T coins at the start.