The Tower and Glass Marbles Problem Richard T. Denman Southwestern University Georgetown, Texas [email protected] David Hailey St. Stephens Episcopal School Austin, Texas [email protected] Michael Rothenberg Bain & Company, Inc. San Francisco, California [email protected] Figure 1: Marble Testing 1 The Tower and Glass Marbles problem [1] is a good vehicle for studying algorithmic reasoning at various levels of mathematical sophistication. The authors have used this problem to motivate audiences from high school Advanced Algebra classes to college Discrete Mathematics and Analysis of Algorithms classes. It consistently generates a high degree of student engagement and lively discussion. A background story frames the problem. The Catseye Marble company regularly tests the strength of its marbles by dropping a marble from various levels of their n story office tower, to find the highest floor f from which a marble will not break. Sometimes, their shipping deadline is comfortably distant, they have time for many test drops, and can use a small number of marbles to reduce costs. At other times, a shipping deadline is imminent, so they must use more marbles to speed the process. Given these constraints, they pose the following question to their QA department: given a number m of glass marbles for testing, what is the smallest number d of experimental drops that guarantees the determination of f , and precisely how should those drops be performed? We abstract away the natural variation in this physical problem by making some simplifying assumptions. Namely, we suppose: 1) The floors are numbered from 1 up to n and each drop is from the vertical middle of the current floor, so that even a drop from the first floor might break a marble (in which case f would be 0). 2) Either f is the top floor of the tower or each of the marbles breaks at every level exceeding f . 3) Each marble is left undamaged by any number of drops from any level not exceeding f . A complete solution will consist of both the minimum integer d, and an algorithm for carrying out a sequence of no more than d drops which must guarantee the determination of f . A proof is required that, given values n 2 and m, no algorithm is guaranteed to determine f in fewer than d drops. The One-Marble Case If there is only one marble, the problem is easy to analyze and solve. The reader is encouraged to stop reading right here and formulate a complete solution in this case, to appreciate the requirements of such a solution. The Two-Marble Case If there are two marbles, the solution to the problem is more subtle. One might naturally first try binary search, but this only improves the answer d found for the one-marble case by a factor of two, since the result of the first drop (from the middle floor) only reduces the number of floors to be tested by one-half. In that approach, if the marble breaks on the first drop (the worst case), then we are back in the one-marble case, with n2 floors left to search. This process can be used to determine f , but the value obtained for d will not be a minimum. That is, there is an algorithm guaranteed to determine f in fewer steps. As before, the reader is encouraged to stop reading right here and formulate a complete solution in √ this case. Hint: 2 d n e is larger than the minimum value of d (where d..e is the ceiling function). The m-Marble Case The culmination is the general case. As before, for maximum enjoyment, the reader is encouraged to think about this independently before reading further. Naturally, a complete solution to the two-marble case is essential for understanding the general case. Solution of the One-Marble Case The solution to the one-marble case is a simple linear search, from the bottom up. In the worst case, d = n, as it may be necessary to test every floor. Solution and Proof for the Two-Marble Case The intuition behind the solution for two marbles is that the sum of the number of drops made with each marble should be made as small as possible. Note that after some 3 number q of successive tests (with the first marble) results in breakage, the remaining candidates will lie below that floor and above the highest previous non-breaking drop. These remaining candidates must be traversed with a bottom up linear search using the remaining marble. This suggests that the first marble should be dropped at intervals starting from the bottom, and q will be at most the number of intervals. If an assumption is made that each interval should have the same size p, then at most p−1 drops will be required with the second marble. Therefore, it will suffice to minimize q + (p − 1), √ subject to pq ≥ n. Simple calculus yields the solution p = q = d n e, which √ means that the first marble will be dropped at most d n e times, after which √ the second marble will be dropped at most d n e − 1 times, for a total worst √ case behavior of 2 d n e − 1 steps. However, allowing the intervals to vary in size leads to an optimal solution. The key idea is to reframe the question as follows: for a given value of d, what is the tallest building (largest number of floors n) for which the correct value f can be determined with no more than d drops? To address this question, we briefly return to the one-marble case. For a given value of d, the tallest building that can be tested has d floors, since we must start at the bottom and work our way up with a linear search. Therefore the first drop in the two-marble case should be made from floor d, so that if the marble breaks, we are left with one marble and d − 1 drops to test the d − 1 floors below. If the first drop does not break a marble, the next drop must be made from floor 2d − 1 = d + (d − 1), so that if the second drop does break a marble, we are left with one marble, d − 2 drops, and d − 2 floors to test. If the second drop does not break a marble, the next drop must be made from floor 3d − 3 = d + (d − 1) + (d − 2) by similar reasoning. Continuing in this fashion gives us an arithmetic sequence d + (d − 1) + (d − 2) + . . . + 3 + 2 + 1 = d X k=1 k= d(d + 1) 2 for the maximum number of floors that can be tested with two marbles. The sum has d terms since there can be at most d drops. 4 Returning then to the original question: given a building with n floors, the smallest number d of drops needed will be the smallest integer d such √ √ 8n+1−1 8n+1−1 ≥ n. Solving for d leads to d ≥ ; that is, d = d e. that d(d+1) 2 2 2 Not only have we found d, but we have proved its optimality. Algorithm for the Two-Marble Case This reasoning leads to an algorithm for computing f , the highest non-breaking floor (see Algorithm 1). In the algorithm, the variable hi keeps track of the lowest known breaking floor, while low keeps track of the highest known non-breaking floor. In the absence of a known breaking floor, hi will be numf loors + 1. Algorithm 1 always terminates and returns the correct value for f in no more than √ e iterations. This is faster than the previous solution by a d = d 8n+1−1 2 √ factor of approximately 2. Algorithm 1 : The two-marble case low = 0 hi = numFloors + 1 √ jump = d( 8 ∗ numFloors + 1 − 1)/2e while (low < hi − 1) f = low + jump if (f > numFloors) f = numFloors if (marble breaks on floor f ) hi = f jump = 1 else low = f if (jump > 1) jump = jump − 1 return low Solution and Proof for the m-Marble Case A good way to investigate an optimal solution to the m-marble case is to reframe the question as in 5 the two-marble case. That is, for a given number d of drops, what is the largest number of floors n that can be tested using m marbles and at most d drops? Call this quantity n(d, m). As in the two-marble case, we ask how to pick the floor from which to make the first drop. When that drop is made, there are two possible outcomes; the marble either breaks or it doesn’t. If it breaks, we will have used one drop, have m − 1 remaining marbles, and know that no higher floors need to be tested. Otherwise, we will have used one drop, will still have m marbles, and know that no lower floors need to be tested. So to test the largest possible number n(d, m) of floors, we need to choose the first drop in a way that maximizes the number of floors below that can be tested using at most d − 1 drops and m − 1 marbles, and also in such a way that maximizes the number of floors above that can be tested with at most d − 1 drops and m marbles. This leads directly to the following recursive definition of function n(d, m): ( n(d, m) = 0 if d = 0 or m = 0 . n(d − 1, m − 1) + n(d − 1, m) + 1 otherwise Returning then to the original question: given m marbles for testing a building with n floors, the smallest number d of drops needed will be the smallest integer d such that n(d, m) ≥ n. Algorithm for the m-Marble Case This reasoning leads to Algorithm 2. The quantity n(d, m) can either be calculated recursively on the fly, or in advance, up to the needed size, and stored in a table. As in the 2-marble algorithm, the variable hi keeps track of the lowest known breaking floor, while low keeps track of the highest known non-breaking floor. As before, until there is a known breaking floor, hi will be numF loors + 1. Algorithm 2 will terminate and return the correct value of f , in no more than d iterations, where d is the smallest number such that n(d, m) ≥ n. 6 Algorithm 2 : The m-marble case low = 0 hi = numFloors + 1 drops = 0 while (n(drops, numMarbles) < numFloors) drops = drops + 1 while (low < hi − 1) f = low + n(drops − 1, numMarbles − 1) + 1 if (f > numFloors) f = numFloors if (marble breaks on floor f ) hi = f numMarbles = numMarbles − 1 else low = f drops = drops − 1 return low Solving the Recurrence It is natural to seek a closed form for n(d, m), which might then be used to calculate d from n and m. This would give a more succinct answer to the question of finding d, and would possibly make the algorithm for locating f more efficient. For this purpose, we define a one-to-one correspondence between execution traces of Algorithm 2 and bit strings of length d, containing at most m ones. Ones in the string represent a breaking drop, while zeros in the string represent either a non-breaking drop or a non-drop following the breaking of the last marble. That is, if the mth marble is broken, we complete the string with a substring of zeros, none of which represents a drop. It is also possible to get a substring of zeros at the end of the string that do represent non-breaking drops, but only if fewer than m ones (representing broken marbles) have appeared previously in the string. There are at most m ones in the string since there are only m marbles. Each of these strings corresponds uniquely to a possible output value for low, i.e., a possible value for the highest non-breaking floor. This correspondence has nothing to do with the binary number that the string represents. For example, note that the 7 string consisting of m consecutive ones, followed by d − m zeros, corresponds to the case that there is no non-breaking floor; in this case, the return value for low will be 0. Note also that the total number of bit strings is exactly the number of subsets of size at most m contained in a set of d elements. So P d th row of Pascal’s n(d, m) + 1 = m i=0 i , the sum of the elements in the d triangle up to column m inclusive. If m > d, in which case there is no column m, then n(d, m) + 1 = 2d , the sum of the entire dth row. Unfortunately this information, though interesting, is of little use, since there is no known closed P d form for m i=0 i . For m = 2, we have d d d d(d + 1) , n(d, m) = + + −1= 0 1 2 2 so the formula for m marbles agrees with the formula for 2 marbles. Note that the number n(d, m)+1 of possible return values for f is directly related to the cumulative binomial distribution function b(d,m) of a Bernoulli 2d 1 process with p = 2 . That is, d b(d, m) = 2 m i d−i X d 1 1 i=0 i 2 2 = m X d i=0 i = n(d, m) + 1. This function b(d, m) satisfies the recurrence relation: ( b(d, m) = 1 if d = 0 or m = 0 , b(d − 1, m − 1) + b(d − 1, m) + 1 otherwise which is consistent with the nonhomogeneous recurrence relation for n(d, m). It is interesting to observe that this recurrence relation b(d, m) is identical to that for the binomial coefficients themselves. The only difference is the second base case, in which m = 0 replaces the more familiar m = d in Pascal’s triangle. This gives Table 1, which might be called “Bernoulli’s rectangle”. 8 m d\ 0 1 2 3 4 5 0 1 1 1 1 1 1 1 2 3 4 5 1 1 1 1 1 2 2 2 2 2 3 4 4 4 4 4 7 8 8 8 5 11 15 16 16 6 16 26 31 32 ... ... ... ... ... ... ... Table 1: Bernoulli’s Rectangle A more concise, although perhaps less intuitive, alternative to solving the recurrence for n(d, m) would have been to consider its homogeneous component b(d, m), and recognize it as the recurrence for counting subsets. Either way, the two approaches together provide a satisfying understanding of the values of b(d, m), and therefore also the values of n(d, m). However, there is no closed form formula for calculating b(d, m), so we must either calculate it recursively, or store the required values in a table. Conclusion The authors hope that the reader has enjoyed this intriguing little puzzle, and can find ways to use it to help motivate algorithmic thinking among students, friends, and colleagues. We believe that the best learning happens when one is discovering new ideas by working on little puzzles like this one. 9 References 1. D. Ginat, Efficiency of algorithms for programming beginners, ACM SIGCSE Bulletin 28 (March 1996) 256-260. 10
© Copyright 2026 Paperzz