One Hat Comes Home
N prisoners sit in a circle wearing name-tagged hats, and every night the jailer rotates every hat one cell clockwise. Release requires at least one prisoner to be wearing his own hat on each of N days. Two failed attempts lead to the waiting number of a hat, the winning plan is built rather than guessed, and the parity of N decides everything.
The problem
There are prisoners in cells arranged in a circle, and hats, each carrying the name-tag of one prisoner. Before the game starts the prisoners may put the hats on each other’s heads in any order they like, and that is what they wear on day 1. Every night the jailer moves each hat one cell clockwise, so the hat you wear today is worn by your clockwise neighbour tomorrow. On day the jailer looks back over the record. If at least one prisoner wore his own hat on each of the days , everyone goes free. One day with nobody correct and nobody goes free.
The prisoners make a single decision and then watch. Everything after the opening arrangement belongs to the jailer, so the question is whether some arrangement can survive all of its own rotations. Number the cells clockwise, and give each prisoner and his hat the number of his cell.
Two attempts that fail
The obvious opening is to give every prisoner his own hat. Day 1 could not go better, with all prisoners correct at once. On day 2 every hat has moved one cell and every prisoner is wearing his anticlockwise neighbour’s hat, so nobody is correct and the game is over. Spending all the luck on one day is the worst thing the prisoners can do.
A scrambled opening does better. Take and put the hats in cells 0 to 4.
This plan covers days 1, 3 and 4 and loses on days 2 and 5. The pattern of the failure is more informative than the failure itself. Days 3 and 4 each have two correct hats, and the two wasted hats are exactly the two missing days. Success is bunching up where it is not needed.
So the prisoners need to spread the luck out, one hat per day, for days. The next step is to find out what controls when a hat arrives.
Following one hat
A prisoner is a poor thing to follow, because the hat on his head changes every night. A hat is a better thing to follow, because it never changes owner and it moves at a steady one cell a night. So ask of each hat the only question that matters: how many nights until it reaches its owner?
Take the hat numbered 4 in the failing plan. It starts in cell 1 and has to reach cell 4, three cells clockwise, so it arrives on day 4. The hat numbered 0 starts in cell 2, with its owner behind it, so it goes the long way round through cells 3, 4, 0 and also takes three nights. Distances on a circle only mean anything modulo , and everything below is written that way.
Waiting numbers
Write the plan as the list , where is the number on the hat placed in cell on day 1. The list is a rearrangement of , since every hat is used exactly once.
The hat in cell is in cell after nights, so it is on its owner’s head when . Exactly one value of between 0 and satisfies this, and it is the hat’s waiting number:
The failing plan has waiting numbers . Two observations turn those five numbers into the whole story.
The first is that every waiting number drops by one modulo overnight, since every hat ends the night one cell closer to its owner, and the hat that was home has just been taken off and must travel the whole circle again. The collection of waiting numbers is therefore the same collection every day, shunted along by one.
The second observation is that on day every hat has moved cells, so the hat that started in cell is home on day exactly when . Day 1 is covered by the hats with waiting number 0, day 2 by those with waiting number 1, and so on.
Now the earlier table explains itself. The waiting numbers contain no 1 and no 4, which is why days 2 and 5 went bare, and they contain 2 and 3 twice each, which is why days 3 and 4 had two correct hats. The repeats and the gaps are the same defect counted twice.
Proposition. A plan frees the prisoners exactly when its waiting numbers are all different, that is, when are in some order.
There are days to cover and hats to cover them with. Day needs a hat with waiting number , so the days 1 to between them need every value from 0 to . That is different values wanted from hats, leaving no room for a repeat: two hats sharing a waiting number means some value is missing, and the day that value would have covered passes with nobody correct.
Building a plan
The proposition is worth more than a test, because it says what to design. A plan and its list of waiting numbers determine each other:
So the prisoners may choose the waiting numbers first, all of them different, and read the hats off afterwards. The only thing to check is that the hats so obtained are different hats.
The simplest list of different waiting numbers is : the hat in cell 0 is already home, the hat in cell 1 waits one night, the hat in cell 2 waits two, and so on round the circle. Reading off the hats gives
The doubling plan is what that choice becomes. It was not guessed: the proposition asked for different waiting numbers, and the hats followed from the simplest such list.
Whether the plan is legal is now the only question, and it is exactly here that the parity of enters. If cells and received the same hat then , and an odd shares no factor with 2, so and the cells are the same. No two cells collide, every hat is used once, and the plan frees the prisoners.
When is even the cancellation is not available, and the plan collapses in the obvious way: cells 0 and both ask for hat 0, since . The simplest choice of waiting numbers is unusable on an even circle.
For the plan reads , the last two having come back round the circle.
Handing the hats out needs no arithmetic at all. Walk clockwise from cell 0 and give out the hats in that order, starting again at the beginning of the list when you run off the end. Because is odd, the second lap picks up exactly the names the first lap skipped. The hat in cell comes home on day and lands on the prisoner numbered , so the correct prisoner on day is the one numbered modulo . The luck advances two cells every morning and goes twice round the circle over the days.
Other choices of waiting numbers work as well. Taking gives the plan , which is legal when shares no factor with and has all its waiting numbers different when shares no factor with . Tripling therefore works whenever is odd and not a multiple of 3. Shifting a winning plan round the circle also wins, since replacing by adds to every waiting number and leaves them all different.
Why an even N is hopeless
Doubling failed on an even circle. That leaves the harder question of whether some cleverer list of waiting numbers might survive, and the answer is that none can, for a reason that has nothing to do with which list is tried.
Add up all waiting numbers. Since ,
because are the numbers 0 to in some order and so have the same sum as the cell numbers. Whatever the prisoners choose, the total waiting is a multiple of . Read physically, the hats between them travel a whole number of laps.
A winning plan, by the proposition, has waiting numbers , which add up to . So a winning plan needs . For odd this holds, since is then a whole number and the total is times it. For even write , and then
which is not 0, since is not a multiple of . An even circle asks the hats to travel half a lap more than a whole number of laps, and no arrangement of hats can supply it.
The failed cancellation and the missing half lap are the same obstruction seen twice. An odd circle lets a 2 be divided out, which is what makes the simplest list of waiting numbers legal; an even circle refuses, and the half lap is what the refusal leaves behind.
The best an even group can do
Four prisoners can still cover three of their four days.
Which value gets repeated is forced. To cover days 1 to the waiting numbers must include each of , with one value repeated to make up the count of hats, and the total must still be a multiple of :
Writing again, the first term is , so , and since lies between 0 and this pins it to . Four prisoners must repeat 1, six must repeat 2, eight must repeat 3.
A plan that achieves it is easy to write down. Give the first half of the circle, cells 0 to , the hats , and give the second half the remaining hats in the order . For eight prisoners that reads , whose waiting numbers are : every day but the last is covered.
How many plans work
The number of arrangements that free the prisoners, counted over all openings, runs
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
|---|---|---|---|---|---|---|---|---|---|
| winning plans | 1 | 0 | 3 | 0 | 15 | 0 | 133 | 0 | 2025 |
Every count is divisible by , as it has to be, since shifting a winning plan round the circle gives another winning plan. The values for odd are known: they are sequence A006717 in the OEIS, where they count what algebra calls the complete mappings, or equivalently the orthomorphisms, of a cyclic group of odd order, and equally the ways of placing non-attacking semi-queens on an board wrapped into a torus. In that language a winning plan is a rearrangement of the residues modulo for which the differences are again all the residues modulo , which is the proposition written in one line.
The puzzle also circulates as the name tag problem, where guests seated round a table keep passing name tags to their neighbour. It has been written up in that form, with the same split by parity and the same multiplication plans, in Christian Carley’s undergraduate thesis at Boise State, Spring 2019. Both sources retrieved 19 September 2026.
A last thought
The puzzle looks as though it should reward cleverness about information, and it rewards none, because the prisoners never learn anything and never act again after the first morning. What they are choosing is a list of waiting times, and the jailer’s rotation only reads that list out one entry at a time.
The notation shows how little the rotation itself matters. Suppose the jailer moved the hats cells a night rather than one. On day every hat would have moved cells, so that day would need a hat with , and as runs from 1 to those targets run through every residue exactly when and share no factor. When they do share a factor, only some residues are ever asked for, fewer days need covering and an even circle stops being hopeless. Four prisoners facing a two-cell rotation go free with the arrangement , the very arrangement that fails them on the fourth day when the jailer moves one cell at a time.