## 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

runs = 100000
init_full_tablets = 100
num_days_till_half_tablet = 0

for _ in range(runs):
num_full_tablets = init_full_tablets
num_half_tablets = 0
while(True):
prob_full_tablet = num_full_tablets/(num_full_tablets + num_half_tablets)
prob_half_tablet = 1 - prob_full_tablet
num_days_till_half_tablet += 1
if random() <= prob_full_tablet:
num_full_tablets -= 1
num_half_tablets += 1
else:
num_half_tablets -= 1
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.