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 Apr 19, 2026
Problem #0203
Level Level 04
Solved By 10,140
Languages C++, Python
Answer 34029210557338
Length 258 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 Foundation

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=1k/piv_p(k!) = \sum_{i=1}^{\infty} \lfloor k/p^i \rfloor. Therefore:

vp(m+nm)=vp((m+n)!)vp(m!)vp(n!)=i=1(m+npimpinpi).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 term (m+n)/pim/pin/pi\lfloor(m+n)/p^i\rfloor - \lfloor m/p^i\rfloor - \lfloor n/p^i\rfloor equals 0 or 1, and it equals 1 precisely when there is a carry out of position i1i-1 in the base-pp addition of mm and nn. This is a standard result; see Granville (1997) for a detailed proof. \square

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

Proof. Since p11p \geq 11 and n50<p2=121n \leq 50 < p^2 = 121, both kk and nkn - k have at most 2 digits in base pp. Adding two numbers with at most 2 digits produces at most 1 carry (from the units digit to the pp‘s digit). 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 n50n \leq 50 is squarefree if and only if (nk)\binom{n}{k} is not divisible by any of 4,9,25,494, 9, 25, 49.

Proof. A positive integer is squarefree iff vp1v_p \leq 1 for every prime pp. By the Lemma, vp(nk)1v_p\binom{n}{k} \leq 1 automatically holds for all primes p11p \geq 11. Thus squarefreeness fails only if vp(nk)2v_p\binom{n}{k} \geq 2 for some prime p{2,3,5,7}p \in \{2, 3, 5, 7\}, which is equivalent to p2(nk)p^2 \mid \binom{n}{k} for p{2,3,5,7}p \in \{2, 3, 5, 7\}. \square

Editorial

By Kummer’s theorem, for n <= 50, primes >= 11 can appear at most once in any binomial coefficient. So squarefreeness reduces to checking divisibility by 4, 9, 25, and 49. We return sum(S).

Pseudocode

Input: N = 50
Output: sum of distinct squarefree values in rows 0..N of Pascal's triangle
S = empty set
For n = 0 to N:
Return sum(S)

Complexity Analysis

  • Time: O(N2)O(N^2) to generate all binomial coefficients. Each squarefreeness test is O(1)O(1) (four divisibility checks). Set insertion is O(logS)O(\log|S|) amortized, and S(N+12)=1326|S| \leq \binom{N+1}{2} = 1326.
  • Space: O(S)O(|S|) for the set of distinct squarefree values.

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;
}