Chapter 79
Can You Bat .299 In 299 Games?
Riddler Express
You draw three coins, each independently a penny, nickel, dime or quarter ( cents) with equal chance, and buy strings of those lengths. What is the probability the three strings can form a triangle?
Solution
Three lengths form a triangle exactly when the longest is shorter than the sum of the other two. With ordered draws there are equally likely triples; checking the triangle inequality, of them qualify, so The quarter is the spoiler: at it dwarfs the other coins, so any triple containing a quarter needs two more sizeable coins to reach past it, and most do not.
The computation
Enumerate all coin triples and count those satisfying the triangle inequality.
from fractions import Fraction as F
from itertools import product
coins = [1, 5, 10, 25]
good = sum(x + y > z and x + z > y and y + z > x
for x, y, z in product(coins, repeat=3))
print(F(good, 64)) # 11/32
Riddler Classic
A player has four at-bats per game, so after games their average is for some whole number of hits . For many there is an making the rounded average (to three decimals) equal . What is the largest for which no such exists?
Solution
For a given number of games , you can hit anywhere from to times, giving averages in steps of . A matching season needs one of these, rounded to three places, to equal . When is large the steps are tiny and some hit count always rounds correctly, but for small the gaps are coarse enough to skip the target. Scanning downwards, the largest with no workable hit count is With games the available averages step by , and none of them rounds to .
The computation
For each from large to small, test every possible hit count and stop at the first for which none rounds to .
for g in range(1000, 1, -1):
if all(round(h / (4 * g), 3) != g / 1000 for h in range(4 * g + 1)):
break
print(g) # 239