Riddler Express
You take half of a vitamin every morning. The vitamins are sold in a bottle of 100 (whole) tablets, so at first you have to cut the tablets in half. Every day you randomly pull one thing from the bottle — if it’s a whole tablet, you cut it in half and put the leftover half back in the bottle. If it’s a half-tablet, you take the vitamin. You just bought a fresh bottle. How many days, on average, will it be before you pull a half-tablet out of the bottle?
Extra credit: What if the halves are less likely to come up than the full tablets? They are smaller, after all.
Solution
from random import random
= 100000
runs = 100
init_full_tablets = 0
num_days_till_half_tablet
for _ in range(runs):
= init_full_tablets
num_full_tablets = 0
num_half_tablets while(True):
= num_full_tablets/(num_full_tablets + num_half_tablets)
prob_full_tablet = 1 - prob_full_tablet
prob_half_tablet += 1
num_days_till_half_tablet if random() <= prob_full_tablet:
-= 1
num_full_tablets += 1
num_half_tablets else:
-= 1
num_half_tablets break
print(num_days_till_half_tablet/runs)
On average, it will take \(13.2\) days to pull a half-tablet out of the bottle.