All Euler problems
Project Euler

Combinatoric Selections

How many values of C(n, r) for 1 <= n <= 100 and 0 <= r <= n are greater than one million?

Source sync Apr 19, 2026
Problem #0053
Level Level 02
Solved By 65,171
Languages C++, Python
Answer 4075
Length 353 words
combinatoricsgeometrysequence

Problem Statement

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

There are exactly ten ways of selecting three from five, 12345: $$123, 124, 125, 134, 135, 145, 234, 235, 245, \text{ and } 345$$ In combinatorics, we use the notation, $\displaystyle \binom 5 3 = 10$.

In general, $\displaystyle \binom n r = \dfrac{n!}{r!(n-r)!}$, where $r \le n$, $n! = n \times (n-1) \times ... \times 3 \times 2 \times 1$, and $0! = 1$.

It is not until $n = 23$, that a value exceeds one-million: $\displaystyle \binom {23} {10} = 1144066$.

How many, not necessarily distinct, values of $\displaystyle \binom n r$ for $1 \le n \le 100$, are greater than one-million?

Problem 53: Combinatoric Selections

Mathematical Development

Formal Development

Definition 1 (Binomial Coefficient). For non-negative integers nrn \geq r,

(nr)=n!r!(nr)!.\binom{n}{r} = \frac{n!}{r!(n-r)!}.

Theorem 1 (Unimodality). For fixed n1n \geq 1, the sequence (n0),(n1),,(nn)\binom{n}{0}, \binom{n}{1}, \ldots, \binom{n}{n} is strictly unimodal: it strictly increases for 0r<n/20 \leq r < n/2 and strictly decreases for n/2<rnn/2 < r \leq n, achieving its maximum at r=n/2r = \lfloor n/2 \rfloor (and r=n/2r = \lceil n/2 \rceil when nn is even).

Proof. Consider the ratio of consecutive terms:

(nr)(nr1)=nr+1r.\frac{\binom{n}{r}}{\binom{n}{r-1}} = \frac{n - r + 1}{r}.

This ratio satisfies:

  • nr+1r>1    nr+1>r    r<n+12\frac{n - r + 1}{r} > 1 \iff n - r + 1 > r \iff r < \frac{n+1}{2},
  • nr+1r=1    r=n+12\frac{n - r + 1}{r} = 1 \iff r = \frac{n+1}{2} (only when nn is odd),
  • nr+1r<1    r>n+12\frac{n - r + 1}{r} < 1 \iff r > \frac{n+1}{2}.

Hence (nr)>(nr1)\binom{n}{r} > \binom{n}{r-1} for r<(n+1)/2r < (n+1)/2 and (nr)<(nr1)\binom{n}{r} < \binom{n}{r-1} for r>(n+1)/2r > (n+1)/2, establishing strict unimodality. \blacksquare

Theorem 2 (Symmetry). For all 0rn0 \leq r \leq n,

(nr)=(nnr).\binom{n}{r} = \binom{n}{n - r}.

Proof. Direct from the definition: (nnr)=n!(nr)!r!=(nr)\binom{n}{n-r} = \frac{n!}{(n-r)!\,r!} = \binom{n}{r}. \blacksquare

Corollary 1 (Contiguous Exceedance). For any threshold T>0T > 0 and fixed nn, the set

ET(n)={r{0,1,,n}:(nr)>T}E_T(n) = \{r \in \{0, 1, \ldots, n\} : \binom{n}{r} > T\}

is either empty or a contiguous interval {rmin,rmin+1,,nrmin}\{r_{\min}, r_{\min}+1, \ldots, n - r_{\min}\} with ET(n)=n2rmin+1|E_T(n)| = n - 2r_{\min} + 1.

Proof. By Theorem 1 (unimodality), (nr)>T\binom{n}{r} > T defines a connected sublevel set of a unimodal function. By Theorem 2 (symmetry), if r0ET(n)r_0 \in E_T(n), then nr0ET(n)n - r_0 \in E_T(n). The exceedance set is therefore symmetric about n/2n/2 and contiguous. If rminr_{\min} is the smallest element, the largest is nrminn - r_{\min}, giving cardinality n2rmin+1n - 2r_{\min} + 1. \blacksquare

Lemma 1 (Pascal’s Recurrence). For 1rn11 \leq r \leq n - 1:

(nr)=(n1r1)+(n1r).\binom{n}{r} = \binom{n-1}{r-1} + \binom{n-1}{r}.

Proof. Combinatorial argument: among subsets of {1,,n}\{1, \ldots, n\} of size rr, those containing element nn number (n1r1)\binom{n-1}{r-1}, and those not containing nn number (n1r)\binom{n-1}{r}. \blacksquare

Lemma 2 (Overflow Avoidance via Capping). Define C^(n,r)=min((nr),T+1)\hat{C}(n, r) = \min\bigl(\binom{n}{r},\, T + 1\bigr) where T=106T = 10^6. Then C^\hat{C} can be computed using Pascal’s recurrence with capped values:

C^(n,r)=min(C^(n1,r1)+C^(n1,r),T+1),\hat{C}(n, r) = \min\bigl(\hat{C}(n-1, r-1) + \hat{C}(n-1, r),\, T + 1\bigr),

and (nr)>T\binom{n}{r} > T if and only if C^(n,r)=T+1\hat{C}(n, r) = T + 1.

Proof. We proceed by induction on nn. Base case: C^(0,0)=1=(00)\hat{C}(0, 0) = 1 = \binom{0}{0}. Inductive step: if C^(n1,r1)\hat{C}(n-1, r-1) and C^(n1,r)\hat{C}(n-1, r) correctly represent their respective binomial coefficients capped at T+1T+1, then:

  • If (n1r1)+(n1r)T\binom{n-1}{r-1} + \binom{n-1}{r} \leq T: both summands are below T+1T + 1, so C^(n1,r1)+C^(n1,r)=(nr)T\hat{C}(n-1,r-1) + \hat{C}(n-1,r) = \binom{n}{r} \leq T, and C^(n,r)=(nr)\hat{C}(n,r) = \binom{n}{r}.
  • If (n1r1)+(n1r)>T\binom{n-1}{r-1} + \binom{n-1}{r} > T: then either some summand equals T+1T+1 (so the sum T+1\geq T + 1) or both are exact but sum to >T> T. Either way, min(sum,T+1)=T+1\min(\text{sum}, T+1) = T+1.

In both cases, C^(n,r)>T    (nr)>T\hat{C}(n,r) > T \iff \binom{n}{r} > T. \blacksquare

Theorem 3 (Counting Formula). The total count of values (nr)>106\binom{n}{r} > 10^6 for 1n1001 \leq n \leq 100 is

n=1100E106(n)=n=1E106(n)100(n2rmin(n)+1).\sum_{n=1}^{100} |E_{10^6}(n)| = \sum_{\substack{n=1 \\ E_{10^6}(n) \neq \emptyset}}^{100} (n - 2r_{\min}(n) + 1).

Editorial

We examine every pair (n,r)(n,r) with 1n1001 \leq n \leq 100 and 0rn0 \leq r \leq n, determine the corresponding binomial coefficient, and count those exceeding one million. A practical implementation can either evaluate the coefficients directly or generate them row by row using Pascal’s recurrence with capping to avoid large intermediate values; both approaches enumerate exactly the same set of coefficients and apply the same threshold test.

Pseudocode

Algorithm: Counting Large Binomial Coefficients
Require: The range 1 ≤ n ≤ 100 and the threshold 10^6.
Ensure: The number of binomial coefficients C(n, r) that exceed 10^6.
1: Initialize count ← 0.
2: For each n in {1, 2, ..., 100} do:
3:     For each r in {0, 1, ..., n}, compute C(n, r) and test whether it exceeds 10^6.
4:     If the test succeeds, increment count.
5: Return count.

Complexity Analysis

Proposition 1 (Time). The algorithm computes Pascal’s triangle row by row. Row nn has n+1n + 1 entries, each computed in O(1)O(1) (values are capped at T+1T + 1, fitting in a machine word). Total operations:

n=1N(n+1)=N(N+1)2+N=N2+3N2=O(N2).\sum_{n=1}^{N}(n + 1) = \frac{N(N+1)}{2} + N = \frac{N^2 + 3N}{2} = O(N^2).

For N=100N = 100: 51505150 operations. \blacksquare

Proposition 2 (Space). O(N)O(N) for storing one row of Pascal’s triangle (at most N+1=101N + 1 = 101 entries).

Answer

4075\boxed{4075}

Code

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

C++ project_euler/problem_053/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    const int LIMIT = 1000000;
    int count = 0;

    // Pascal's triangle with capping to prevent overflow
    vector<long long> row(102, 0);
    row[0] = 1;

    for (int n = 1; n <= 100; n++) {
        for (int r = n; r >= 1; r--) {
            row[r] = row[r] + row[r - 1];
            if (row[r] > LIMIT) row[r] = LIMIT + 1;
        }
        for (int r = 0; r <= n; r++) {
            if (row[r] > LIMIT) count++;
        }
    }

    cout << count << endl;
    return 0;
}