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 May 21, 2026
Problem #0104
Level Level 03
Solved By 18,264
Languages C++, Python
Answer 329468
Length 340 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 n≥1n \geq 1,

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

where φ=1+52\varphi = \frac{1 + \sqrt{5}}{2} and ψ=1−52\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 x2−x−1=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=5⋅1/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(n−1)+f(n−2)=φn−2(φ+1)−ψn−2(ψ+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=nlog⁡10φ−12log⁡105L_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}+m−1⌋\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/5∣→0|\psi^n/\sqrt{5}| \to 0 exponentially, so Fn=⌊φn/5+1/2⌋F_n = \lfloor \varphi^n/\sqrt{5} + 1/2 \rfloor for n≥1n \geq 1. Therefore:

log⁡10Fn=log⁡10 ⁣(φn5)+εn=nlog⁡10φ−12log⁡105+ε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 Fn≈10{Ln}⋅10⌊Ln⌋F_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}+m−1⌋\lfloor 10^{\{L_n\} + m - 1} \rfloor.

The error ∣εn∣|\varepsilon_n| satisfies ∣εn∣<∣ψ∣n/(5ln⁡10)<10−14|\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 Fn mod 109F_n \bmod 10^9, which satisfies Fn mod 109=(Fn−1 mod 109+Fn−2 mod 109) mod 109F_n \bmod 10^9 = (F_{n-1} \bmod 10^9 + F_{n-2} \bmod 10^9) \bmod 10^9.

Proof. The ring homomorphism Z→Z/109Z\mathbb{Z} \to \mathbb{Z}/10^9\mathbb{Z} preserves addition: (a+b) mod m=((a mod m)+(b mod m)) mod m(a + b) \bmod m = ((a \bmod m) + (b \bmod m)) \bmod m. Applying this to Fn=Fn−1+Fn−2F_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

The last nine digits of a Fibonacci number are easy to maintain exactly: we simply run the recurrence modulo 10910^9. The first nine digits are harder to obtain directly, but Binet’s formula shows that for large kk they are determined by the fractional part of klog⁡10φ−12log⁡105k \log_{10}\varphi - \tfrac12 \log_{10} 5. So the problem naturally splits into a cheap exact check for the tail and a logarithmic approximation for the head.

The implementation uses that split as a filter. It advances through Fibonacci residues modulo 10910^9, tests whether the trailing nine digits are pandigital, and only for those rare survivors computes the leading nine digits from the logarithmic formula. This avoids ever constructing huge Fibonacci numbers while still checking exactly the two conditions required by the problem.

Pseudocode

Store two consecutive Fibonacci residues modulo 10^9.

For k = 3, 4, 5, ... :
    Update the trailing nine digits with the modular Fibonacci recurrence.
    If the last nine digits are not 1-9 pandigital, move on to the next index.
    Use the fractional part of k log10(phi) - (1/2) log10(5) to reconstruct the first nine digits.
    If those first nine digits are also 1-9 pandigital, return k.

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;
        }
    }
}