// you’re reading...
1 Star2 Stars3 Stars4 Stars5 Stars (7 votes, average: 5.00 out of 5)
Loading...

Project Euler Solutions

Project Euler 101 Solution

Project Euler 101 Solution

Project Euler 101: Investigate the optimum polynomial function to model the first k terms of a given sequence.


Problem Description

If we are presented with the first k terms of a sequence it is impossible to say with certainty the value of the next term, as there are infinitely many polynomial functions that can model the sequence.

As an example, let us consider the sequence of cube numbers. This is defined by the generating function,
un = n3: 1, 8, 27, 64, 125, 216, …

Suppose we were only given the first two terms of this sequence. Working on the principle that "simple is best" we should assume a linear relationship and predict the next term to be 15 (common difference 7). Even if we were presented with the first three terms, by the same principle of simplicity, a quadratic relationship should be assumed.

We shall define OP(k, n) to be the nth term of the optimum polynomial generating function for the first k terms of a sequence. It should be clear that OP(k, n) will accurately generate the terms of the sequence for nk, and potentially the first incorrect term (FIT) will be OP(k, k+1); in which case we shall call it a bad OP (BOP).

As a basis, if we were only given the first term of sequence, it would be most sensible to assume constancy; that is, for n ≥ 2, OP(1, n) = u1.

Hence we obtain the following OPs for the cubic sequence:

OP(1, n) = 1 1, 1, 1, 1, …
OP(2, n) = 7n−6 1, 8, 15, …
OP(3, n) = 6n2−11n+6      1, 8, 27, 58, …
OP(4, n) = n3 1, 8, 27, 64, 125, …

Clearly no BOPs exist for k ≥ 4.

By considering the sum of FITs generated by the BOPs (indicated in red above), we obtain 1 + 15 + 58 = 74.

Consider the following tenth degree polynomial generating function:

un = 1 − n + n2n3 + n4n5 + n6n7 + n8n9 + n10

Find the sum of FITs for the BOPs.

Analysis

I used the difference method to solve this one as we only need to sum the FITs and not extend the series or find equations through fitting.

Take the example and build a pyramidal array with the base of k terms of the original expression. Next, take the differences between left-to-right neighbors and stack those differences on top of the base until the triangle is complete.

Now, sum the entire array to calculate the sum of all FITs. The individual FITs are summed along the diagonal layers of the triangle:
12 + 19 + 27 = 58, 7 + 8 = 15, 1

OP = n3, k = 3

   12
  7  19
1   8  27

12 + 7 + 19 + 1 + 8 + 27 = 74

Project Euler 101 Solution

Runs < 0.001 seconds in Python 2.7.
download arrowUse this link to get the Project Euler 101 Solution Python 2.7 source.

Afterthoughts

  • Difficult to read recursive approach:
k, u = 3, lambda x: x**3
def tld(x):
    if len(x[-1]) == 1: return x
    return x + tld([[x[-1][i] - x[-1][i-1] for i in range(1, len(x[-1]))]])

print "Sum of FITs for the BOPs", sum(sum(tld([map(u, range(1, k+1))]), []))
Project Euler 101 Solution last updated

Discussion

No comments yet.

Post a comment