All Euler problems
Project Euler

Fibonacci Words

For any two strings of digits A and B, define the Fibonacci word sequence: F(1) = A F(2) = B F(n) = F(n-2) F(n-1) for n > 2 (concatenation) Let D(k) denote the k -th digit of the first term in the...

Source sync Apr 19, 2026
Problem #0230
Level Level 08
Solved By 3,238
Languages C++, Python
Answer 850481152593119296
Length 297 words
sequencerecursionbrute_force

Problem Statement

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

For any two strings of digits, $A$ and $B$, we define $F_{A, B}$ to be the sequence

$(A,B,AB,BAB,ABBAB,\dots)$ in which each term is the concatenation of the previous two.

Further, we define $D_{A, B}(n)$ to be the $n^{th}$ digit in the first term of $F_{A, B}$ that contains at least $n$ digits.

Example:

Let $A=1415926535$, $B=8979323846$. We wish to find $D_{A, B}(35)$, say.

The first few terms of $F_{A, B}$ are:

$1415926535$

$8979323846$

$14159265358979323846$

$897932384614159265358979323846$

$1415926535897932384689793238461415{\color{red}\mathbf 9}265358979323846$

Then $D_{A, B}(35)$ is the $35^{th}$ digit in the fifth term, which is $9$.

Now we use for $A$ the first $100$ digits of $\pi$ behind the decimal point:

$14159265358979323846264338327950288419716939937510$

$58209749445923078164062862089986280348253421170679$

and for $B$ the next hundred digits:

$82148086513282306647093844609550582231725359408128$

$48111745028410270193852110555964462294895493038196$.

Find $\sum_{n = 0}^{17} 10^n \times D_{A,B}((127+19n) \times 7^n)$.

Problem 230: Fibonacci Words

Mathematical Foundation

Theorem 1 (Length Recurrence). The lengths L(n)=F(n)L(n) = |F(n)| satisfy L(1)=L(2)=100L(1) = L(2) = 100 and L(n)=L(n1)+L(n2)L(n) = L(n-1) + L(n-2) for n3n \geq 3.

Proof. F(n)=F(n2)F(n1)F(n) = F(n-2) \cdot F(n-1) is the concatenation of F(n2)F(n-2) and F(n1)F(n-1), so F(n)=F(n2)+F(n1)|F(n)| = |F(n-2)| + |F(n-1)|. \square

Theorem 2 (Exponential Growth). L(n)=Θ(ϕn)L(n) = \Theta(\phi^n) where ϕ=(1+5)/2\phi = (1 + \sqrt{5})/2 is the golden ratio. Specifically,

L(n)=100ϕ+1(ϕn1+(ϕ)(n1))+100ϕ+1(ϕn2+(ϕ)(n2)).L(n) = \frac{100}{\phi + 1}\bigl(\phi^{n-1} + (-\phi)^{-(n-1)}\bigr) + \frac{100}{\phi + 1}\bigl(\phi^{n-2} + (-\phi)^{-(n-2)}\bigr).

Proof. The recurrence L(n)=L(n1)+L(n2)L(n) = L(n-1) + L(n-2) with L(1)=L(2)=100L(1) = L(2) = 100 has characteristic equation x2=x+1x^2 = x + 1 with roots ϕ\phi and 1/ϕ-1/\phi. The closed form follows from solving the initial conditions. Asymptotically, the (1/ϕ)n(-1/\phi)^n terms vanish, giving L(n)=Θ(ϕn)L(n) = \Theta(\phi^n). \square

Lemma 1 (Sufficient Depth). The largest position queried is k17=(127+1917)717=4507171.05×1017k_{17} = (127 + 19 \cdot 17) \cdot 7^{17} = 450 \cdot 7^{17} \approx 1.05 \times 10^{17}. We need L(n)k17L(n) \geq k_{17}, which is satisfied for n85n \approx 85.

Proof. L(n)100ϕn2/(ϕ+1)L(n) \approx 100 \cdot \phi^{n-2} / (\phi + 1). Solving L(n)1017L(n) \geq 10^{17} gives n2+logϕ(1017(ϕ+1)/100)85n \geq \lceil 2 + \log_\phi(10^{17} \cdot (\phi+1)/100) \rceil \approx 85. \square

Theorem 3 (Recursive Digit Lookup). To find the kk-th digit of F(n)F(n) where nn is the smallest index with L(n)kL(n) \geq k:

  • If n=1n = 1: return A[k]A[k].
  • If n=2n = 2: return B[k]B[k].
  • If kL(n2)k \leq L(n-2): the digit is in F(n2)F(n-2); recurse with (n2,k)(n-2, k).
  • If k>L(n2)k > L(n-2): the digit is in F(n1)F(n-1); recurse with (n1,kL(n2))(n-1, k - L(n-2)).

Proof. Since F(n)=F(n2)F(n1)F(n) = F(n-2) \cdot F(n-1), the first L(n2)L(n-2) characters are from F(n2)F(n-2) and the remaining L(n1)L(n-1) characters are from F(n1)F(n-1). The recursion correctly decomposes the position. Termination is guaranteed since at each step we reduce nn by at least 1 (and kk stays bounded by L(n)L(n), which decreases exponentially). \square

Lemma 2 (Lookup Complexity). Each digit lookup performs O(n)=O(logϕk)O(n) = O(\log_\phi k) recursive steps.

Proof. At each step, nn decreases by 1 or 2. Since we start at nlogϕkn \approx \log_\phi k and reach n{1,2}n \in \{1, 2\}, the number of steps is O(logϕk)O(\log_\phi k). \square

Editorial

F(1) = A (100 digits of pi), F(2) = B (next 100 digits of pi) F(n) = F(n-2) . F(n-1) (concatenation, older part first) Sequence: A, B, AB, BAB, ABBAB, … Find sum of D((127+19n)*7^n) * 10^n for n = 0 to 17, where D(k) is the k-th digit of the infinite Fibonacci word. We precompute Fibonacci lengths. We then find smallest n with L[n] >= k. Finally, recurse.

Pseudocode

Precompute Fibonacci lengths
Find smallest n with L[n] >= k
Recurse
else

Complexity Analysis

  • Time: O(18logϕkmax)=O(18×85)=O(1530)O(18 \cdot \log_\phi k_{\max}) = O(18 \times 85) = O(1530). Essentially instantaneous.
  • Space: O(logϕkmax)=O(85)O(\log_\phi k_{\max}) = O(85) for the precomputed lengths.

Answer

850481152593119296\boxed{850481152593119296}

Code

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

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

int main() {
    // Problem 230: Fibonacci Words
    // F(1) = A (100 digits of pi), F(2) = B (next 100 digits of pi)
    // F(n) = F(n-2) . F(n-1) (concatenation, older first)
    // Find sum of D((127+19n)*7^n) * 10^n for n=0..17

    string A = "1415926535897932384626433832795028841971"
               "6939937510582097494459230781640628620899"
               "86280348253421170679";
    string B = "8214808651328230664709384460955058223172"
               "5359408128481117450284102701938521105559"
               "64462294895493038196";

    // Use __int128 for large positions
    typedef __int128 lll;
    const int MAXN = 200;
    lll L[MAXN + 1];
    L[1] = L[2] = 100;
    lll cap = (lll)1e25;
    for (int i = 3; i <= MAXN; i++) {
        L[i] = L[i-1] + L[i-2];
        if (L[i] > cap) {
            for (int j = i + 1; j <= MAXN; j++) L[j] = cap;
            break;
        }
    }

    // Compute 7^n using __int128
    auto power7 = [](int n) -> lll {
        lll result = 1;
        for (int i = 0; i < n; i++) result *= 7;
        return result;
    };

    // Find the k-th digit (1-indexed) of the Fibonacci word
    // F(n) = F(n-2) . F(n-1): first L[n-2] chars from F(n-2), rest from F(n-1)
    auto find_digit = [&](lll k) -> int {
        int n = 1;
        while (L[n] < k) n++;
        while (n > 2) {
            if (k <= L[n-2]) {
                n -= 2;
            } else {
                k -= L[n-2];
                n -= 1;
            }
        }
        if (n == 1) return A[(int)(k - 1)] - '0';
        else return B[(int)(k - 1)] - '0';
    };

    // Compute the answer
    long long answer = 0;
    long long pow10 = 1;
    for (int n = 0; n <= 17; n++) {
        lll k = (lll)(127 + 19 * n) * power7(n);
        int digit = find_digit(k);
        answer += pow10 * digit;
        pow10 *= 10;
    }

    printf("%lld\n", answer);
    return 0;
}