Essays/88 Hats
The Puzzle
The "88 Hats" puzzle was posed by Andrew Nikitin to the J Forum on 2007-06-05. The following is a slightly modified version.
- 88 people stand in a circle, each having a hat with a number from 0 to 87 written on it. Everyone can see the numbers on other people's hats but can not see his own number. They simultaneously write a number on a piece of paper and give it to the judge. If at least one of them wrote a number that is on his own hat then everyone wins, otherwise everyone loses. What strategy should they use to guarantee victory?
- (Numbers on the hats do not have to be all different. People can not exchange any information during the procedure but can agree on some strategy beforehand.)
The puzzle was solved by John Randall a day later.
Solution below.
A Winning Strategy
The strategy works for any number of hats m . Beforehand, the people number themselves from 0 to m-1 . Let h be the list of hat numbers, and n{s be the sum of the numbers that person n sees. Then n{w , the number that person n writes, is m|n-n{s .
m=: 13 ] h=: ?.@#~ m 10 3 3 6 11 11 4 0 10 2 5 6 2 ] s=: 1 +/\. h 63 70 70 67 62 62 69 73 63 71 68 67 71 ] w=: m | (i.m) - s 2 9 10 1 7 8 2 12 10 3 7 9 6 w ,: h 2 9 10 1 7 8 2 12 10 3 7 9 6 10 3 3 6 11 11 4 0 10 2 5 6 2 +/ w = h 1
Here's why it works. Let t=:m|+/h be the sum of all the hat numbers, modulo m . The following are equal:
t m|(t{s)+t-t{s NB. addition and subtraction m|(t{s)+m|t-t{s NB. modulo arithmetic m|(t{s)+t{w NB. definition of w
Moreover, since t{s is the sum of all hats excluding t{h , it must be that t=m|(t{s)+t{h . Therefore m|(t{s)+t{w and m|(t{s)+t{h are equal and so t{w and t{h are equal. That is, the written number t{w for person t matches his hat number t{h .
] t=: m|+/h 8 t{w 10 t{h 10
It is instructive to examine what the "winning strategy" does for 2 hats. The strategy calls for person n to write m|n-n{s . When m=:2 , this reduces to person 0 writing the hat number of person 1 and person 1 writing the negation of the hat number of person 0. The tabulation of all possible hat numberings and corresponding writings shows that in each scenario there is a written number that equals a hat number.
numbering writing 0 0 0 1 0 1 1 1 1 0 0 0 1 1 1 0
An Abstraction
Let G be an abelian group of size m with operation g and whose group members are items of u . Compute s=: 1 g/\.h{u ; n{s is the "sum" of hat numbers that person n sees. Then the written number n{w is u i. (n{u) g gi n{s where gi x is the inverse of x in the group. (In the previous section G is the group of integers under addition modulo m ; the group elements u are i.m .)
Why does it work? Let t=: u i. g/h{u be the index of the "sum" of all the hat numbers. The following are equal:
(t{u) (t{u) g (t{s) g gi (t{s) NB. group inverse and identity (t{s) g (t{u) g gi (t{s) NB. associative and commutative (t{s) g (t{w){u NB. definition of w
Moreover, since t{s is the sum of all hats excluding (t{h){u and the group is abelian, it follows that (t{u)=(t{s) g (t{h){u . Therefore (t{s) g (t{w){u and (t{s) g (t{h){u are equal, and so (t{w){u and (t{h){u are equal and consequently t{w and t{h are equal. That is, the written number t{w for person t matches his hat number t{h .
88 Hats
The original puzzle has m=:88 . Since
I. 88 = 5 p: i.1000 89 115 178 184 230 276
There are at least 7 different strategies, derived from addition modulo 88 and multiplication modulo each b of 89 115 178 184 230 276 on the numbers co-prime to b . Thus:
NB. winning strategy based on group table x for hat numbering y NB. default group table is + modulo #y win=: ((# | +/~@i.@#) $: ]) : (4 : 0) assert. y e. i.m=.#y assert. ($x) -: m,m assert. x -: |:x NB. must be abelian G=. ({.x) i. x NB. standardize group table g=. G {~ ;&.> NB. group operation gi=. {&G i."1 0: NB. inverse (i.m) g gi 1 g/\.y )
The left argument x of win is a group table. It is standardized by doing ({.x)i.x , whence the group elements are i.m and the identity element is 0.
mgrp=: | */~@I.@(1 = ] +. i.) NB. group table for * modulo y mgrp 7 1 2 3 4 5 6 2 4 6 1 3 5 3 6 2 5 1 4 4 1 5 2 6 3 5 3 1 6 4 2 6 5 4 3 2 1 mgrp 9 1 2 4 5 7 8 2 4 8 1 5 7 4 8 7 2 1 5 5 1 2 7 8 4 7 5 1 8 4 2 8 7 5 4 2 1
mgrp b computes the group table for the integers under multiplication modulo b . The group elements are the numbers co-prime to b .
m=: 88 h=: ?.@#~ m I. h = win h 19 I. h = (mgrp 89) win h 28 I. h = (mgrp 115) win h 15 I. h = (mgrp 178) win h 40 I. h = (mgrp 184) win h 71 I. h = (mgrp 230) win h 55 I. h = (mgrp 276) win h 38
Contributed by Andrew Nikitin, John Randall, and Roger Hui. Dyalog APL version available here.