Chapter 281
Can You Eat All The Chocolates?
The Riddler for October 2, 2020. The Express counts the ways a number is a difference of two squares, and the Classic shows a surprisingly clean answer for which chocolate gets eaten last.
Riddler Express
A number is “hip” if it can be written as a difference of two perfect squares; it is doubly hip if it can be written that way in two ways, and so on. In how many ways can be written as a difference of two perfect squares?
The Riddler, FiveThirtyEight, October 2, 2020(original post)
Solution
Write with integers . Each representation corresponds to a factorisation with , and we recover , . These are integers exactly when and have the same parity.
Now has divisors, hence factor pairs. Since is even it cannot be a product of two odd factors, so every same-parity pair must be eveneven. Of the pairs, six mix an even and an odd factor (, , , , , ) and are rejected; the other six are eveneven. So is hip in ways: , , , , , . (In general, the number of representations is the count of same-parity factor pairs: for odd it is counting , and for it is .)
The computation
Encode the definition: search for integer pairs with .
N = 1400
reps = []
for A in range(1, N // 2 + 2): # A = (s+t)/2 <= (N+1)/2
b2 = A * A - N
if b2 < 0:
continue
B = int(b2 ** 0.5)
if B * B == b2:
reps.append((A, B))
print(len(reps), reps) # 6 [(39,11),(45,25),...,(351,349)]
There are exactly six representations.
Riddler Classic
A bag holds chocolates: milk and dark. You draw one at random and eat it, then keep drawing and eating as long as each draw matches the kind you started the streak with. The first time you draw the other kind, you put it back and start a fresh streak (eating the next draw, whatever it is). You continue until all are eaten. What is the probability the last chocolate eaten is milk?
The Riddler, FiveThirtyEight, October 2, 2020(original post)
Solution
The answer is exactly , independent of the -and- split, by strong induction on the total number of chocolates . Write for the probability the last chocolate is milk when starting with dark and milk (). Base case : one dark and one milk are equally likely to be eaten second, so .
Inductive step: assume for every bag of fewer than chocolates, and start with . Look at the opening streak, which begins with a free draw and then keeps eating the same kind until the other kind appears. Exactly three things can happen.
You draw all darks before any milk. The chance the first draws (without replacement) are all dark is . The bag is then all milk, so the last chocolate is milk.
You draw all milks first, with probability ; the last chocolate is dark.
Otherwise you eat some chocolates of one kind, then draw the other kind and return it. The bag now has strictly fewer than chocolates and still contains both kinds (the returned one, plus at least one of the kind you were eating), so by the inductive hypothesis the last chocolate is milk with probability .
The crucial fact is that , so : the two “clean sweep” cases are equally likely. Letting , So the probability is, exactly,
The computation
Encode the eating process and simulate: start each streak with a free draw, keep eating while the kind matches, otherwise restart.
import random
def last_eaten(rng):
pool = ['M', 'M'] + ['D'] * 8
last, streak = None, None
while pool:
pick = pool[rng.randrange(len(pool))]
if streak is None or pick == streak:
pool.remove(pick); last = pick; streak = pick
else:
streak = None # other kind: put back, restart
return last
rng = random.Random(0)
trials = 300_000
milk = sum(1 for _ in range(trials) if last_eaten(rng) == 'M')
print(round(milk / trials, 4)) # ~0.4988 (exactly 1/2)
The simulation hovers at : the last chocolate is milk exactly half the time.