Skip to content
Vamshi Jandhyala

Books · The Fiddler: Solutions

Chapter 79

Can You Solve the Tricky Mathematical Treat?

A trick-or-treat host hands you an enormous bag containing exactly three peanut butter cups, your favourite, with the rest of the bag filled with individual candy corn kernels, not your favourite. You have no idea how many kernels there are: any whole number, including zero, seems equally likely. You draw three candies at random, one after another, and every one is a peanut butter cup. Whatever remains is candy corn. How many kernels do you expect are left in the bag?

The Fiddler, Zach Wissner-Gross, October 25, 2024(original post)

Solution

Start from a flat prior: before drawing, the number of kernels nn is equally likely to be any of 0,1,2,0,1,2,\dots. If there are nn kernels, the bag holds n+3n+3 candies, and the chance of drawing three peanut butter cups in a row is P(n)=3n+32n+21n+1=6(n+1)(n+2)(n+3).P(n)=\frac{3}{n+3}\cdot\frac{2}{n+2}\cdot\frac{1}{n+1}=\frac{6}{(n+1)(n+2)(n+3)}. After seeing three cups, the posterior weight on nn is proportional to this. Two telescoping sums finish it, n01(n+1)(n+2)(n+3)=14,n0n(n+1)(n+2)(n+3)=14,\begin{gather*} \sum_{n\ge0}\frac{1}{(n+1)(n+2)(n+3)}=\frac14,\\[2pt] \sum_{n\ge0}\frac{n}{(n+1)(n+2)(n+3)}=\frac14, \end{gather*} so the posterior mean is 14/14\tfrac14\big/\tfrac14: 1\boxed{1} candy corn kernel. The three draws are strong evidence the bag is nearly all peanut butter cups, and a single expected kernel is what survives.

The computation

Form the posterior directly: weight each nn by the probability of drawing three cups, then take the weighted mean of nn. The truncated sum closes in on 11.

from fractions import Fraction as F
S = Sn = F(0)
for n in range(200000):
    w = F(6, (n + 1) * (n + 2) * (n + 3))     # posterior weight (flat prior)
    S += w; Sn += n * w
print(float(Sn / S))                           # -> 1.0

Extra Credit

Now the bag has NN peanut butter cups (N3N\ge3), and you draw kk of them in a row (3kN3\le k\le N). How many kernels do you expect remain?

Solution

With nn kernels the probability of drawing kk cups in a row is i=0k1NiN+ni\prod_{i=0}^{k-1}\frac{N-i}{N+n-i}, so the posterior weight on nn is proportional to i=0k11N+ni\prod_{i=0}^{k-1}\frac{1}{N+n-i}, a product of kk consecutive reciprocals. The same telescoping generalises to a tidy closed form,   E[n]=Nk+1k2  (k>2).\boxed{\;\mathbb{E}[n]=\frac{N-k+1}{k-2}\;}\qquad(k>2). For the original N=k=3N=k=3 this is 11=1\tfrac11=1. The more cups you pull, the more lopsided the evidence: drawing kk near NN drives the expected kernel count toward zero, while k=3k=3 leaves it largest.

The computation

The same posterior sum with the general weight reproduces Nk+1k2\tfrac{N-k+1}{k-2} across several N,kN,k.

def expected(N, k, M=300000):
    S = Sn = F(0)
    for n in range(M):
        w = F(1)
        for i in range(k): w *= F(1, N + n - i)
        S += w; Sn += n * w
    return float(Sn / S)
for N, k in [(3, 3), (5, 4), (6, 4), (5, 5)]:
    print(N, k, round(expected(N, k), 4))      # 1, 1, 1.5, 0.333  == (N-k+1)/(k-2)