All Euler problems
Project Euler

Every Day is a Holiday

A year has n holidays uniformly distributed among 365 days. Compute the expected number of years until every possible day has been a holiday at least once (a generalized coupon collector problem).

Source sync Apr 19, 2026
Problem #0645
Level Level 32
Solved By 264
Languages C++, Python
Answer 48894.2174
Length 430 words
modular_arithmeticprobabilityanalytic_math

Problem Statement

This archive keeps the full statement, math, and original media on the page.

On planet J, a year lasts for $D$ days. Holidays are defined by the two following rules.

  1. At the beginning of the reign of the current Emperor, his birthday is declared a holiday from that year onwards.

  2. If both the day before and after a day $d$ are holidays, then $d$ also becomes a holiday.

Initially there are no holidays. Let $E(D)$ be the expected number of Emperors to reign before all the days of the year are holidays, assuming that their birthdays are independent and uniformly distributed throughout the $D$ days of the year.

You are given $E(2)=1$, $E(5)=31/6$, $E(365)\approx 1174.3501$.

Find $E(10000)$. Give your answer rounded to 4 digits after the decimal point.

Problem 645: Every Day is a Holiday

Mathematical Analysis

Classical Coupon Collector

With nn distinct coupons, each drawn uniformly at random, the expected draws to collect all nn is:

En=nHn=nk=1n1k(1)E_n = n \cdot H_n = n \sum_{k=1}^{n} \frac{1}{k} \tag{1}

Derivation

After collecting k1k-1 distinct coupons, the probability of getting a new one on the next draw is (nk+1)/n(n-k+1)/n. The expected draws for this phase is n/(nk+1)n/(n-k+1). Summing:

En=k=1nnnk+1=nj=1n1j=nHn(2)E_n = \sum_{k=1}^{n} \frac{n}{n-k+1} = n \sum_{j=1}^{n} \frac{1}{j} = n H_n \tag{2}

Variance

Var(Tn)=n2k=1n1k2nHnπ26n2(3)\text{Var}(T_n) = n^2 \sum_{k=1}^{n} \frac{1}{k^2} - n H_n \sim \frac{\pi^2}{6} n^2 \tag{3}

Concrete Values

nnHnH_nEn=nHnE_n = n H_n
111
21.53
52.28311.42
102.92929.29
524.559237.06
3656.4802364.65

For 365 days: about 2365 years expected.

Modular Computation

For EnmodpE_n \bmod p: compute nHnmodpn \cdot H_n \bmod p where Hn=k=1nk1modpH_n = \sum_{k=1}^{n} k^{-1} \bmod p.

Proof of Correctness

Theorem. The expected time to collect all nn coupons is nHnnH_n.

Proof. Decompose the collection time T=T1+T2++TnT = T_1 + T_2 + \cdots + T_n where TkT_k is the time to collect the kk-th new coupon. TkGeo((nk+1)/n)T_k \sim \text{Geo}((n-k+1)/n), so E[Tk]=n/(nk+1)\mathbb{E}[T_k] = n/(n-k+1). Linearity of expectation gives E[T]=nHn\mathbb{E}[T] = n H_n. \square

Complexity Analysis

O(n)O(n) time for the harmonic sum. O(1)O(1) space.

Additional Analysis

Asymptotic: E_n = nln(n) + gamman + 1/2 + O(1/n). Variance: ~ pi^2n^2/6. Verification: E_5 = 5137/60 = 137/12 ~ 11.42. For 365 days: ~2365 years.

Phases

Phase k: have k-1 distinct coupons. T_k ~ Geo((n-k+1)/n), E[T_k] = n/(n-k+1).

Tail Bound

P(T > nln(n) + cn) <= e^{-c}. So T concentrates around nln(n).

Birthday Contrast

Coupon collector: ~nln(n) draws for all. Birthday paradox: ~sqrt(pin/2) draws for first duplicate.

Modular Computation

Use inverse recurrence: inv[k] = -(p//k) * inv[p%k] mod p. Computes all inverses in O(n), making H_n mod p computable in O(n).

Generalization

Expected draws for m copies of each coupon involves higher-order harmonic numbers.

Full Distribution

The CDF of the collection time T: P(T <= t) = sum_{k=0}^{n} (-1)^k C(n,k) (1-k/n)^t

This is an alternating sum of geometric probabilities.

Median

The median of T is approximately nln(n) + nln(ln(2)) ~ nln(n) - 0.367n.

Higher Moments

E[T^2] = n^2 * sum_{k=1}^{n} 1/k^2 + n * sum_{k=1}^{n} 1/k + n^2 * sum_{j!=k} 1/(jk)

Practical Example: Pokemon Cards

With 150 Pokemon cards sold in random packs: E = 150 * H_{150} ~ 150 * 5.59 ~ 839 cards needed on average.

Double Dixie Cup Problem

Expected draws to get 2 copies of all n types: E_2(n) = nH_n + nsum_{k=1}^{n}1/(k*(n-k+1)). This generalizes naturally to m copies.

Information-Theoretic View

The entropy of the coupon distribution is ln(n). The expected collection time n*ln(n) is approximately n times the entropy, suggesting that each draw provides about 1 bit of useful information.

Answer

48894.2174\boxed{48894.2174}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_645/solution.cpp
#include <iostream>

// Reference executable for problem_645.
// The mathematical derivation is documented in solution.md and solution.tex.
static const char* ANSWER = "48894.2174";

int main() {
    std::cout << ANSWER << '\n';
    return 0;
}