Project Euler Problem 203

The binomial coefficients displaystyle binom n k can be arranged in triangular form, Pascal's triangle, like this: | | |

Project Euler Problem 203

Solution

Answer: 34029210557338

The key task is to compute all distinct squarefree binomial coefficients appearing in rows $0$ through $50$ of Pascal’s triangle, then sum them.

A number is squarefree if no prime square divides it.

Examples:

  • $6 = 2 \cdot 3$ is squarefree.
  • $12 = 2^2 \cdot 3$ is not.
  • $20 = 2^2 \cdot 5$ is not.

The problem explicitly provides a working brute-force program. We will analyze exactly why it works and what result it produces.


Mathematical Analysis

Pascal’s triangle contains entries

$$\binom{n}{k} = \frac{n!}{k!(n-k)!}$$

for rows $n = 0,1,2,\dots$.

We need:

  1. Every distinct value appearing among

$$\binom{n}{k}, \quad 0 \le n \le 50$$ 2. Keep only those that are squarefree. 3. Sum them.


Squarefree Test

A number $m$ is squarefree iff there is no integer $d \ge 2$ such that

$$d^2 \mid m$$

The supplied function checks this directly:

def is_squarefree(n):
    if n <= 1: return n == 1
    d = 2
    while d * d <= n:
        if n % (d*d) == 0: return False
        d += 1
    return True

This works because:

  • if any square divides $n$,
  • then some square $d^2$ with $d \le \sqrt n$ divides it.

So checking all $d$ up to $\sqrt n$ is sufficient.


Python Implementation

The problem explicitly asks us to run this exact code:

from math import comb, isqrt

def is_squarefree(n):
    if n <= 1:
        return n == 1

    d = 2
    while d * d <= n:
        if n % (d * d) == 0:
            return False
        d += 1

    return True

seen = set()

for n in range(51):
    for k in range(n + 1):
        c = comb(n, k)

        if c not in seen and is_squarefree(c):
            seen.add(c)

print(sum(seen))

Code Walkthrough

Import

from math import comb, isqrt
  • comb(n,k) computes exact binomial coefficients.

(isqrt is imported but unused.)


Squarefree checker

def is_squarefree(n):

Checks whether $n$ has any repeated prime factor.

Base case

if n <= 1:
    return n == 1
  • $1$ is squarefree.
  • $0$ is not relevant here.

Trial squares

d = 2
while d * d <= n:

Tests all possible square divisors $d^2$.


Detect square divisibility

if n % (d*d) == 0:
    return False

If some square divides $n$, it is not squarefree.


Otherwise

return True

No square divisor exists.


Building Pascal’s Triangle Values

seen = set()

We only want distinct coefficients.


Generate rows 0..50

for n in range(51):

This includes exactly rows:

$$0,1,2,\dots,50$$


Generate entries in row $n$

for k in range(n + 1):

Computes all

$$\binom{n}{0}, \binom{n}{1}, \dots, \binom{n}{n}$$


Compute coefficient

c = comb(n, k)

Keep distinct squarefree values

if c not in seen and is_squarefree(c):
    seen.add(c)

Only unique squarefree coefficients are stored.


Verification on Small Example

The statement says the first 8 rows contain distinct values

$$1,2,3,4,5,6,7,10,15,20,21,35$$

Among these:

  • $4 = 2^2$ is not squarefree.
  • $20 = 2^2 \cdot 5$ is not squarefree.

Remaining:

$$1+2+3+5+6+7+10+15+21+35 = 105$$

which matches the problem statement.

So the algorithm behaves correctly on the sample.


Final Computation

Running the exact supplied code yields:

$$34029210557338$$

This magnitude is reasonable because Pascal coefficients up to row 50 become quite large, and many distinct squarefree values occur.

Answer: 34029210557338