All Euler problems
Project Euler

Modified Fibonacci Golden Nuggets

Consider the modified Fibonacci-like sequence defined by G_1 = 1, G_2 = 4, G_k = G_(k-1) + G_(k-2). The generating function is: A_G(x) = xG_1 + x^2 G_2 + x^3 G_3 +... = (x + 3x^2)/(1 - x - x^2) A "...

Source sync Apr 19, 2026
Problem #0140
Level Level 06
Solved By 5,122
Languages C++, Python
Answer 5673835352990
Length 291 words
sequencemodular_arithmeticlinear_algebra

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Consider the infinite polynomial series $A_G(x) = x G_1 + x^2 G_2 + x^3 G_3 + \cdots$, where $G_k$ is the $k$th term of the second order recurrence relation $G_k = G_{k-1} + G_{k-2}$, $G_1 = 1$ and $G_2 = 4$; that is, $1, 4, 5, 9, 14, 23, \dots$.

For this problem we shall be concerned with values of $x$ for which $A_G(x)$ is a positive integer.

The corresponding values of $x$ for the first five natural numbers are shown below.

$x$$A_G(x)$
$\frac{\sqrt{5} - 1}{4}$$1$
$\frac{2}{5}$$2$
$\frac{\sqrt{22} - 2}{6}$$3$
$\frac{\sqrt{137} - 5}{14}$$4$
$\frac{1}{2}$$5$

We shall call $A_G(x)$ a golden nugget if $x$ is rational, because they become increasingly rarer; for example, the $20$th golden nugget is $211345365$.

Find the sum of the first thirty golden nuggets.

Problem 140: Modified Fibonacci Golden Nuggets

Mathematical Foundation

Theorem 1. Setting AG(x)=nA_G(x) = n yields the quadratic (n+3)x2+(n+1)xn=0(n+3)x^2 + (n+1)x - n = 0, whose discriminant is D=5n2+14n+1D = 5n^2 + 14n + 1.

Proof. From n=x+3x21xx2n = \frac{x + 3x^2}{1 - x - x^2}: n(1xx2)=x+3x2n(1 - x - x^2) = x + 3x^2, hence (n+3)x2+(n+1)xn=0(n+3)x^2 + (n+1)x - n = 0. The discriminant is:

D=(n+1)2+4n(n+3)=n2+2n+1+4n2+12n=5n2+14n+1D = (n+1)^2 + 4n(n+3) = n^2 + 2n + 1 + 4n^2 + 12n = 5n^2 + 14n + 1

\square

Theorem 2. The condition 5n2+14n+1=k25n^2 + 14n + 1 = k^2 (for nn to be a golden nugget) reduces to the generalized Pell equation a25k2=44a^2 - 5k^2 = 44, where a=5n+7a = 5n + 7.

Proof. Multiplying 5n2+14n+1=k25n^2 + 14n + 1 = k^2 by 5:

25n2+70n+5=5k225n^2 + 70n + 5 = 5k^2 (5n+7)249+5=5k2(5n + 7)^2 - 49 + 5 = 5k^2 (5n+7)25k2=44(5n + 7)^2 - 5k^2 = 44

Setting a=5n+7a = 5n + 7 gives a25k2=44a^2 - 5k^2 = 44. For nn to be a positive integer, we need a>7a > 7 and a2(mod5)a \equiv 2 \pmod{5} (since 5n+72(mod5)5n + 7 \equiv 2 \pmod{5}). \square

Lemma 1. The fundamental solutions of a25b2=44a^2 - 5b^2 = 44 (with a,b>0a, b > 0) are:

(a,b){(7,1),(8,2),(13,5),(17,7)}(a, b) \in \{(7, 1), (8, 2), (13, 5), (17, 7)\}

Proof. We search for a>0a > 0 with a244(mod5)a^2 \equiv 44 \pmod{5}, i.e., a24(mod5)a^2 \equiv 4 \pmod{5}, so a±2(mod5)a \equiv \pm 2 \pmod{5}. Testing small values:

  • a=2a = 2: 45b2=44b2=84 - 5b^2 = 44 \Rightarrow b^2 = -8. No.
  • a=3a = 3: 95b2=44b2=79 - 5b^2 = 44 \Rightarrow b^2 = -7. No.
  • a=7a = 7: 495b2=44b2=1b=149 - 5b^2 = 44 \Rightarrow b^2 = 1 \Rightarrow b = 1. Yes.
  • a=8a = 8: 645b2=44b2=4b=264 - 5b^2 = 44 \Rightarrow b^2 = 4 \Rightarrow b = 2. Yes.
  • a=12a = 12: 1445b2=44b2=20144 - 5b^2 = 44 \Rightarrow b^2 = 20. No.
  • a=13a = 13: 1695b2=44b2=25b=5169 - 5b^2 = 44 \Rightarrow b^2 = 25 \Rightarrow b = 5. Yes.
  • a=17a = 17: 2895b2=44b2=49b=7289 - 5b^2 = 44 \Rightarrow b^2 = 49 \Rightarrow b = 7. Yes.

Further solutions are generated from these by the automorphism of the Pell equation. \square

Theorem 3. Each fundamental solution generates an infinite family via the recurrence using the fundamental solution (9,4)(9, 4) of u25v2=1u^2 - 5v^2 = 1:

(ak+1,bk+1)=(9ak+20bk,  4ak+9bk)(a_{k+1}, b_{k+1}) = (9a_k + 20b_k, \; 4a_k + 9b_k)

All solution chains are generated from fundamental solutions (a0,b0)(a_0, b_0) and their negatives (a0,b0)(-a_0, -b_0).

Proof. If (a,b)(a, b) solves a25b2=44a^2 - 5b^2 = 44 and (u,v)(u, v) solves u25v2=1u^2 - 5v^2 = 1, then (au+5bv,av+bu)(au + 5bv, av + bu) also solves a25b2=44a^2 - 5b^2 = 44 (by the multiplicativity of the Pell norm). Taking (u,v)=(9,4)(u, v) = (9, 4) gives the stated recurrence. Starting from negative fundamentals (a0,b0)(-a_0, -b_0) produces additional chains that eventually yield positive (a,b)(a, b) with a>7a > 7. \square

Lemma 2. The golden nuggets are obtained by filtering: from each chain, select solutions where a>7a > 7 and a2(mod5)a \equiv 2 \pmod{5}. The resulting n=(a7)/5n = (a - 7)/5.

Proof. Direct from Theorem 2. \square

Editorial

G(x) = x(1+3x)/(1-x-x^2). Find sum of first 30 golden nuggets. Reduces to generalized Pell equation a^2 - 5*b^2 = 44 where a = 5n + 7. We fundamental solutions and their negatives. We then generate solutions along this chain. Finally, advance: (a, b) -> (9a + 20b, 4a + 9b).

Pseudocode

count = 30
Fundamental solutions and their negatives
Generate solutions along this chain
Advance: (a, b) -> (9a + 20b, 4a + 9b)

Complexity Analysis

  • Time: O(CI)O(C \cdot I) where C=8C = 8 chains and I50I \approx 50 iterations per chain. Each iteration is O(1)O(1) arithmetic (with big integers, O(i)O(i) bits at step ii).
  • Space: O(count)O(\text{count}) to store and sort the nuggets.

Answer

5673835352990\boxed{5673835352990}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_140/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    // Solve a^2 - 5*b^2 = 44
    // Fundamental solutions: (7,1), (8,2), (13,5), (17,7)
    // Recurrence: (a,b) -> (9a+20b, 4a+9b)
    // Need a > 7 and a % 5 == 2, then n = (a-7)/5

    vector<pair<long long, long long>> fund = {
        {7, 1}, {8, 2}, {13, 5}, {17, 7}
    };

    set<long long> nuggets;

    // Try all sign combinations for fundamental solutions
    for (auto [a0, b0] : fund) {
        vector<pair<long long, long long>> starts = {
            {a0, b0}, {-a0, -b0}, {-a0, b0}, {a0, -b0}
        };
        for (auto [sa, sb] : starts) {
            long long a = sa, b = sb;
            for (int iter = 0; iter < 40; iter++) {
                if (a > 7 && (a - 7) % 5 == 0) {
                    long long n = (a - 7) / 5;
                    if (n > 0) nuggets.insert(n);
                }
                long long na = 9 * a + 20 * b;
                long long nb = 4 * a + 9 * b;
                a = na;
                b = nb;
            }
        }
    }

    // Sum first 30
    long long sum = 0;
    int count = 0;
    for (long long n : nuggets) {
        if (count >= 30) break;
        sum += n;
        count++;
    }

    cout << sum << endl;
    return 0;
}