Skip to content
Vamshi Jandhyala

Books · The Riddler

Chapter 85

Can You Fold All Your Socks?

You have 1414 pairs of socks and a chair holding at most 99 socks. Draw socks one at a time at random; a sock whose partner is already on the chair forms a folded pair (freeing that spot), otherwise it takes an empty spot. What is the probability you fold all 1414 pairs without ever needing a tenth spot? Extra credit: for other numbers of pairs and spots?

Solution

The chair only ever holds unmatched socks, so the state is captured by two numbers: rr, the untouched pairs still in the basket, and oo, the lone socks on the chair. The next sock you draw is one of 2r+o2r + o equally likely socks. With probability 2r2r+o\tfrac{2r}{2r+o} it belongs to an untouched pair and opens a new spot (needing o<9o < 9), and with probability o2r+o\tfrac{o}{2r+o} it matches a sock on the chair and folds a pair, freeing a spot. Writing P(r,o)P(r, o) for the chance of finishing from that state, with P(0,o)=1P(0, o) = 1 once only folds remain, P(r,o)=2r2r+oP(r1,o+1)[o<9]+o2r+oP(r,o1),P(r, o) = \frac{2r}{2r+o}\,P(r-1, o+1)\,[o < 9] + \frac{o}{2r+o}\,P(r, o-1), where running out of room (o=9o = 9 with no sock to fold) contributes nothing. Starting empty, P(14,0)=0.70049.P(14, 0) = \boxed{0.70049}. A few values show how demanding the chair size is: 1414 pairs succeed only 0.210.21 of the time with 77 spots, 0.700.70 with 99, and 0.970.97 with 1111. The required chair grows roughly like the square root of the sock count, not linearly, so doubling the wardrobe to 2828 pairs drops even a 1313-spot chair below a 10%10\% success rate.

The computation

Solve the recurrence with memoisation: from each state, weight opening a new pair and folding an open one by their draw probabilities.

from functools import lru_cache
@lru_cache(None)
def P(r, o, slots):
    if r == 0: return 1.0
    total = 2 * r + o; p = 0.0
    if o < slots: p += (2 * r / total) * P(r - 1, o + 1, slots)
    if o > 0:     p += (o / total) * P(r, o - 1, slots)
    return p
print(round(P(14, 0, 9), 5))                   # 0.70049