Lambdia

683 Chocolate Chips for 100 Cookies, and Why 500 Is a Coin Flip

Drop chips at random into dough, cut it into a hundred cookies, and ask how many chips guarantee no bare cookie nine times out of ten. Five hundred chips, five per cookie on average, works about half the time. Inclusion-exclusion pins the answer at 683, a closed form you can solve on a whiteboard agrees, and the coupon collector's mean of 518.7 is the sophisticated wrong answer.

Chocolate chips are dropped into a batch of dough, each one landing at a uniformly random spot, independently of all the others. The dough is then cut into 100100 equal cookies. How many chips do you need before you can promise, with probability 0.900.90, that not one cookie comes out bare?

Most people answer a few hundred and feel safe doing it. The answer is 683683, and the reason the estimate misses is not that people are bad at arithmetic. It is that they answer a different question: how many chips make the average cookie comfortable. That question has a much smaller answer, and it is the wrong question.

Where a few hundred comes from

With NN chips and 100100 cookies, each cookie gets N/100N/100 chips on average. Set that to something reassuring like three or five and you land on 300300 or 500500. Both feel generous. Neither is close.

At N=500N = 500, so five chips per cookie on average, the exact probability that every single cookie is served is

P(500)  =  0.511556P(500) \;=\; 0.511556\ldots
(1)

which is a coin flip. Five chips per cookie on average, and you fail one batch in two. The average was never the constraint. The worst cookie is the constraint, and averages tell you very little about a minimum over a hundred cells.

The sharpest version of this is to take the average all the way down. Drop exactly 100100 chips, so exactly one per cookie on average, and every cookie is served only in the case where the chips happen to land one per cookie:

100!100100    9.3×1043\frac{100!}{100^{100}} \;\approx\; 9.3 \times 10^{-43}
(2)

A perfect average, and the event has essentially probability zero. That is the whole lesson of the problem compressed into one fraction.

A single named cookie occupies a hundredth of the dough, so each chip misses it with probability 99/10099/100, and all NN chips miss it with probability (99/100)N(99/100)^N. That is the only elementary fact in the problem.

Now flip the requirement around. You want a hundred cookies to succeed at once, which means you have a hundred separate ways to fail. To hold the total failure probability near 0.100.10, each individual cookie needs to fail about a hundred times less often, which is roughly once in a thousand. Solve for that and you get the right order of magnitude immediately:

(99100)683  =  0.001044\left(\frac{99}{100}\right)^{683} \;=\; 0.001044\ldots
(3)

One chance in a thousand per cookie, a hundred cookies, and the batch survives about nine times in ten. The reason 683683 is so much larger than 500500 is that the last few cookies are expensive. Cutting a per-cookie failure rate from one in a hundred to one in a thousand costs another 230230 chips, because you are paying for it exponentially.

The exact model

Equation (3) is a per-cookie statement, and turning it into a statement about all hundred cookies at once needs care, because the hundred failure events are not independent. If one cookie is bare, the chips that missed it landed somewhere, which makes the other cookies slightly better off. The events are negatively correlated.

The occupancy model

Each of the NN chips lands in one of m=100m = 100 cells, independently and uniformly, so the vector of counts is multinomial with equal cell probabilities. The event of interest is that no cell is empty.

Inclusion-exclusion over the set of empty cookies handles the correlation exactly. There are (mj)\binom{m}{j} ways to choose jj cookies to be empty, and the probability that all NN chips avoid a given set of jj cookies is (1j/m)N(1 - j/m)^N, so

P(N)  =  j=0m(1)j(mj)(1jm) ⁣NP(N) \;=\; \sum_{j=0}^{m} (-1)^j \binom{m}{j}\left(1 - \frac{j}{m}\right)^{\!N}
(4)

Evaluate (4) at m=100m = 100 in exact rational arithmetic and the threshold is pinned from both sides:

P(682)=0.899499  <  0.90  <  0.900456=P(683)P(682) = 0.899499\ldots \;<\; 0.90 \;<\; 0.900456\ldots = P(683)
(5)

So 683683 is not merely sufficient, it is the smallest integer that works. The same evaluation at the higher target gives P(915)=0.989902P(915) = 0.989902 and P(916)=0.990003P(916) = 0.990003, so 916916 chips buy you 9999 percent.

Fig. 1 — The exact curve from equation (4). It goes from hopeless to near certain over a few hundred chips, and then spends forever creeping toward one.

The shape of that curve is the reason the reflex answer feels reasonable. The interesting action all happens between 400400 and 800800, which is exactly the range a guess lands in, and inside that range the probability moves faster than intuition expects.

A closed form you can solve on a whiteboard

Nobody sums equation (4) under interview conditions. The version you can actually do treats the hundred cookies as independent, which is the reasoning of the previous section written down honestly:

P~(N)  =  (1(11m) ⁣N) ⁣m\tilde P(N) \;=\; \left(1 - \left(1 - \tfrac{1}{m}\right)^{\!N}\right)^{\!m}
(6)

This is not exact, and because the empty-cookie events are negatively correlated it sits slightly above the truth. At N=683N = 683 it reports 0.9007860.900786 against the true 0.9004560.900456, an error of 0.000330.00033. Small enough that it returns the same integer at both targets, which is the only thing that matters here.

And (6) inverts in one line. Asking for P~(N)q\tilde P(N) \ge q gives

N    ln ⁣(1q1/m)ln ⁣(11/m)N \;\ge\; \frac{\ln\!\left(1 - q^{1/m}\right)}{\ln\!\left(1 - 1/m\right)}
(7)

At m=100m = 100 and q=0.90q = 0.90 the right-hand side is 682.17682.17, so 683683. At q=0.99q = 0.99 it is 915.93915.93, so 916916. Both correct, from a formula you can write down in fifteen seconds and evaluate with logarithms.

For the asymptotic behaviour, the classical coupon-collector tail says that with N=mlnm+cmN = m\ln m + cm chips the probability of full coverage tends to eece^{-e^{-c}}, which inverts to

N    m(lnmln(lnq))N \;\approx\; m\left(\ln m - \ln(-\ln q)\right)
(8)

At m=100m = 100 this gives 685.6685.6 for q=0.90q = 0.90 and 920.5920.5 for q=0.99q = 0.99, so it overshoots by three and five chips respectively. Fine for a sanity check, not good enough to name an integer, which is why the exact model is the one that owns the published number.

The average is a trap twice over

There is a second, more sophisticated wrong answer, and it is worth naming because it looks like the right kind of answer. The expected number of chips needed to cover all 100100cookies is the coupon collector's mean,

mHm  =  100i=11001i    518.74m H_m \;=\; 100 \sum_{i=1}^{100}\frac{1}{i} \;\approx\; 518.74
(9)

That number is correct and it answers nothing that was asked. It is a mean, not a quantile. Stopping at 519519 chips leaves you at about 5858 percent. The coverage time is skewed to the right, so its mean of 518.7518.7 sits above its median of 497497 and a long way below its ninetieth percentile of 683683. Reaching for (9) when the question says 9090 percent repeats the mistake that made 500500 look safe, one level up.

Fig. 2 — The failure event is a minimum over a hundred cells, so a single bare cookie sinks the whole batch.

Fig. 2 also carries the number that makes the threshold intuitive. At 683683 chips the expected count of bare cookies is 100×(0.99)6830.10100 \times (0.99)^{683} \approx 0.10, which is very nearly the failure probability itself. When bare cookies are rare, they essentially never arrive in pairs, so "expected number of failures" and "probability of at least one failure" converge. That is a Poisson approximation doing quiet work, and it is why the crude reasoning of equation (3) lands so close to the exact answer.

The same question in other clothes

Fix the target at qq and let the number of cells mm grow. Equation (8) says the chip count grows like mlnmm \ln m, so it beats the naive mm by a logarithmic factor that never stops growing. Scaling the answer linearly would predict 6,8306{,}830 chips for a thousand cookies; equation (7) at m=1000m = 1000 returns 9,1549{,}154.

The mirror image of this question is the birthday problem, which asks when cells start colliding rather than when they finish filling. Collisions begin around m\sqrt{m} draws and coverage finishes around mlnmm\ln m, so between ten chips and seven hundred chips you pass from "two chips have probably shared a cookie" to "every cookie has probably been served". The same uniform draws, two thresholds, a factor of seventy apart.

Sources and further reading

Every number above was checked three ways before publication: exact rational evaluation of equation (4), the closed form (6) as an independent second model, and a seeded simulation of the actual experiment at 120,000120{,}000 trials per point, which returned 0.900900.90090 at N=683N = 683 against the exact 0.9004560.900456.

Commentaires · 0

Sois le premier à commenter.