From Alex Vornsand, a puzzle for your daily routine:
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?
In [1]:
import random
import numpy as np
import matplotlib.pyplot as plt
BOTTLE_NUM = 100
In [2]:
odds = []
odds_not_half_pulled = 1
for day in range(BOTTLE_NUM+1):
odds_half_pulled = 1 - odds_not_half_pulled
print(f'{day:>6.0f} {odds_half_pulled:<6.5f}')
odds_not_half_pulled *= (BOTTLE_NUM - day)/(BOTTLE_NUM)
odds.append(odds_half_pulled)
In [3]:
plt.plot((0,BOTTLE_NUM),(0.5,0.5))
plt.plot(odds)
plt.xlim(0, BOTTLE_NUM)
plt.ylim(0, 1)
plt.show()
In [4]:
plt.plot((0,BOTTLE_NUM),(0.5,0.5))
plt.plot(odds)
plt.plot((12,12),(0.3,0.7))
plt.xlim(10, 20)
plt.ylim(0.4, 0.6)
plt.show()
In [5]:
def simulate_picking(bottle_num):
for day in range(BOTTLE_NUM):
if random.random() < (day/BOTTLE_NUM):
return day
In [6]:
TRIALS = 1000000
bins = list(range(101))
n, bins, patches = plt.hist([simulate_picking(BOTTLE_NUM) for _ in range(TRIALS)], bins, normed=1)
plt.xlim(0, 40)
plt.show()
On average, you will have to wait 12 days