Computation

Twenty questions when one answer may lie

Twenty yes-or-no questions find any number up to a million. Allow the person answering to lie once, and twenty are no longer enough — and the exact number needed is decided by a single conserved quantity, Berlekamp's weight, which every question splits between its two answers. The weight says 25; an exhaustive search of the game confirms that the bound is met for almost every range, missing it by one question only at 3, 5, 9, 51 and 93 below 300; and the questions can even be written down in advance, as a code.

Worth reading first: Sixteen spheres that fill a cube · The best a code can be.

Someone has chosen a number between one and a million, and a questioner may ask anything that can be answered yes or no. Twenty questions are enough, because each answer halves the remaining possibilities and 220=1,048,5762^{20} = 1{,}048{,}576 is just over a million. That is binary search, and it is optimal: nineteen questions have only 2192^{19} possible answer patterns, too few to tell a million numbers apart. The counting argument is worth keeping in view, because everything below is the same argument with one complication added. Without lies, each number must own one answer pattern, so MM numbers need MM patterns and 2q≥M2^q \ge M. With a lie, each number must own a whole cluster of patterns — every pattern the answerer could produce while it is the chosen number — and the clusters must not overlap.

Now suppose the person answering is allowed to lie — once, at most, and at a moment of their choosing. Stanisław Ulam asked in his autobiography how many questions are then needed, and Alfréd Rényi had asked the same thing about a noisy channel a decade before. The answer for a million is 25. The reason it is 25, and not 21 or 40, is a piece of bookkeeping due to Elwyn Berlekamp: a single number, computed from the state of the game, that each question splits exactly between its two possible answers, and that must therefore halve at every step if the questioner is to win.

This essay computes the game exactly. It plays it out, draws the counting argument behind the bound, searches the game tree for every range up to 300 to see where the bound is met, compares questions chosen adaptively with questions written down in advance — which turn out to be the same thing as an error-correcting code — and measures what each additional lie costs.

Sixteen numbers, seven questions

Sixteen numbers, seven questions and one lie. 7:16/0 w128 6:8/8 w64 5:4/8 w32 4:2/6 w16 3:0/8 w8 2:0/4 w4 1:0/2 w2 0:0/1 w1
Fig. 1 Sixteen numbers and seven questions against an answerer who may lie once and always picks the answer that leaves the questioner worse off. Each row shows candidates with no lie used, candidates with the lie used, and the weight, which equals 2 to the power q at every step.

With one lie allowed, a candidate number is not ruled out by a single answer against it. Berlekamp’s bookkeeping sorts the candidates into two kinds. A candidate consistent with every answer so far would mean nobody has lied yet; call there aa of these. A candidate inconsistent with exactly one answer would mean the lie has been used; call there bb of those. A candidate inconsistent with two answers is out. A question is a set of numbers — is the chosen number in this set? — and it acts on the two kinds differently: a first-kind candidate that the answer contradicts becomes a second-kind candidate, and a second-kind candidate that the answer contradicts is eliminated.

Now give each candidate a weight. With qq questions still to come, a first-kind candidate could yet be confirmed by q+1q + 1 different patterns of future answers — all truthful, or with the lie at any one of the qq places — and a second-kind candidate by exactly one, all truthful. So the state’s weight is a(q+1)+ba(q + 1) + b. Count the patterns behind each answer to the next question and the weight splits exactly: whatever set is asked about, the weight of the state after a yes plus the weight after a no equals the weight before.

The figure plays sixteen numbers against seven questions, with an answerer who always gives the answer leaving the larger weight. The weight starts at 16×8=128=2716 \times 8 = 128 = 2^7; after the first question, whichever way it is answered, it is 64; then 32, 16, 8, 4, 2, and 1 — a single candidate. The questioner had no slack at all. At every step the weight equalled 2q2^q exactly, and each question had to split it into two equal halves, which is possible here only because sixteen and seven are a very special pair.

Why the weight cannot exceed two to the q

Sixteen balls of answer strings fill all 128. 0000000 1110000 1001100 0111100 0101010 1011010 1100110 0010110 1101001 0011001 0100101 1010101 1000011 0110011 0001111 1111111
Fig. 2 The 128 possible strings of seven answers, sorted by the number from 1 to 16 that each points to when the questions are those of the Hamming code. Each column holds the truthful answers and the seven strings with one lie, and the 16 columns of 8 fill all 128.

The weight gives an immediate lower bound. At the end, with no questions left, the questioner wins only if at most one candidate remains, so the final weight is at most 1. Since every question splits the weight exactly between the two answers and the answerer may choose either, the larger half must be at most 2q−12^{q-1} whenever the weight is at most 2q2^q, and conversely an answerer can always keep the weight above half its value. So the questioner can only win if the starting weight, M(q+1)M(q + 1), is at most 2q2^q:

M(q+1)≤2q.M(q + 1) \le 2^q.

The figure shows the same count without a game. Seven questions have 27=1282^7 = 128 possible answer strings. For the questioner to identify the number, each number must own its own set of strings — the truthful string and the seven strings with one answer flipped — and those sets cannot overlap, since an overlapping string would be consistent with two numbers. Sixteen numbers need 16×8=12816 \times 8 = 128 strings, all of them. When the questions are those of the Hamming code, the sixteen sets of eight are exactly the sixteen balls of radius one around the code words, and they tile the cube of answer strings perfectly — the same picture in which sixteen spheres fill a cube.

For a million numbers the bound reads 1,000,000×(q+1)≤2q1{,}000{,}000 \times (q + 1) \le 2^q. At q=24q = 24 the right side is 16.8 million and the left 25 million; at q=25q = 25 the right side is 33.6 million and the left 26 million. So at least 25 questions are needed. The question is whether 25 suffice, and that needs the questioner, not just the counting.

Where the bound is met, and where it is not

Questions needed with one lie, against the volume bound. exceptions 3,5,9,51,93; q(1e6) Pelc 25; M=16:7 M=300:12
Fig. 3 The fewest questions that find a number from 1 to M with one possible lie, for M from 2 to 300, by exact search of the game, against the volume bound and the questions needed without lies. The search meets the bound except at 3, 5, 9, 51 and 93.

The game is finite and small enough to solve. A state is the pair (a,b)(a, b) with qq questions left; a question only matters through how many candidates of each kind it includes, xx of the aa and yy of the bb; a yes leads to (x, y+a−x)(x,\ y + a - x) and a no to (a−x, b−y+x)(a - x,\ b - y + x); and the questioner wins from a state if some choice of xx and yy wins from both. The search uses the weight to prune — no state of weight above 2q2^q can be won — and solves every range MM from 2 to 300 exactly.

The result is in the figure. For almost every MM, the fewest questions equal the volume bound: whenever the counting allows it, a questioner exists who achieves it. The exceptions below 300 are M=3,5,9,51M = 3, 5, 9, 51 and 9393, where one more question is needed. Three numbers with one lie need five questions, although the bound allows four: 3×5=15≤163 \times 5 = 15 \le 16, so the bound is not violated, but no first question can split the weight 15 into two parts of at most 8. A first question that includes xx of the three candidates leaves weight 3x+33x + 3 after a yes and 12−3x12 - 3x after a no — 3 and 12, 6 and 9, 9 and 6, 12 and 3 — and every one of those pairs has a part above 8. The weight is fine; it simply cannot be cut evenly enough, because candidates come in indivisible lumps.

That parity obstacle is the whole story, and Andrzej Pelc found its exact form in 1987. For even MM the answer is the smallest qq with M(q+1)≤2qM(q + 1) \le 2^q; for odd MM it is the smallest qq with M(q+1)+(q−1)≤2qM(q + 1) + (q - 1) \le 2^q. The search agrees with Pelc’s formula at every one of the 299 ranges, and the formula gives 25 for a million — five more than with no lies. The dotted staircase in the figure, the questions needed without lies, shows the cost of the single lie growing slowly, from two questions for small ranges to five for a million.

Questions written down in advance

Adaptive questions against questions fixed in advance. saved at 21,22,23,24,25,26,27,28,41,42,43,44,45,46,47,48,49,50,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92
Fig. 4 The fewest questions with one possible lie when each question may depend on the previous answers, and when all must be fixed in advance. Fixed questions handle at most the largest code of distance 3 — 16 at length 7, 20 at length 8, 40 at length 9 — and adapting saves one question in the shaded ranges, never more.

The questioner above adapts, choosing each question after hearing the last answer. Suppose instead the questions must all be written down before any answer is heard — as they must be if the answers come back by post, or through a channel with no way to reply. Then qq questions assign to each number a fixed string of qq answers, and the strings must differ in at least three places: if two numbers’ strings differed in only two, one lie could make each look like a string one step from the other, and no rule could tell them apart. So fixed questions are exactly a binary code of length qq with minimum distance 3, and qq questions can handle at most as many numbers as the largest such code has words.

The largest codes are known for small lengths: 16 words at length 7 (the Hamming code), 20 at length 8, 40 at length 9, 72 at length 10, 144 at length 11. The computation checks lengths up to 7 by exhaustive search. Comparing with the adaptive game, the figure finds that fixed questions are never better and are worse by exactly one question in the shaded ranges — 21 to 28 numbers, 41 to 50, 73 to 92 — and equal everywhere else. Twenty-one numbers need eight adaptive questions but nine fixed ones, because no code of length 8 with distance 3 has 21 words.

For a million numbers the two agree. A Hamming code of length 31 has 2262^{26} words; deleting six of its message positions leaves a code of length 25 with 220=1,048,5762^{20} = 1{,}048{,}576 words and distance still 3. So 25 questions written down in advance find any number up to a million despite one lie — adaptivity buys nothing there. The volume bound and the sphere-packing bound on codes are one inequality, and for large ranges the best codes nearly meet it.

What more lies cost

What each lie costs in questions. 1e6: 20 25 29 33; k=40: 0,6,11,15
Fig. 5 The extra questions lies cost beyond log⁡2M\log_2 M, for MM up to 2402^{40}, from the volume bound. A million numbers need 20, 25, 29 and 33 questions with no lies, one, two and three.

The volume bound extends to any number of lies. With ee lies allowed, each number owns the answer strings within ee flips of its truthful string, 1+q+(q2)+⋯+(qe)1 + q + \binom q2 + \cdots + \binom qe of them, and these must fit into 2q2^q. The figure plots the extra questions this forces beyond log⁡2M\log_2 M for ranges up to 2402^{40}. One lie costs about log⁡2log⁡2M\log_2 \log_2 M extra questions — the number of bits needed to say where the lie was — and each further lie costs roughly as much again. A million numbers need 20 questions with no lies, 25 with one, at least 29 with two and at least 33 with three.

For one lie the bound is attained, by Pelc’s theorem. For more lies it is a lower bound, and the question of when it is attained is harder: Joel Spencer showed in 1992 that for any fixed number of lies, adaptive search achieves the bound up to a small additive constant once the range is large enough, and the exact answers for two and three lies have been settled case by case in long analyses. The game also has a logical side that the figures do not draw: Daniele Mundici showed that what the questioner knows in Ulam’s game with ee lies is exactly a truth value in Łukasiewicz’s many-valued logic, the logic whose formulas draw the zigzags of piecewise linear functions.

Seven questions for sixteen numbers

Seven questions fixed in advance for sixteen numbers. 0110011010011001 0101101010100101 0101010101010101 0011110011000011 0011001100110011 0000111100001111 0000000011111111
Fig. 6 Seven questions written down in advance — question ii asks whether the number is shaded in row ii — that identify any number from 1 to 16 even if one answer is a lie. Each column is a word of the Hamming code, and the syndrome of the answers names the lie.

The fixed questions for sixteen numbers can be written out, and the figure does so. Question ii asks whether the number is among those shaded in row ii. Read down a column and the pattern of shaded cells is that number’s truthful answer string, a word of the Hamming code; any two columns differ in at least three rows. To decode, the questioner forms three parity checks of the seven answers, the syndrome that finds the error without reading the message: if all three are even, nobody lied; otherwise the three parities, read as a binary number from 1 to 7, name the question that was answered falsely. Flipping that answer gives a code word, and the code word gives the number.

The computation checks this for all sixteen numbers and every one of the eight possibilities — no lie, or a lie at any of the seven questions — and the decoding always recovers the right number. These seven questions are also the ones behind the sixteen balls of the volume figure. Nothing about them depends on the answers: a questioner could print them on a card, collect the seven answers by post, and still find the number through one deliberate lie. That is the sense in which the Hamming code is a perfect strategy for the liar game with sixteen numbers, and it is the same perfection that lets hat-wearers guess together with only one chance in eight of all being wrong.

What the search does not cover

The exhaustive search solves the game for ranges up to 300 with one lie, and Pelc’s theorem extends the answer to every range. The computation does not search larger ranges or more lies; the figure of the cost of lies draws the volume bound, which for two or more lies is a lower bound and not always the answer. The claim that adaptivity saves at most one question rests on the known table of the largest codes, verified here only up to length 7.

The model also fixes what a lie is. In Ulam’s game the answerer may lie at most once, anywhere, adversarially. Other models give each answer an independent chance of being false, which is a noisy channel and its capacity rather than a game; or allow lies only in one direction, which changes the counting; or let the questions be restricted to intervals, which is much closer to how binary search is actually used and changes the answers substantially. None of those is drawn.

And the game assumes a question is any subset of the numbers. A questioner who may only ask whether the number is less than some kk has far fewer questions available, and with one lie needs more of them than the volume bound requires; that interval version has its own literature and its own exceptions.

Still open: many lies, and a fraction of them

For a fixed number of lies and large ranges, Spencer’s theorem says the volume bound is essentially attained, but the exact answers are known only for up to three or four lies, each requiring its own analysis. A different and harder question allows the number of lies to grow in proportion to the number of questions — at most a fraction rr of all answers may be false. Then the question is the largest rate at which information can be extracted, and for adaptive questions the answer is known to differ from the answer for fixed questions, which is the open problem of the best codes in disguise: the adaptive rate is known exactly for small fractions and is zero from one third on, while the corresponding rate for codes written in advance is not known for any positive fraction at all.

There is also a question about the exceptions. Pelc’s formula says exactly when the volume bound fails for one lie — certain odd ranges — and the search finds them at 3, 5, 9, 51 and 93 below 300. For two lies the corresponding exceptions have been worked out in a long case analysis, and for larger numbers of lies there is no general description of when the bound fails, nor of how large the failures can be.

One conserved weight decides the game

With one possible lie, finding a number from 1 to MM needs the smallest qq with M(q+1)≤2qM(q + 1) \le 2^q, plus one more question for certain odd MM — 25 questions for a million, five more than with no lies. The bound comes from Berlekamp’s weight, a(q+1)+ba(q + 1) + b, which every question splits exactly between its two answers, and equivalently from the answer strings: each number needs its own ball of q+1q + 1 strings, and the balls must fit into 2q2^q. An exhaustive search of the game finds the bound met for every range up to 300 except 3, 5, 9, 51 and 93, as Pelc’s formula predicts. Questions fixed in advance are a code of distance 3, never better than adaptive ones and worse by at most one — and for sixteen numbers the seven questions of the Hamming code meet the bound exactly, with the lie located by a syndrome.

Shares its objects with

Essays that name at least two of the same things, and that neither author linked.

Named objects

A dashed tag is an object no other essay names yet.

Adaptive searchBinary searchExhaustive searchGame treeHamming codeSphere-packing bound