252008/17: Finding the odd ball with a scale or a balance

25.1Problem

We are given \(n\) balls, which are apparently equal and not too heavy. All of them are of equal weight, except for one (we do not know if the one is heavier or lighter). Our aim is to separate out this different ball in the shortest possible time. There are two instruments at our disposal: one is a normal table scale with a display; the other is a double-arm scale (both are ideal, i.e. infinitely precise and can hold infinite weight).

a) On the table scale, we can measure the balls one by one. What is the expected time of finding the different ball, if one measurement takes \(T\) time? What is this time in the limit of \(n\to\infty\)?

b) Determine the minimal number of measurement (denoted by \(N(n)\)) by which using the double-arm scale we can surely find the different ball!

c) If one measurement on the double-arm scale takes on average \(C\cdot T\), at which value of \(C\) should one use the double-arm scale at fixed \(n\)?

d) Let \(n\) be 40, and \(C = 51/11\)! Show that in this case the double-arm scale is favourable, and describe the measurement procedure in all details from beginning to end!

(translated by Dezső Varga)

(Gábor Homa)

25.2Explanation

We have \(n\) balls that look the same. All of them have the same weight except one, which we call the odd ball; it is heavier or lighter by an unknown amount. The Hungarian original states explicitly that the common weight of the other balls is also unknown. We only have to find the odd ball; we do not have to tell whether it is heavier or lighter. The odd ball is equally likely to be any of the \(n\) balls, and heavier or lighter with equal probability.

In part a) the table scale shows the weight of one ball per measurement. We have to find \(e_T(n)\), the expected number of measurements, and the expected time \(e_T(n)\,T\). In part b) the two-pan balance compares two groups of balls and shows which side is heavier, or that they balance. \(N(n)\) is the smallest number of weighings with which some procedure finds the odd ball in every case. In part c) we compare the expected times of the two instruments, and in part d) we write down a complete procedure for \(40\) balls.

Two points of the statement need an interpretation. First, we need \(n \ge 3\). With two balls neither instrument can help: the scale shows two different weights, the balance tips, and nothing tells which ball has the common weight. Second, we put the same number of balls on the two pans of the balance. If one pan holds more balls and the odd ball differs only slightly, the pan with more balls goes down wherever the odd ball is. The problem gives no bound on the difference, so such a weighing gives no information in the worst case.

25.3Theory

Expected value. If a quantity takes the value \(x_k\) with probability \(p_k\), its expected value is \(\sum_k p_k x_k\). We call each of the \(2n\) equally likely possibilities, “ball \(i\) is heavier” and “ball \(i\) is lighter”, a case.

Procedures as trees. A procedure may choose each weighing according to the results of the previous ones. We draw it as a tree. The first weighing is the root; each weighing has three branches, one for each result (left pan down, right pan down, balance), and each branch leads to the next weighing or to an end point, where the procedure names a ball. A procedure with at most \(w\) weighings has at most \(3^w\) end points, because each of the \(w\) results can take three values. Each case determines the result of every weighing, so each case leads to exactly one end point. The procedure works if all cases that lead to the same end point have the same odd ball. If more cases than end points must be told apart, two of them end at the same point; this is the pigeonhole principle.

Suspects. During a procedure each ball is in one of four states. A ball is known to be good if no case that agrees with the results so far makes it the odd ball. A two-sided suspect may still be heavier or lighter. A one-sided suspect may be the odd ball in one way only: a ball that was on the pan that went down can only be heavier, and a ball that was on the pan that went up can only be lighter. Two-sided suspects count as two cases, one-sided suspects as one.

Rounding up. \(\lceil x\rceil\) denotes the smallest integer not less than \(x\), and \(\log_3 x\) is the base-3 logarithm, the number \(y\) with \(3^y = x\).

25.4Solutions

25.4.1Counting argument and construction

(a) The table scale. We measure the balls in the order \(1, 2, 3, \dots\). We do not know the common weight, so we learn it only when two balls agree. Suppose the odd ball is ball \(k\).

  • If \(k = 1\) or \(k = 2\), the first two measurements disagree, and a third ball decides which of them has the common weight: \(3\) measurements.

  • If \(3 \le k \le n-1\), the first \(k-1\) balls agree and ball \(k\) differs: \(k\) measurements.

  • If \(k = n\), the first \(n-1\) balls agree, and the last ball must be the odd one without being measured: \(n-1\) measurements.

Averaging over the \(n\) equally likely positions, and using \(3 + 4 + \dots + (n-1) = n(n-1)/2 - 3\),

\[ e_T(n) = \frac{1}{n}\Bigl[\,6 + \frac{n(n-1)}{2} - 3 + (n-1)\Bigr] = \frac{1}{n}\Bigl[\frac{n(n+1)}{2} + 2\Bigr], \]

so the expected time is

\[ e_T(n)\,T = \Bigl(\frac{n+1}{2} + \frac{2}{n}\Bigr)\,T . \tag{174}\]

For example \(e_T(3) = 8/3\) and \(e_T(40) = 411/20 = 20.55\). No other order is better: the only choice in a procedure that measures one ball at a time is which unmeasured ball comes next, measuring a ball twice gives nothing new, and the balls are interchangeable. For \(n \to \infty\) the expected time grows without bound, proportionally to \(n\): \(e_T(n)\,T/n \to T/2\), so on average we measure half of the balls. If the common weight were known, every ball could be checked on its own, and the average would be \((n+1)/2 - 1/n\); not knowing it costs \(3/n\) measurements on average.

(b) Lower bound for the balance. Let a procedure find the odd ball among \(n\) balls with at most \(w\) weighings in every case, and let its first weighing put \(k\) balls on each pan (\(k \ge 1\)).

If the first weighing tips, say to the left, the \(2k\) balls on the pans become one-sided suspects (the left ones can only be heavier, the right ones only lighter) and all other balls are known to be good. These \(2k\) cases have \(2k\) different odd balls, so the remaining \(w-1\) weighings must send them to \(2k\) different end points: \(2k \le 3^{w-1}\). Since \(3^{w-1}\) is odd, \(2k \le 3^{w-1} - 1\).

If the first weighing balances, the \(m = n - 2k\) balls left off the pans are two-sided suspects, \(2m\) cases. Two cases may share an end point only if they are “ball \(i\) is heavier” and “ball \(i\) is lighter” for the same ball \(i\). These two cases give the same results only if ball \(i\) is never on a pan along their path, and then every weighing along that path balances. There is only one end point at which all results are “balance”, so at most one ball can be found this way, and all other cases need end points of their own: \(2m - 1 \le 3^{w-1}\), that is \(m \le (3^{w-1}+1)/2\). Adding the two bounds,

\[ n = 2k + m \le 3^{w-1} - 1 + \frac{3^{w-1} + 1}{2} = \frac{3^w - 1}{2} . \tag{175}\]

(b) A procedure that reaches the bound. We show that \(w\) weighings suffice whenever \(3 \le n \le (3^w-1)/2\), in three steps.

Step 1: one-sided suspects. Suppose there are \(s \le 3^r\) one-sided suspects and at least one ball known to be good. We divide the suspects into groups \(A\), \(B\), \(C\) of at most \(3^{r-1}\) each. The heavier-type suspects of \(A\) and the lighter-type suspects of \(B\) go on the left pan; the lighter-type suspects of \(A\) and the heavier-type suspects of \(B\) go on the right pan; \(C\) stays off. If the left pan goes down, the odd ball is in \(A\); if the right pan goes down, in \(B\); if they balance, in \(C\). In each outcome we again have one-sided suspects, at most \(3^{r-1}\) of them, so by repeating the step \(r\) weighings leave one suspect. The pans must hold the same number of balls, and one good ball always suffices to make up the difference. To see this, let \(a = \min(3^{r-1}, \lfloor s/2\rfloor)\), where \(\lfloor x\rfloor\) is \(x\) rounded down; then \(s - 2a \le 3^{r-1}\). We form pairs of suspects of the same type, and from each of \(a\) pairs we put one ball in \(A\) and the other in \(B\). The two balls of a pair then lie on opposite pans, so the pans are equally loaded, and the rest goes to \(C\). There are enough pairs unless the numbers of heavier-type and lighter-type suspects are both odd and \(a = s/2\); then we use all \(a - 1\) pairs, add the unpaired heavier-type suspect to \(A\) and leave the unpaired lighter-type one in \(C\), and the pans differ by one ball.

Step 2: two-sided suspects with a good ball available. Suppose there are \(m \le (3^r+1)/2\) two-sided suspects and some balls known to be good. We put \(j = \max\bigl(1,\ m - (3^{r-1}+1)/2\bigr)\) suspects on the pans, divided as evenly as possible, and fill up with one good ball if \(j\) is odd. One checks that \(j \le 3^{r-1}\). If a pan goes down, the \(j\) suspects become one-sided, and we finish with Step 1 in \(r-1\) weighings. If the pans balance, at most \((3^{r-1}+1)/2\) two-sided suspects remain, and we repeat Step 2. With \(r = 0\) weighings left a single suspect remains, which is the odd ball, although we do not learn whether it is heavier or lighter.

Step 3: the first weighing. No ball is known to be good yet. We put \(k\) balls on each pan, with \(k\) the smallest integer that is at least \(1\) and at least \(\bigl(n - (3^{w-1}+1)/2\bigr)/2\). From \(n \le (3^w-1)/2\) it follows that \(2k \le 3^{w-1} - 1\), so if a pan goes down, Step 1 applies to the \(2k \le 3^{w-1}\) one-sided suspects. The \(n - 2k \ge 1\) balls left off are then known to be good and provide the good ball needed in Step 1. If the pans balance, Step 2 applies to the \(n - 2k \le (3^{w-1}+1)/2\) balls left off.

Together with (175), the minimal number of weighings is

\[ N(n) = \min\Bigl\{w : n \le \frac{3^w - 1}{2}\Bigr\} = \bigl\lceil \log_3 (2n+1) \bigr\rceil , \qquad n \ge 3 . \tag{176}\]

So \(N(n) = 2\) for \(n = 3, 4\); \(N(n) = 3\) for \(5 \le n \le 13\); \(N(n) = 4\) for \(14 \le n \le 40\); and \(N(n) = 5\) for \(41 \le n \le 121\). If we also had to tell whether the odd ball is heavier or lighter, the end point where all weighings balance could not hold a ball either, and the largest number of balls for \(w\) weighings would be \((3^w - 3)/2\), for example \(12\) balls in \(3\) weighings. This is the classic twelve-coin problem, solved in general by Dyson [66]. The problem asks only for the ball, which allows one more.

(c) When the balance is faster. The procedure of part b) never needs more than \(N(n)\) weighings, that is the time \(C\,N(n)\,T\). It is faster than the table scale on average whenever \(C\,N(n) < e_T(n)\), that is for

\[ C < C^*(n) = \frac{e_T(n)}{N(n)} = \frac{(n+1)/2 + 2/n}{\bigl\lceil \log_3(2n+1)\bigr\rceil} . \tag{177}\]

Below this value the balance takes less time even in its worst case than the table scale on average. In some cases a procedure ends earlier than after \(N(n)\) weighings, so a comparison of averages needs \(E_B(n)\), the least average number of weighings over all procedures, and the exact condition is \(C < e_T(n)/E_B(n)\). \(E_B(n)\) has no simple formula; we computed it by exhaustive search (Section 25.4.2). For \(n = 5\), for example, \(N = 3\) but \(E_B = 11/5 = 2.2\). Both critical values are plotted in Figure 48. \(C^*(n)\) rises roughly linearly within each range of constant \(N(n)\) and drops where \(N(n)\) increases, at \(n = 14\), \(41\), \(122, \dots\). For large \(n\) it grows like \(n/(2\log_3 2n)\): \(C^*(100) = 10.1\) and \(C^*(1000) = 71.5\). The table scale needs on average a number of measurements proportional to \(n\), the balance only one proportional to \(\log n\), so for any fixed \(C\) the balance is faster once \(n\) is large enough.

Figure 48
Figure 48: The critical value of \(C\) below which the balance finds the odd ball faster than the table scale on average. Filled dots: \(C^*(n) = e_T(n)/N(n)\), comparing with the largest number of weighings. Open circles: \(e_T(n)/E_B(n)\), comparing with the least average number of weighings, from an exhaustive search for \(n \le 41\). The dashed line is \(C = 51/11\) of part d).

(d) Forty balls. For \(n = 40\) the table scale needs on average \(e_T(40)\,T = 20.55\,T\) by (174). Since \(40 = (3^4 - 1)/2\), the balance needs \(N(40) = 4\) weighings by (176), the time \(4 \cdot 51/11\,T = 204/11\,T = 18.55\,T\). The balance saves \(2.00\,T\) on average, and it is faster even in its worst case. The critical value (177) is \(C^*(40) = 411/80 = 5.14\), above \(51/11 = 4.64\). By exhaustive search, \(E_B(40) = 4\): no procedure needs fewer than four weighings on average, so the two criteria agree. Forty is the largest number of balls that four weighings can handle. With \(41\) balls a fifth weighing is needed and \(C^*(41) = 4.21\), so in the worst-case comparison (177) the value \(51/11\) favors the balance for \(36 \le n \le 40\) and for all \(n \ge 46\), but not for \(41 \le n \le 45\).

Since \(40\) meets the bound (175) with equality, every inequality in the argument of part b) must hold with equality, and the procedure has no freedom in its numbers. The first weighing has \(13\) balls on each pan. If it tips, \(26\) one-sided cases remain, split \(9\), \(9\), \(8\) by the second weighing, and then \(3\), \(3\), \(3\) (or \(3\), \(2\), \(3\)) by the third. If it balances, \(14\) two-sided suspects remain; the second weighing puts \(9\) of them on the pans, the third \(3\) of the remaining \(5\), and the fourth \(1\) of the last \(2\). The complete procedure is listed in Table 18, with the balls numbered \(1\) to \(40\). “\(>\)” means that the left pan goes down, “\(<\)” that the right pan goes down, and “\(=\)” that the pans balance. In the column of suspects, H marks balls that can only be heavier and L balls that can only be lighter. Every path uses exactly four weighings. We checked the table by computer for all \(80\) cases.

Table 18: The complete procedure for \(40\) balls with four weighings. Balls 1–9 in steps 2 and 3, and balls 27 and 28 in steps 5 and 8, are known to be good and only fill up the pans. In the end point of step 3 where ball 40 is named, ball 40 was never weighed, and we do not learn whether it is heavier or lighter.
stepsuspectsleft panright panresult
1all 401–1314–26\(=\): step 2. \(>\): step 5. \(<\): step 5 with the numbers of balls 1–13 and 14–26 exchanged.
227–4027–351–9\(=\): step 3. \(>\): one of 27–35 is heavier, step 4. \(<\): one of 27–35 is lighter, step 4.
336–4036, 37, 381, 2, 3\(=\): weigh 39 against 1; if the pans differ, the odd ball is 39, otherwise 40. \(>\) (\(<\)): one of 36–38 is heavier (lighter); weigh 36 against 37: the odd ball is the heavier (lighter) one, or 38 if they balance.
427–35, all H or all L27, 28, 2930, 31, 32The odd ball is in the triple on the pan that goes down if it is heavier (up if it is lighter), or in 33, 34, 35 if the pans balance. Weigh the first ball of the triple against the second, as in step 3.
51–13 H, 14–26 L1–5, 14–186–9, 19–22, 27, 28\(>\): step 6. \(<\): step 7. \(=\): step 8.
61–5 H, 19–22 L1, 2, 193, 4, 20\(>\): weigh 1 against 2; the heavier one, or 20 if they balance. \(<\): weigh 3 against 4; the heavier one, or 19. \(=\): weigh 21 against 22; the lighter one, or 5.
76–9 H, 14–18 L6, 7, 148, 9, 15\(>\): weigh 6 against 7; the heavier one, or 15. \(<\): weigh 8 against 9; the heavier one, or 14. \(=\): weigh 16 against 17; the lighter one, or 18.
810–13 H, 23–26 L10, 11, 2312, 24, 27\(>\): weigh 10 against 11; the heavier one, or 24. \(<\): weigh 12 against 27; 12 if the pans differ, 23 if they balance. \(=\): weigh 25 against 26; the lighter one, or 13.

25.4.2Exhaustive computer search

The lower bound of part b) uses an argument about all possible procedures, which is the kind of argument that is easy to get wrong. A computer can check it directly for small \(n\) by trying every procedure. The state of knowledge during a balance procedure is fully described by four numbers: the numbers \(u\) of two-sided suspects, \(h\) of suspects that can only be heavier, \(l\) of suspects that can only be lighter, and \(g\) of balls known to be good. The balls of one kind are interchangeable, so a weighing is described by how many balls of each kind go on each pan, with equal totals on the two pans. For every such weighing the program computes the three new states, and it finds the least number of weighings by recursion: a state with at most one suspect needs none, and any other state needs one more than the best weighing’s worst outcome. The same recursion with the sum over outcomes in place of the maximum gives the least total number of weighings over all cases, and dividing by \(2n\) gives \(E_B(n)\). Weighings in which some outcome does not reduce the number of cases are skipped, because they cannot be part of an optimal procedure.

For every \(n\) from \(3\) to \(41\) the search returns exactly \(N(n)\) of (176). With the stricter goal of also telling heavier from lighter, it returns \(3\) balls for two weighings and \(12\) for three, Dyson’s \((3^w-3)/2\). The least averages \(E_B(n)\), used in Figure 48, are \(5/3\), \(2\), \(11/5\), \(7/3\) for \(n = 3, 4, 5, 6\), rise to \(3\) at \(n = 13\) and to \(4\) at \(n = 40\). The program also builds the decision tree of the three-step procedure of part b) for every \(n\) from \(3\) to \(364 = (3^6-1)/2\), runs all \(2n\) cases through it, and confirms that each ends at the right ball within \(N(n)\) weighings and that one good ball always suffices to fill up the pans. For the table scale, a similar search over all orders of measurement for \(n \le 9\) reproduces (174).

References

  • [66] F. J. Dyson. The problem of the pennies. The Mathematical Gazette 30, 231–234, 1946.