Skip to content
Vamshi Jandhyala

Books · The Riddler

Chapter 38

Can You Find A Matching Pair Of Socks?

The Riddler for December 20, 2019. The Express is an office-vote puzzle: from three rounded-down percentages, reconstruct the smallest possible office. The Classic asks, on average, how many loose socks you must pull from a drawer to get a matching pair.

Riddler Express

You record the share of the vote each party theme received, but in a hurry you keep only the digits before the decimal point (so 35.0%35.0\%, 35.17%35.17\% and 35.92%35.92\% are all written “3535”). The winner came out at 73%73\%, second place at 58%58\%, and third at 32%32\% (people evidently voted more than once). Based on these recorded percentages, what is the minimum number of people who could work in the office? Extra credit: what is the greatest number of people the office cannot have?

The Riddler, FiveThirtyEight, December 20, 2019(original post)

Solution

Recording “7373” for a count of aa votes out of NN people means 73100a/N<7473 \le 100a/N < 74, that is, the half-open interval [73N100,74N100)\bigl[\tfrac{73N}{100}, \tfrac{74N}{100}\bigr) contains a whole number aa. The same goes for 5858 and 3232. (Since people cheated and voted repeatedly, the counts are just nonnegative integers, not tied to NN.) Each such interval has length N/100N/100.

That length is the whole story. Once N100N \ge 100 every one of these intervals is at least 11 wide and so always contains an integer, making all three percentages achievable. So only N<100N < 100 can fail, and both a smallest working NN and a largest failing NN exist; find them by checking the intervals.

Searching upward, the first NN for which all three intervals catch an integer is 34,\boxed{34}, realised by 25/34=73.53%25/34 = 73.53\%, 20/34=58.82%20/34 = 58.82\% and 11/34=32.35%11/34 = 32.35\%.

For the extra credit, search downward from 9999. The largest office size for which some percentage is unreachable is 88:\boxed{88}: between 51/88=57.95%51/88 = 57.95\% and 52/88=59.09%52/88 = 59.09\% no count yields 58%58\%. Every size 8989 and above works, in keeping with the N100N \ge 100 guarantee.

The computation

Encode the rule directly: a percentage pp is attainable for office size NN when some integer count aa has 100a/N=p\lfloor 100a/N\rfloor = p. Find the smallest NN that attains all of 73,58,3273, 58, 32, and the largest NN that fails at least one.

import math
def attainable(N, p):
    return any(math.floor(100*a/N) == p for a in range(0, 3*N + 1))
def works(N):
    return all(attainable(N, p) for p in (73, 58, 32))
min_N    = next(N for N in range(1, 500) if works(N))
max_fail = max(N for N in range(1, 100) if not works(N))
print(min_N, max_fail)                # 34 88

The smallest possible office is 3434 people, and the greatest impossible size is 8888, as boxed.

Riddler Classic

I have 10 pairs of distinct socks loose in a drawer. In the dark I pull socks out one at a time until I have a matching pair among those removed. On average, how many socks do I pull out to get my first matching pair? Extra credit: what if I have NN pairs?

The Riddler, FiveThirtyEight, December 20, 2019(original post)

Solution

Suppose the first ii socks pulled all came from different pairs, so no match yet. Among the 2Ni2N - i socks still in the drawer, exactly ii are the partners of the ones already out (drawing any of them makes a pair), and the other 2N2i2N - 2i come from untouched pairs. So the next sock avoids a match with probability 2N2i2Ni.\frac{2N - 2i}{2N - i}. Let TT be the draw on which the first pair appears. Getting through kk draws with no match has probability P(T>k)=i=0k12N2i2NiP(T>k) = \prod_{i=0}^{k-1}\frac{2N-2i}{2N-i}, and summing the survival probabilities, E[T]=k=0Ni=0k12N2i2Ni.\mathbb{E}[T] = \sum_{k=0}^{N} \prod_{i=0}^{k-1}\frac{2N - 2i}{2N - i}. For N=10N = 10 this evaluates to 262144461895.68.\frac{262144}{46189} \approx \boxed{5.68}. So even though the pigeonhole guarantee is 1111 socks, on average fewer than six suffice.

For the extra credit, the early factors behave like 1i2N1 - \tfrac{i}{2N}, so P(T>k)ek2/(4N)P(T>k) \approx e^{-k^2/(4N)} and the sum is well approximated by a Gaussian integral, giving E[T]πN  as N.\mathbb{E}[T] \to \sqrt{\pi N}\ \ \text{as } N \to \infty. For N=10N=10 that estimate is 10π5.60\sqrt{10\pi} \approx 5.60, already close to the exact 5.685.68.

The computation

Evaluate the exact product-sum, and confirm it by pulling socks at random until a pair appears.

from fractions import Fraction as F
import numpy as np
def expected(N):
    E = F(0); p = F(1)
    for k in range(N + 1):
        E += p
        p *= F(2*N - 2*k, 2*N - k)        # survival for next draw
    return E
print(expected(10), float(expected(10)))   # 262144/46189 ~ 5.676
rng = np.random.default_rng(0); runs = 1_000_000; total = 0
for _ in range(runs):
    drawer = rng.permutation([i for i in range(10) for _ in range(2)])
    seen = set()
    for n, s in enumerate(drawer, 1):
        if s in seen:
            total += n; break
        seen.add(s)
print(total / runs)                         # ~5.68