Chapter 259
Can You Get The Gloves Out Of The Box?
The Riddler for March 27, 2020. The Express counts the ordered ways to empty a box of ten gloves when you only ever pull two, three or four at a time. The Classic rolls a die to relabel a die, over and over, and asks how long until every face agrees.
Riddler Express
You pull gloves from a box two, three or four at a time (never any other number), and there are gloves left. In how many distinct ordered ways can you remove all ? (Order matters: then then differs from then then .)
The Riddler, FiveThirtyEight, March 27, 2020(original post)
Solution
Let be the number of ordered ways to remove gloves in pulls of , or . The very first pull is a , a or a , and after it you face the same kind of problem with , or gloves left. So with (one way to remove nothing: stop) and for . Climbing from the bottom, So there are distinct ways to empty the box.
The computation
Encode the same recurrence directly.
def ways(n, parts=(2, 3, 4)):
if n == 0: return 1
return sum(ways(n - p) for p in parts if n - p >= 0)
print(ways(10)) # 17
The recursion counts orderings, as boxed.
Riddler Classic
Start with a fair -sided die and roll it six times; write those six results on the faces of a new die (so a value rolled twice appears on two faces). Roll the new die six times to make the next die, and so on. Eventually every face shows the same number. On average, how many rolls does this take? Extra credit: what about an -sided die?
The Riddler, FiveThirtyEight, March 27, 2020(original post)
Solution
What matters about a die is not which numbers are on it but how its six faces split into repeated values: a die showing behaves exactly like one showing . So the state is a partition of (the multiplicities of the distinct values), and there are eleven of them, from (all six distinct) to (all the same, the absorbing state).
One round rolls the current die six times. Each roll lands on a value with probability equal to that value’s multiplicity over , so the six rolls are a multinomial draw, and the counts they produce are the multiplicities of the next die. That fixes the transition probability between any two partitions. Writing for the expected number of rounds left, satisfies off the absorbing state, with . Solving the eleven equations exactly, starting from the all-distinct die, gives Since each round is six rolls, that is about rolls. For the extra credit, the same machinery (a Markov chain on partitions of ) applies; the expected number of rounds grows like as increases.
The computation
Encode the chain on partitions of : build each partition’s transition law from the multinomial of six rolls, then solve the linear system for the expected rounds, exactly.
import itertools, math, sympy as sp
from collections import defaultdict
def partitions(n):
out = []
def rec(n, mx, cur):
if n == 0: out.append(tuple(sorted(cur, reverse=True))); return
for k in range(min(n, mx), 0, -1): rec(n - k, k, cur + [k])
rec(n, n, []); return sorted(set(out))
parts = partitions(6); idx = {p: i for i, p in enumerate(parts)}
def transition(p): # die with face-multiplicities p
out = defaultdict(lambda: sp.Integer(0))
for cv in itertools.product(range(7), repeat=len(p)):
if sum(cv) != 6: continue
coef = math.factorial(6)
for c in cv: coef //= math.factorial(c)
pr = sp.Rational(coef)
for c, m in zip(cv, p): pr *= sp.Rational(m, 6)**c
out[tuple(sorted((c for c in cv if c), reverse=True))] += pr
return out
n = len(parts); A = sp.zeros(n, n); b = sp.zeros(n, 1)
for p in parts:
i = idx[p]; A[i, i] += 1
if p == (6,): continue
b[i] = 1
for q, pr in transition(p).items(): A[i, idx[q]] -= pr
E = A.LUsolve(b)
print(E[idx[(1,) * 6]]) # 31394023/3251248 ~ 9.656
The exact expected number of rounds from the all-distinct die is , as boxed.