Library · The Fiddler: Solutions · Chapter 142

Can You Light Up the Pinball Machine?

Revised Report an error

A pinball game has four lanes, all initially unlit. Every flip sends the ball through exactly one of the four lanes, chosen at random, and toggles that lane: an unlit lane becomes lit, and a lit lane becomes unlit. On average, how many flips does it take until all four lanes are lit?

The Fiddler, Zach Wissner-Gross, September 29, 2023(original post)

The official solution appeared in the post of October 6, 2023 and gives 64/364/3 flips for the main puzzle and N=6N = 6 for the Extra Credit. Both agree with the answers below.

Solution

Which lanes are lit does not matter, only how many. With kk of the four lit, the next ball goes through an unlit lane with probability (4−k)/4(4 - k)/4, lighting it, and through a lit lane with probability k/4k/4, putting it out. So the number lit moves one step up or down along 0,1,2,3,40, 1, 2, 3, 4.

The number of lit lanes, with the chance of each step up (above) and down (below).
The number of lit lanes, with the chance of each step up (above) and down (below).

To get from 00 lit to 44 lit, the count must pass through each step k→k+1k \to k + 1 for the first time, in order. So the expected total is the sum of the expected waits tkt_k for each of those first crossings. Each tkt_k follows from the one before it:

  • From 00 lit, any lane lights, so t0=1t_0 = 1.

  • From kk lit, one flip either lights a new lane (probability (4−k)/4(4 - k)/4), and the wait is over, or puts one out (probability k/4k/4). In the second case it takes tk−1t_{k-1} flips on average to get back to kk, and then a fresh wait tkt_k. So tk  =  1+k4(tk−1+tk),that is,tk  =  4+k tk−14−k.t_k \;=\; 1 + \frac{k}{4}\bigl(t_{k-1} + t_k\bigr), \qquad\text{that is,}\qquad t_k \;=\; \frac{4 + k\,t_{k-1}}{4 - k} .

Working up the steps: t0=1,t1=4+13=53,t_0 = 1, \qquad t_1 = \frac{4 + 1}{3} = \frac53, t2=4+2⋅532=113,t3=4+3⋅1131=15.t_2 = \frac{4 + 2 \cdot \frac53}{2} = \frac{11}{3}, \qquad t_3 = \frac{4 + 3 \cdot \frac{11}{3}}{1} = 15 . The last step is by far the slowest: with three lanes lit, the ball finds the dark one only a quarter of the time, and three times as often it undoes earlier progress. Adding the four waits,  1+53+113+15  =  643  ≈  21.33 flips. \boxed{\ 1 + \frac53 + \frac{11}{3} + 15 \;=\; \frac{64}{3} \;\approx\; 21.33 \text{ flips}. \ }

The computation

The program writes down the defining equations for the expected number of flips still needed, f(k)f(k), with kk lanes lit: f(N)=0f(N) = 0, and otherwise one flip followed by f(k−1)f(k - 1) or f(k+1)f(k + 1) with the chances above. It solves them exactly, with fractions, by elimination. It does not use the step-by-step waits of the solution.

from fractions import Fraction

def expected_flips(n):
    """Expected flips to light all n lanes, starting dark.
    f[k] = expected flips still needed with k lanes lit:
      f[n] = 0,
      f[k] = 1 + (k/n) f[k-1] + ((n-k)/n) f[k+1].
    Solved exactly by eliminating down the chain: write
    f[k] = A[k] + B[k] f[k+1], then substitute back up."""
    A, B = [], []
    for k in range(n):
        down, up = Fraction(k, n), Fraction(n - k, n)
        a_prev, b_prev = (A[-1], B[-1]) if k else (0, 0)
        scale = 1 - down * b_prev
        A.append((1 + down * a_prev) / scale)
        B.append(up / scale)
    f = Fraction(0)                  # f[n]
    for k in reversed(range(n)):
        f = A[k] + B[k] * f
    return f

print("main:", expected_flips(4))

It prints

main: 64/3

Extra Credit

With NN lanes, let E(N)E(N) be the average number of flips until all are lit. Each extra lane roughly doubles EE, but not exactly. For which NN is the ratio E(N+1)/E(N)E(N + 1)/E(N) smallest?

Solution

With NN lanes the same reasoning gives t0=1t_0 = 1 and tk  =  N+k tk−1N−k,E(N)  =  t0+t1+⋯+tN−1.t_k \;=\; \frac{N + k\,t_{k-1}}{N - k}, \qquad E(N) \;=\; t_0 + t_1 + \dots + t_{N-1} . This recursion has a tidy solution. Scale each wait by a binomial coefficient, sk=(N−1k)tks_k = \binom{N-1}{k} t_k. Since (N−1k)1N−k=1N(Nk)\binom{N-1}{k} \frac{1}{N - k} = \frac1N\binom{N}{k} and kN(Nk)=(N−1k−1)\frac{k}{N}\binom{N}{k} = \binom{N-1}{k-1}, the recursion becomes sk  =  (Nk)+(N−1k−1)tk−1  =  (Nk)+sk−1.s_k \;=\; \binom{N}{k} + \binom{N-1}{k-1} t_{k-1} \;=\; \binom{N}{k} + s_{k-1} . So sks_k is a running total of binomial coefficients, and tk  =  (N0)+(N1)+⋯+(Nk)(N−1k),E(N)  =  ∑k=0N−1tk.t_k \;=\; \frac{\binom{N}{0} + \binom{N}{1} + \dots + \binom{N}{k}}{\binom{N-1}{k}}, \qquad E(N) \;=\; \sum_{k=0}^{N-1} t_k . For N=4N = 4 this gives 1,53,113,151, \frac53, \frac{11}{3}, 15 again.

Why is EE close to doubling? The last wait dominates: its numerator is every binomial coefficient except the last, 2N−12^N - 1, over (N−1N−1)=1\binom{N-1}{N-1} = 1. The next wait is about 2N/(N−1)2^N/(N - 1), and the rest are much smaller. So E(N)E(N) is a little more than 2N2^N, and the ratio E(N+1)/E(N)E(N+1)/E(N) is a little less than 22 for large NN, creeping back up towards 22. For small NN the ratio starts well above 22 (E(1)=1E(1) = 1, E(2)=4E(2) = 4). So it falls, bottoms out, and rises. Exact values show where:

NN E(N)E(N) E(N+1)/E(N)E(N+1)/E(N)
33 1010 2.13332.1333
44 64/364/3 ≈21.33\approx 21.33 22
55 128/3128/3 ≈42.67\approx 42.67 1.95001.9500
66 416/5416/5 =83.2= 83.2 151/78≈1.9359151/78 \approx 1.9359
77 2416/152416/15 ≈161.07\approx 161.07 1.93761.9376
88 32768/10532768/105 ≈312.08\approx 312.08 1.94531.9453
99 21248/3521248/35 ≈607.09\approx 607.09 1.95451.9545

The minimum among these is at N=6N = 6. To rule out a smaller ratio for some large NN, bound EE from both sides. From below, the last wait alone gives E(N+1)≥2N+1−1E(N + 1) \ge 2^{N+1} - 1. From above, for N≥5N \ge 5: the waits t0=1t_0 = 1 and t1=(N+1)/(N−1)t_1 = (N + 1)/(N - 1) add to at most 33; the last wait is below 2N2^N; the next is below 2N/(N−1)2^N/(N - 1); and each of the other N−4N - 4 waits has a numerator below 2N2^N and a denominator at least (N−12)\binom{N-1}{2}, so together they are below 2N⋅2/(N−1)2^N \cdot 2/(N - 1). Hence E(N)  <  2N(1+3N−1)+3,E(N) \;<\; 2^N\left(1 + \frac{3}{N - 1}\right) + 3 , and so E(N+1)E(N)  >  2N+1−12N(1+3N−1)+3.\frac{E(N + 1)}{E(N)} \;>\; \frac{2^{N+1} - 1}{2^N\bigl(1 + \frac{3}{N-1}\bigr) + 3} . Apart from the negligible −1-1 and +3+3, the right side is 2(N−1)/(N+2)2(N - 1)/(N + 2), which exceeds 151/78151/78 once N≥92N \ge 92, and the −1-1 and +3+3 cannot change that. The exact computation covers every NN up to 100100, so  N=6,E(7)E(6)=15178≈1.936. \boxed{\ N = 6, \qquad \frac{E(7)}{E(6)} = \frac{151}{78} \approx 1.936 . \ }

The computation

The equation solver from The computation above, run for every NN up to 101101, gives the exact ratios up to N=100N = 100; the program also confirms that they rise steadily after the minimum.

E = {n: expected_flips(n) for n in range(1, 102)}
ratio = {n: E[n + 1] / E[n] for n in range(1, 101)}
best = min(ratio, key=ratio.get)
print("extra credit: N =", best, " ratio =", ratio[best])
print("ratio rises after it:",
      all(ratio[n] < ratio[n + 1] for n in range(best, 100)))

It prints

extra credit: N = 6  ratio = 151/78
ratio rises after it: True
Report an error on this page

Reports are stored by Netlify. See the privacy note.