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
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. 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