Open Source · 7 Languages · 900+ Problems

Project Euler Solutions

Complete solutions in C++, Python & Java — with step-by-step mathematical explanations

All Problems

Problem 140: Modified Fibonacci Golden Nuggets

View on Project Euler

Project Euler Problem 140 Solution

EulerSolve provides an optimized solution for Project Euler Problem 140, Modified Fibonacci Golden Nuggets, with C++, Python, Java, and a step-by-step mathematical explanation.

Problem Summary Let \(G_1=1\), \(G_2=4\), and \(G_k=G_{k-1}+G_{k-2}\) for \(k\ge 3\). The generating function is $A_G(x)=\sum_{k=1}^{\infty} G_k x^k.$ A "golden nugget" is a positive integer \(n\) for which the equation \(A_G(x)=n\) has a positive rational solution \(x\). The task is to add the first 30 such integers. The crucial point is that this is not a search over rational numbers. After simplifying the generating function, the nugget condition becomes a Pell-type Diophantine equation, and the implementations generate all relevant solutions from a small set of seed pairs. Mathematical Approach The sequence begins \(1,4,5,9,14,23,\dots\), so the same Fibonacci recurrence is present, but the initial values are different. That changes the numerator of the generating function and, later, the constant term in the Pell equation. From the recurrence to a closed generating function Write \(A_G(x)=x+4x^2+\sum_{k=3}^{\infty} G_k x^k\). Using \(G_k=G_{k-1}+G_{k-2}\), $A_G(x)=x+4x^2+\sum_{k=3}^{\infty}(G_{k-1}+G_{k-2})x^k.$ Reindex the two sums: $A_G(x)=x+4x^2+x\sum_{j=2}^{\infty}G_j x^j+x^2\sum_{j=1}^{\infty}G_j x^j=x+4x^2+x(A_G(x)-x)+x^2A_G(x).$ Therefore $A_G(x)(1-x-x^2)=x+3x^2,$ so the generating function is $\boxed{A_G(x)=\frac{x+3x^2}{1-x-x^2}.}$ The rationality condition becomes a square discriminant Set \(A_G(x)=n\) with \(n\in \mathbb Z_{>0}\)....

Detailed mathematical approach

Problem Summary

Let \(G_1=1\), \(G_2=4\), and \(G_k=G_{k-1}+G_{k-2}\) for \(k\ge 3\). The generating function is

$A_G(x)=\sum_{k=1}^{\infty} G_k x^k.$

A "golden nugget" is a positive integer \(n\) for which the equation \(A_G(x)=n\) has a positive rational solution \(x\). The task is to add the first 30 such integers.

The crucial point is that this is not a search over rational numbers. After simplifying the generating function, the nugget condition becomes a Pell-type Diophantine equation, and the implementations generate all relevant solutions from a small set of seed pairs.

Mathematical Approach

The sequence begins \(1,4,5,9,14,23,\dots\), so the same Fibonacci recurrence is present, but the initial values are different. That changes the numerator of the generating function and, later, the constant term in the Pell equation.

From the recurrence to a closed generating function

Write \(A_G(x)=x+4x^2+\sum_{k=3}^{\infty} G_k x^k\). Using \(G_k=G_{k-1}+G_{k-2}\),

$A_G(x)=x+4x^2+\sum_{k=3}^{\infty}(G_{k-1}+G_{k-2})x^k.$

Reindex the two sums:

$A_G(x)=x+4x^2+x\sum_{j=2}^{\infty}G_j x^j+x^2\sum_{j=1}^{\infty}G_j x^j=x+4x^2+x(A_G(x)-x)+x^2A_G(x).$

Therefore

$A_G(x)(1-x-x^2)=x+3x^2,$

so the generating function is

$\boxed{A_G(x)=\frac{x+3x^2}{1-x-x^2}.}$

The rationality condition becomes a square discriminant

Set \(A_G(x)=n\) with \(n\in \mathbb Z_{>0}\). Then

$n(1-x-x^2)=x+3x^2,$

which rearranges to

$\boxed{(n+3)x^2+(n+1)x-n=0.}$

This quadratic has a rational root exactly when its discriminant is a perfect square:

$\Delta=(n+1)^2+4n(n+3)=5n^2+14n+1=m^2.$

Conversely, if \(m^2=5n^2+14n+1\), then

$x=\frac{-\,(n+1)+m}{2(n+3)}$

is rational, and it is positive because \(m^2-(n+1)^2=4n(n+3)>0\).

Rewriting the condition as a Pell-type equation

Multiply the discriminant identity by 5:

$25n^2+70n+5=5m^2.$

Now complete the square:

$25n^2+70n+49-44=5m^2,$

so

$\boxed{(5n+7)^2-5m^2=44.}$

If we define

$U=5n+7,\qquad V=m,$

then every nugget gives a positive solution of

$\boxed{U^2-5V^2=44,}$

and every solution with \(U>7\) and \(U-7\) divisible by 5 gives back a nugget through

$\boxed{n=\frac{U-7}{5}.}$

The invariant-preserving recurrence and the six seed branches

The unit \(9+4\sqrt5\) has norm 1, because \(9^2-5\cdot4^2=1\). Multiplying \(U+V\sqrt5\) by this unit sends one solution of \(U^2-5V^2=44\) to another:

$U'=(9U+20V),\qquad V'=(4U+9V).$

A direct expansion shows the invariant is preserved:

$U'^2-5V'^2=U^2-5V^2.$

For the positive solution set relevant here, the branches can be represented by the six minimal positive seeds

$(7,1),\ (8,2),\ (13,5),\ (17,7),\ (32,14),\ (43,19).$

Not every iterate is a nugget. The recurrence preserves the Pell norm, but it does not force \(U\equiv 7 \pmod 5\). That is why the implementations generate each branch and keep only those states with \(U>7\) and \(U-7\) divisible by 5. The first accepted nuggets are

$2,\ 5,\ 21,\ 42,\ 152,\ 296,\ 1050,\ 2037,\dots$

The seed \((7,1)\) itself corresponds to \(n=0\), so it is not a golden nugget, but its later iterates still belong to a valid branch and eventually produce positive nuggets.

Worked example: recovering \(x\) from a Pell solution

Take \((U,V)=(17,7)\). It satisfies

$17^2-5\cdot 7^2=289-245=44,$

and because \(17-7\) is divisible by 5, it yields

$n=\frac{17-7}{5}=2.$

Then the quadratic becomes

$5x^2+3x-2=0,$

whose positive rational root is

$x=\frac{-3+7}{10}=\frac25.$

So \(A_G(2/5)=2\), and 2 is indeed a golden nugget. If we apply the Pell recurrence once, we get \((293,131)\); the invariant \(U^2-5V^2=44\) still holds, but \(293-7\) is not divisible by 5, so that iterate is not accepted. This is exactly why the code keeps the recurrence and the divisibility test separate.

How the Code Works

Enumerating the Pell branches

The C++, Python, and Java implementations hard-code the six seed pairs above and iterate the recurrence

$U' = 9U+20V,\qquad V' = 4U+9V.$

Each family is advanced a fixed number of times. Whenever an iterate satisfies \(U>7\) and \(U-7\) divisible by 5, the implementation converts it to a nugget with \(n=(U-7)/5\) and inserts it into an ordered collection. Using a set is a simple way to deduplicate values and keep them sorted for the final sum.

Taking the required prefix

After all branches have been generated, the implementations read the nuggets in increasing order and add the first 30 of them. One implementation also allows a different requested count and includes a small checkpoint that the first five nuggets sum to 222, but the underlying mathematics is the same in all three languages.

The fixed iteration depth is more than enough because the branch growth factor is \(9+4\sqrt5\approx 17.944\), so the values explode exponentially. A few dozen steps per seed already produce far more than 30 valid nuggets.

Complexity Analysis

As written, the algorithm is essentially constant-time. There are 6 seed families and 50 recurrence steps per family, so fewer than 300 Pell updates are performed, followed by insertion into a small ordered set and extraction of the first 30 values.

If one abstracts away from the hard-coded bounds, the cost is linear in the number of generated Pell iterates, with an additional logarithmic factor if an ordered set is used for deduplication and sorting. For the actual problem instance, both time and memory usage are negligible.

Footnotes and References

  1. Problem page: Project Euler 140
  2. Fibonacci-type recurrences: Wikipedia - Fibonacci number
  3. Generating functions: Wikipedia - Generating function
  4. Pell-type equations: Wikipedia - Pell's equation
  5. Quadratic discriminants: Wikipedia - Quadratic equation

Mathematical approach · C++ solution · Python solution · Java solution

Previous: Problem 139 · All Project Euler solutions · Next: Problem 141

Need help with a problem? Ask me! 💡
e
✦ Euler GLM 5.2
Hi! I'm Euler. Ask me anything about Project Euler problems, math concepts, or solution approaches! 🧮