All Euler problems
Project Euler

Squarefree Binomial Coefficients

Find the sum of all distinct squarefree numbers in the first 51 rows (rows 0 through 50) of Pascal's triangle.

Source sync May 21, 2026
Problem #0203
Level Level 04
Solved By 10,140
Languages C++, Python
Answer 34029210557338
Length 300 words
combinatoricsgeometrymodular_arithmetic

Problem Statement

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

The binomial coefficients \(\displaystyle \binom n k\) can be arranged in triangular form, Pascal’s triangle, like this:

\[ \begin {array}{cccccccccccccccc} &&&&&&& 1 &&&&&&& \\ &&&&&& 1 && 1 &&&&&& \\ &&&&& 1 && 2 && 1 &&&&& \\ &&&& 1 && 3 && 3 && 1 &&&& \\ &&& 1 && 4 && 6 && 4 && 1 &&& \\ && 1 && 5 && 10 && 10 && 5 && 1 && \\ & 1 && 6 && 15 && 20 && 15 && 6 && 1 & \\ 1 && 7 && 21 && 35 && 35 && 21 && 7 && 1 \\ &&&&&&& \ldots &&&&&&& \\ \end {array} \]

It can be seen that the first eight rows of Pascal’s triangle contain twelve distinct numbers: \(1\), \(2\), \(3\), \(4\), \(5\), \(6\), \(7\), \(10\), \(15\), \(20\), \(21\) and \(35\).

A positive integer \(n\) is called squarefree if no square of a prime divides \(n\). Of the twelve distinct numbers in the first eight rows of Pascal’s triangle, all except \(4\) and \(20\) are squarefree. The sum of the distinct squarefree numbers in the first eight rows is 105.

Find the sum of the distinct squarefree numbers in the first 51 rows of Pascal’s triangle.

Problem 203: Squarefree Binomial Coefficients

Mathematical Development

Theorem (Kummer, 1852). For a prime pp and non-negative integers m,nm, n, the pp-adic valuation vp(m+nm)v_p\binom{m+n}{m} equals the number of carries when adding mm and nn in base pp.

Proof. By Legendre’s formula,

vp(k!)=∑i=1∞⌊kpi⌋.v_p(k!) = \sum_{i=1}^{\infty} \left\lfloor \frac{k}{p^i} \right\rfloor.

Therefore

vp(m+nm)=vp((m+n)!)−vp(m!)−vp(n!)=∑i=1∞(⌊m+npi⌋−⌊mpi⌋−⌊npi⌋).v_p\binom{m+n}{m} = v_p((m+n)!) - v_p(m!) - v_p(n!) = \sum_{i=1}^{\infty}\left(\left\lfloor\frac{m+n}{p^i}\right\rfloor - \left\lfloor\frac{m}{p^i}\right\rfloor - \left\lfloor\frac{n}{p^i}\right\rfloor\right).

Each summand equals 0 or 1, and it equals 1 precisely when there is a carry out of position i−1i-1 in the base-pp addition of mm and nn. □\square

Lemma (Bounded Valuation for Large Primes). For n≤50n \leq 50 and any prime p≥11p \geq 11, every binomial coefficient (nk)\binom{n}{k} satisfies vp(nk)≤1v_p\binom{n}{k} \leq 1.

Proof. Since p≥11p \geq 11 and n≤50<p2=121n \leq 50 < p^2 = 121, both kk and n−kn-k have at most two digits in base pp. Adding two such numbers produces at most one carry. By Kummer’s theorem, vp(nk)≤1v_p\binom{n}{k} \leq 1. □\square

Theorem (Squarefreeness Criterion). A binomial coefficient (nk)\binom{n}{k} with n≤50n \leq 50 is squarefree if and only if (nk)\binom{n}{k} is not divisible by any of 44, 99, 2525, or 4949.

Proof. A positive integer is squarefree if and only if vp≤1v_p \leq 1 for every prime pp. By the lemma, this automatically holds for all primes p≥11p \geq 11. Thus squarefreeness can fail only for p∈{2,3,5,7}p \in \{2,3,5,7\}, and that is equivalent to divisibility by p2∈{4,9,25,49}p^2 \in \{4,9,25,49\}. □\square

Editorial

The heavy number theory reduces the checking step to something very small. Kummer’s theorem shows that in rows 00 through 5050, any prime at least 11 can appear with exponent at most 1 in a binomial coefficient, so repeated prime factors can only come from 22, 33, 55, or 77.

That means we can generate every distinct binomial coefficient in the first 51 rows of Pascal’s triangle, store them in a set, and test squarefreeness with only four divisibility checks: by 44, 99, 2525, and 4949. The values that survive are exactly the squarefree ones, and summing them finishes the problem.

Pseudocode

Set N = 50.
Create an empty set values.
Start with row = [1].
Insert 1 into values.

For n from 1 to N:
    Build the next Pascal row from the previous one:
        begin with 1,
        fill each interior entry by adding the two entries above it,
        end with 1.
    Insert every entry of the new row into values.

answer = 0
For each v in values:
    If v is not divisible by 4, 9, 25, or 49:
        answer += v

Return answer

Complexity Analysis

  • Time: O(N2)O(N^2) to generate the first 51 rows of Pascal’s triangle, plus constant work per distinct value for the four square divisibility checks.
  • Space: O(∣S∣)O(|S|) for the set of distinct binomial coefficients.

Answer

34029210557338\boxed{34029210557338}

Code

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

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

int main(){
    // Find sum of distinct squarefree binomial coefficients in rows 0..50.
    // By Kummer's theorem, for n<=50, only primes 2,3,5,7 can appear with
    // exponent >= 2. So check divisibility by 4, 9, 25, 49.

    const int N = 50;
    set<long long> vals;

    // Generate Pascal's triangle
    vector<vector<long long>> C(N+1);
    for(int n = 0; n <= N; n++){
        C[n].resize(n+1);
        C[n][0] = C[n][n] = 1;
        for(int k = 1; k < n; k++){
            C[n][k] = C[n-1][k-1] + C[n-1][k];
        }
        for(int k = 0; k <= n; k++){
            vals.insert(C[n][k]);
        }
    }

    long long ans = 0;
    for(long long v : vals){
        if(v % 4 != 0 && v % 9 != 0 && v % 25 != 0 && v % 49 != 0){
            ans += v;
        }
    }

    cout << ans << endl;
    return 0;
}