Chapter 44
Can You Flip The Magic Coin?
Riddler Express
I claim my coin is magical: on most days it is fair, but once a year, on the summer solstice, it always lands the opposite side from the previous flip. You think there is a chance it really is magical and a chance it is an ordinary fair coin. You ask me to demonstrate by flipping it. How many successive alternating flips must I produce before you are sure the coin is magical (or at least rigged to alternate)?
The Riddler, FiveThirtyEight, June 19, 2020(original post)
Solution
This is Bayes’ rule. Let be the event that the coin is magical, with prior . A magical coin alternates with certainty, so , where is the event of flips all alternating. A fair coin alternates only if it happens to land in one of the two perfectly alternating patterns (starting heads or starting tails) out of , so .
The posterior is Requiring this to reach rearranges to , so , giving and At fifteen alternating flips the posterior is about , just over the line; fourteen would leave you only sure.
The computation
Evaluate the posterior for growing and report the first that reaches confidence.
from fractions import Fraction as F
prior = F(1, 100)
n = 1
while True:
post = (1 * prior) / (1 * prior + F(2, 2**n) * (1 - prior))
if post >= F(99, 100):
print(n, float(post)); break # 15, ~0.994
n += 1
Riddler Classic
King Auric has a set of solid gold spheres, one of each diameter centimetres. He divides them into three groups of equal weight, one for each of his three children, and finds his collection is the smallest that allows such a division. How many spheres does he have? Extra credit: how many would he need to split the gold evenly among two, four, five or six children?
The Riddler, FiveThirtyEight, June 19, 2020(original post)
Solution
A solid sphere’s weight is proportional to the cube of its diameter, so the spheres weigh and the task is to split into three groups of equal sum.
Two simple facts trim the search. First, the total weight is , and for three equal groups this must be divisible by , which forces or . Second, the heaviest sphere, weighing , must fit inside its own group of weight (total), so The candidates are therefore , and a direct partition search finds that none works until where the spheres split into three groups each weighing . For other family sizes the same search gives the minimum collection:
| children | minimum |
|---|---|
The computation
Encode the partition directly: for each in turn, test whether splits into groups of equal sum (assign each cube to a bin, pruning bins that would overflow the target). Report the first that works.
def splits(vals, k):
total = sum(vals)
if total % k: return False
target = total // k
vals = sorted(vals, reverse=True)
if vals[0] > target: return False
bins = [0] * k
def place(i):
if i == len(vals): return True
seen = set()
for b in range(k):
if bins[b] in seen or bins[b] + vals[i] > target: continue
seen.add(bins[b]); bins[b] += vals[i]
if place(i + 1): return True
bins[b] -= vals[i]
return False
return place(0)
def min_N(k):
N = 1
while not splits([n ** 3 for n in range(1, N + 1)], k): N += 1
return N
for k in (2, 3, 4, 5, 6):
print(k, min_N(k)) # 2->12, 3->23, 4->24, 5->24, 6->35