Project Euler Problem 101

If we are presented with the first k terms of a sequence it is impossible to say with certainty the value of the next te

Project Euler Problem 101

Solution

Answer: 37076114526

Let

$$u_n = 1-n+n^2-n^3+\cdots-n^9+n^{10}.$$

We are asked to compute the sum of the FITs (“first incorrect terms”) obtained from the optimum polynomial (OP) of degree $k-1$ fitting the first $k$ terms of the sequence, for $k=1,\dots,10$.


Step 1 — Understanding the mathematics

The sequence is generated by the degree-10 polynomial

$$u(n)=\sum_{i=0}^{10}(-1)^i n^i.$$

We are given the first $k$ values

$$u(1),u(2),\dots,u(k),$$

and construct the unique polynomial $P_k(n)$ of degree at most $k-1$ interpolating them.

This polynomial is called the “optimum polynomial”:

$$\operatorname{OP}(k,n)=P_k(n).$$

Because the true generating function has degree $10$:

  • for $k<11$, $P_k(n)\neq u(n)$,
  • therefore the first mismatch occurs at $n=k+1$,
  • the FIT is

$$P_k(k+1).$$

We must compute

$$\sum_{k=1}^{10} P_k(k+1).$$


Step 2 — Compute the sequence values

First evaluate $u(n)$ for $n=1,\dots,11$.

A useful simplification is the finite geometric-series identity:

$$1-n+n^2-\cdots+n^{10} = \frac{1+n^{11}}{1+n}.$$

Indeed,

$$\sum_{i=0}^{10}(-n)^i = \frac{1-(-n)^{11}}{1-(-n)} = \frac{1+n^{11}}{1+n}.$$

So:

$$u(n)=\frac{n^{11}+1}{n+1}.$$

The first values are:

$$\begin{aligned} u(1)&=1\ u(2)&=683\ u(3)&=44287\ u(4)&=838861\ u(5)&=8138021\ u(6)&=51828151\ u(7)&=247165843\ u(8)&=954437177\ u(9)&=3138105961\ u(10)&=9090909091. \end{aligned}$$


Step 3 — Interpolation idea

For each $k$, we interpolate a polynomial of degree $k-1$ through the first $k$ points:

$$(1,u_1),\dots,(k,u_k).$$

The cleanest formula is Lagrange interpolation:

$$P_k(x)= \sum_{i=1}^{k} u_i \prod_{\substack{1\le j\le k\j\ne i}} \frac{x-j}{i-j}.$$

Then the FIT is

$$P_k(k+1).$$

We sum these for $k=1,\dots,10$.


Step 4 — Python implementation

from fractions import Fraction

# True generating function
def u(n):
    return sum(((-1) ** i) * (n ** i) for i in range(11))

# Lagrange interpolation evaluated at x
def interpolate(xs, ys, x):
    total = Fraction(0, 1)

    for i in range(len(xs)):
        term = Fraction(ys[i], 1)

        for j in range(len(xs)):
            if i != j:
                term *= Fraction(x - xs[j], xs[i] - xs[j])

        total += term

    return total

answer = 0

# Compute FITs for k = 1..10
for k in range(1, 11):

    xs = list(range(1, k + 1))
    ys = [u(n) for n in xs]

    # First incorrect term occurs at n = k+1
    fit = interpolate(xs, ys, k + 1)

    answer += fit

print(answer)

Step 5 — Code walkthrough

u(n)

def u(n):
    return sum(((-1) ** i) * (n ** i) for i in range(11))

Computes

$$1-n+n^2-\cdots+n^{10}.$$


interpolate(xs, ys, x)

Uses exact rational arithmetic (Fraction) to avoid floating-point errors.

For each interpolation basis polynomial:

term *= Fraction(x - xs[j], xs[i] - xs[j])

this constructs

$$\prod_{j\ne i}\frac{x-x_j}{x_i-x_j}.$$

Summing all basis terms gives the unique interpolating polynomial evaluated at $x$.


Main loop

For each $k$:

xs = [1,2,...,k]
ys = [u(1),...,u(k)]

construct the OP polynomial through the first $k$ points.

Then:

fit = interpolate(xs, ys, k + 1)

computes the first incorrect term.

Finally we sum all FITs.


Step 6 — Check against the cubic example

For the example sequence $u_n=n^3$:

  • $k=1$: FIT = $1$
  • $k=2$: FIT = $15$
  • $k=3$: FIT = $58$

Sum:

$$1+15+58=74,$$

matching the problem statement.

So the interpolation method is correct.


Step 7 — Final computation

The FITs produced are:

$$\begin{aligned} 1,&\ 1365,&\ 130813,&\ 3092453,&\ 32740951,&\ 205015603,&\ 898165577,&\ 3093310441,&\ 9071313571,&\ 23772343751. \end{aligned}$$

Their sum is

$$37076114526.$$

This is positive and of the expected magnitude: the sequence values themselves are around $10^{10}$, and we accumulate ten such terms.


Answer: 37076114526