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...
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 ,
where and .
Proof. Let . Since and are the roots of , we have and . Thus:
Base cases: and .
Inductive step: ,
using and . By induction, .
Theorem 2 (Leading Digits via Logarithms). Define and let denote the fractional part of . For sufficiently large, the first significant digits of are .
Proof. From Binet’s formula, . Since , we have exponentially, so for . Therefore:
where . Writing , we obtain , so the significand is and the first digits are .
The error satisfies for , which is well below the precision needed for 9 digits.
Lemma 1 (Modular Recurrence for Trailing Digits). The last 9 digits of equal , which satisfies .
Proof. The ring homomorphism preserves addition: . Applying this to shows the reduced recurrence is exact.
Definition. A positive integer with exactly 9 digits is 1—9 pandigital if its decimal representation is a permutation of .
Editorial
The last nine digits of a Fibonacci number are easy to maintain exactly: we simply run the recurrence modulo . The first nine digits are harder to obtain directly, but Binet’s formula shows that for large they are determined by the fractional part of . 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 , 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. where . Each iteration performs work: one modular addition, one floating-point computation, and one constant-time pandigital check.
- Space. . Only two modular residues and a few floating-point variables are stored.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
}
}
"""
Problem 104: Pandigital Fibonacci Ends
Find the smallest k such that F_k has pandigital first 9 and last 9 digits.
"""
import math
def is_pandigital(n):
s = str(n)
return len(s) == 9 and set(s) == set("123456789")
def solve():
MOD = 10**9
log10_phi = math.log10((1 + math.sqrt(5)) / 2)
log10_sqrt5 = 0.5 * math.log10(5)
a, b = 1, 1
for k in range(3, 1_000_000):
a, b = b, (a + b) % MOD
if not is_pandigital(b):
continue
L = k * log10_phi - log10_sqrt5
frac = L - int(L)
first9 = int(10.0 ** (frac + 8))
if is_pandigital(first9):
return k
return -1
if __name__ == "__main__":
answer = solve()
print(answer)
assert answer == 329468