All Euler problems
Project Euler

Pandigital Fibonacci Ends

The Fibonacci sequence is defined by F_1 = F_2 = 1 and F_n = F_(n-1) + F_(n-2) for n >= 3. Find the smallest index k such that both the first 9 digits and the last 9 digits of F_k are 1--9 pandigit...

Source sync Apr 19, 2026
Problem #0104
Level Level 03
Solved By 18,264
Languages C++, Python
Answer 329468
Length 252 words
number_theorymodular_arithmeticsequence

Problem Statement

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

The Fibonacci sequence is defined by the recurrence relation: \[F_n = F_{n - 1} + F_{n - 2} \text {, where } F_1 = 1 \text { and } F_2 = 1\] It turns out that \(F_{541}\), which contains \(113\) digits, is the first Fibonacci number for which the last nine digits are \(1\)-\(9\) pandigital (contain all the digits \(1\) to \(9\), but not necessarily in order). And \(F_{2749}\), which contains \(575\) digits, is the first Fibonacci number for which the first nine digits are \(1\)-\(9\) pandigital.

Given that \(F_k\) is the first Fibonacci number for which the first nine digits AND the last nine digits are \(1\)-\(9\) pandigital, find \(k\).

Problem 104: Pandigital Fibonacci Ends

Mathematical Development

Theorem 1 (Binet’s Formula). For all n1n \geq 1,

Fn=φnψn5,F_n = \frac{\varphi^n - \psi^n}{\sqrt{5}},

where φ=1+52\varphi = \frac{1 + \sqrt{5}}{2} and ψ=152\psi = \frac{1 - \sqrt{5}}{2}.

Proof. Let f(n)=(φnψn)/5f(n) = (\varphi^n - \psi^n)/\sqrt{5}. Since φ\varphi and ψ\psi are the roots of x2x1=0x^2 - x - 1 = 0, we have φ2=φ+1\varphi^2 = \varphi + 1 and ψ2=ψ+1\psi^2 = \psi + 1. Thus:

Base cases: f(1)=(φψ)/5=5/5=1f(1) = (\varphi - \psi)/\sqrt{5} = \sqrt{5}/\sqrt{5} = 1 and f(2)=(φ2ψ2)/5=(φψ)(φ+ψ)/5=51/5=1f(2) = (\varphi^2 - \psi^2)/\sqrt{5} = (\varphi - \psi)(\varphi + \psi)/\sqrt{5} = \sqrt{5} \cdot 1/\sqrt{5} = 1.

Inductive step: f(n1)+f(n2)=φn2(φ+1)ψn2(ψ+1)5=φnψn5=f(n)f(n-1) + f(n-2) = \frac{\varphi^{n-2}(\varphi + 1) - \psi^{n-2}(\psi + 1)}{\sqrt{5}} = \frac{\varphi^n - \psi^n}{\sqrt{5}} = f(n),

using φ+1=φ2\varphi + 1 = \varphi^2 and ψ+1=ψ2\psi + 1 = \psi^2. By induction, f(n)=Fnf(n) = F_n. \blacksquare

Theorem 2 (Leading Digits via Logarithms). Define Ln=nlog10φ12log105L_n = n \log_{10} \varphi - \frac{1}{2}\log_{10} 5 and let {Ln}\{L_n\} denote the fractional part of LnL_n. For nn sufficiently large, the first mm significant digits of FnF_n are 10{Ln}+m1\lfloor 10^{\{L_n\} + m - 1} \rfloor.

Proof. From Binet’s formula, Fn=φn/5ψn/5F_n = \varphi^n/\sqrt{5} - \psi^n/\sqrt{5}. Since ψ<1|\psi| < 1, we have ψn/50|\psi^n/\sqrt{5}| \to 0 exponentially, so Fn=φn/5+1/2F_n = \lfloor \varphi^n/\sqrt{5} + 1/2 \rfloor for n1n \geq 1. Therefore:

log10Fn=log10 ⁣(φn5)+εn=nlog10φ12log105+εn,\log_{10} F_n = \log_{10}\!\left(\frac{\varphi^n}{\sqrt{5}}\right) + \varepsilon_n = n\log_{10}\varphi - \tfrac{1}{2}\log_{10} 5 + \varepsilon_n,

where εn=O(ψn)|\varepsilon_n| = O(|\psi|^n). Writing Ln=Ln+{Ln}L_n = \lfloor L_n \rfloor + \{L_n\}, we obtain Fn10{Ln}10LnF_n \approx 10^{\{L_n\}} \cdot 10^{\lfloor L_n \rfloor}, so the significand is 10{Ln}10^{\{L_n\}} and the first mm digits are 10{Ln}+m1\lfloor 10^{\{L_n\} + m - 1} \rfloor.

The error εn|\varepsilon_n| satisfies εn<ψn/(5ln10)<1014|\varepsilon_n| < |\psi|^n/(\sqrt{5}\ln 10) < 10^{-14} for n>70n > 70, which is well below the precision needed for 9 digits. \blacksquare

Lemma 1 (Modular Recurrence for Trailing Digits). The last 9 digits of FnF_n equal Fnmod109F_n \bmod 10^9, which satisfies Fnmod109=(Fn1mod109+Fn2mod109)mod109F_n \bmod 10^9 = (F_{n-1} \bmod 10^9 + F_{n-2} \bmod 10^9) \bmod 10^9.

Proof. The ring homomorphism ZZ/109Z\mathbb{Z} \to \mathbb{Z}/10^9\mathbb{Z} preserves addition: (a+b)modm=((amodm)+(bmodm))modm(a + b) \bmod m = ((a \bmod m) + (b \bmod m)) \bmod m. Applying this to Fn=Fn1+Fn2F_n = F_{n-1} + F_{n-2} shows the reduced recurrence is exact. \blacksquare

Definition. A positive integer NN with exactly 9 digits is 1—9 pandigital if its decimal representation is a permutation of {1,2,3,4,5,6,7,8,9}\{1, 2, 3, 4, 5, 6, 7, 8, 9\}.

Editorial

We enumerate the admissible parameter range, discard candidates that violate the derived bounds or arithmetic constraints, and update the final set or total whenever a candidate passes the acceptance test.

Pseudocode

    Set MOD <- 10^9
    Set log_phi <- log10((1 + sqrt(5)) / 2)
    Set log_s5 <- 0.5 * log10(5)
    Set a, b <- 1, 1 // F_1, F_2 mod MOD

    for k = 3, 4, 5, ...:
        Set a, b <- b, (a + b) mod MOD
        if not IS_PANDIGITAL(b): continue // filter on last 9 digits
        Set L <- k * log_phi - log_s5
        Set f <- L - floor(L)
        Set first9 <- floor(10^(f + 8))
        if IS_PANDIGITAL(first9): return k

    Return (x has exactly 9 digits) and (digit set = {1,...,9})

Complexity Analysis

  • Time. O(k)O(k^*) where k=329,468k^* = 329{,}468. Each iteration performs O(1)O(1) work: one modular addition, one floating-point computation, and one constant-time pandigital check.
  • Space. O(1)O(1). Only two modular residues and a few floating-point variables are stored.

Answer

329468\boxed{329468}

Code

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

C++ project_euler/problem_104/solution.cpp
#include <iostream>
#include <cmath>
using namespace std;

/*
 * Problem 104: Pandigital Fibonacci Ends
 *
 * Maintain F_k mod 10^9 for trailing digits. Use the logarithmic
 * formula for leading digits. Return the first k where both ends
 * are 1-9 pandigital.
 */

bool is_pandigital(long long n) {
    if (n < 123456789 || n > 987654321) return false;
    int seen[10] = {};
    for (int i = 0; i < 9; i++) {
        int d = n % 10;
        n /= 10;
        if (d == 0 || ++seen[d] > 1) return false;
    }
    return true;
}

int main() {
    const long long MOD = 1000000000LL;
    const double LOG10_PHI = log10((1.0 + sqrt(5.0)) / 2.0);
    const double LOG10_SQRT5 = 0.5 * log10(5.0);

    long long a = 1, b = 1;
    for (int k = 3; ; k++) {
        long long c = (a + b) % MOD;
        a = b;
        b = c;
        if (!is_pandigital(c)) continue;
        double L = (double)k * LOG10_PHI - LOG10_SQRT5;
        double frac = L - floor(L);
        long long first9 = (long long)pow(10.0, frac + 8.0);
        if (is_pandigital(first9)) {
            cout << k << endl;
            return 0;
        }
    }
}