Riddler Express

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)


     0 0.00000
     1 0.00000
     2 0.01000
     3 0.02980
     4 0.05891
     5 0.09655
     6 0.14172
     7 0.19322
     8 0.24969
     9 0.30972
    10 0.37184
    11 0.43466
    12 0.49685
    13 0.55723
    14 0.61479
    15 0.66872
    16 0.71841
    17 0.76346
    18 0.80367
    19 0.83901
    20 0.86960
    21 0.89568
    22 0.91759
    23 0.93572
    24 0.95050
    25 0.96238
    26 0.97179
    27 0.97912
    28 0.98476
    29 0.98903
    30 0.99221
    31 0.99455
    32 0.99624
    33 0.99744
    34 0.99829
    35 0.99887
    36 0.99926
    37 0.99953
    38 0.99970
    39 0.99982
    40 0.99989
    41 0.99993
    42 0.99996
    43 0.99998
    44 0.99999
    45 0.99999
    46 1.00000
    47 1.00000
    48 1.00000
    49 1.00000
    50 1.00000
    51 1.00000
    52 1.00000
    53 1.00000
    54 1.00000
    55 1.00000
    56 1.00000
    57 1.00000
    58 1.00000
    59 1.00000
    60 1.00000
    61 1.00000
    62 1.00000
    63 1.00000
    64 1.00000
    65 1.00000
    66 1.00000
    67 1.00000
    68 1.00000
    69 1.00000
    70 1.00000
    71 1.00000
    72 1.00000
    73 1.00000
    74 1.00000
    75 1.00000
    76 1.00000
    77 1.00000
    78 1.00000
    79 1.00000
    80 1.00000
    81 1.00000
    82 1.00000
    83 1.00000
    84 1.00000
    85 1.00000
    86 1.00000
    87 1.00000
    88 1.00000
    89 1.00000
    90 1.00000
    91 1.00000
    92 1.00000
    93 1.00000
    94 1.00000
    95 1.00000
    96 1.00000
    97 1.00000
    98 1.00000
    99 1.00000
   100 1.00000

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