Chapter 3
Can You Find Your Pills?
I’ve been prescribed to take pills of a certain medication every day for days, so I have a bottle with pills. Each morning, I take two pills out of the bottle at random.
On the first morning, these are guaranteed to be two full pills. I consume one of them, split the other in half using a precision blade, consume half of that second pill, and place the remaining half back into the bottle.
On subsequent mornings when I take out two pills, there are three possibilities:
I get two full pills. As on the first morning, I split one and place the unused half back into the bottle. I get one full pill and one half-pill, both of which I consume. I get two half-pills. In this case, I take out another pill at random. If it’s a half-pill, then I consume all three halves. But if it’s a full pill, I split it and place the unused half back in the bottle. Assume that each pill, whether it is a full pill or a half-pill, is equally likely to be taken out of the bottle.
On the th day, I again take out two pills and consume them. In a rush, I immediately throw the bottle in the trash before bothering to check whether I had just consumed full pills or half-pills. What’s the probability that I took the full dosage, meaning I don’t have to dig through the trash for a remaining half-pill?
Solution
Track the bottle by its contents , the number of full pills and half-pills. The whole process is a Markov chain on these states. Drawing two of the pills, the three immediate outcomes and their probabilities are In the two-halves case a third pill is drawn from the remaining : it is full with probability , sending (split it, return a half), or a half with probability , sending .
I have taken the full dosage exactly when the bottle empties on the tenth draw, that is, when exactly two pills remain after the first nine mornings. Pushing the distribution from forward nine days and summing the weight on states with gives the exact
The computation
Evolve the exact distribution over states with rational arithmetic: each day, redistribute every state’s probability across the four outcomes above, then read off the total probability on the two-pill states after nine days.
from fractions import Fraction as F
from math import comb
from collections import defaultdict
dist = {(15, 0): F(1)}
for _ in range(9):
nd = defaultdict(F)
for (f, h), pr in dist.items():
t = f + h
if t < 2: nd[(f, h)] += pr; continue # fewer than two pills: carry
c2 = F(1, comb(t, 2))
if f >= 2: nd[(f-2, h+1)] += pr*comb(f, 2)*c2 # two full
if f and h: nd[(f-1, h-1)] += pr*(f*h)*c2 # one full, one half
if h >= 2: # two halves, draw a third
p2h = comb(h, 2)*c2
if t > 2:
nd[(f-1, h-1)] += pr*p2h*F(f, t-2) # third is full: split it
nd[(f, h-3)] += pr*p2h*F(h-2, t-2) # third is half: consume all
else:
nd[(f, h-3)] += pr*p2h # no third pill to draw
dist = nd
print(sum(pr for (f, h), pr in dist.items() if f + h == 2)) # 80529/101920