1000-Digit Fibonacci Number
The Fibonacci sequence is defined by F_1 = 1, F_2 = 1, and F_n = F_(n-1) + F_(n-2) for n >= 3. Determine the index of the first term to contain 1000 digits.
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\]
Hence the first \(12\) terms will be: \begin {align*} F_1 &= 1 \\ F_2 &= 1 \\ F_3 &= 2 \\ F_4 &= 3 \\ F_5 &= 5 \\ F_6 &= 8 \\ F_7 &= 13 \\ F_8 &= 21 \\ F_9 &= 34 \\ F_{10} &= 56 \\ F_{11} &= 90 \\ F_{12} &= 146 \end {align*}
The \(12\)th term, \(F_{12}\), is the first term to contain three digits.
What is the index of the first term in the Fibonacci sequence to contain \(1000\) digits?
Problem 25: 1000-Digit Fibonacci Number
Mathematical Development
Definitions
Definition 1 (Fibonacci Sequence). The sequence defined by and for .
Definition 2 (Golden Ratio). and . These are the roots of the characteristic polynomial .
Theorems and Proofs
Theorem 1 (Binet’s Formula). For all ,
Proof. The recurrence has characteristic equation with roots and . Since , the general solution of the recurrence is for constants determined by initial conditions.
From : . From : . Since and (both satisfy ), the second equation becomes , hence . Substituting into the first equation gives . Since , we conclude and .
Lemma 1 (Nearest Integer Property). For all ,
In particular, (the nearest integer to ).
Proof. By Binet’s formula, , so the absolute error is . Since , we have for all :
The nearest-integer characterization follows immediately.
Theorem 2 (Digit Count of Fibonacci Numbers). The number of decimal digits of (for ) is
Proof. A positive integer has digits if and only if , equivalently . By Lemma 1, where . Therefore
The correction term tends to 0 exponentially fast and is small enough that it does not affect the integer part of for all . (Formally: , and even for , while the fractional part of is bounded away from 0 and 1 for the relevant .) Hence .
Theorem 3 (Index Formula). The smallest index such that has at least digits is
Proof. By Theorem 2, has at least digits if and only if
which holds if and only if (since we seek the first such , the floor equals exactly at the transition). Rearranging:
The smallest integer satisfying this is the ceiling.
Numerical Evaluation for
Verification.
- has 999 digits.
- has 1000 digits.
Editorial
We keep both the analytical and iterative viewpoints. The closed-form method uses the logarithmic inequality derived from Binet’s formula to compute the smallest index whose Fibonacci number has digits, while the iterative method advances the Fibonacci sequence until the digit threshold is reached. This is sufficient because the formula identifies the first valid index directly and the iterative scan provides a straightforward verification.
Pseudocode
Algorithm: First Fibonacci Index with D Digits by Formula
Require: An integer D ≥ 1.
Ensure: The least index n such that F_n has at least D decimal digits.
1: Compute phi ← (1 + sqrt(5)) / 2.
2: Evaluate alpha ← (D - 1 + log_10(5) / 2) / log_10(phi).
3: Set n ← ceil(alpha).
4: Return n.
Algorithm: First Fibonacci Index with D Digits by Iteration
Require: An integer D ≥ 1.
Ensure: The least index n such that F_n has at least D decimal digits.
1: Initialize (F_prev, F_curr, n) ← (1, 1, 2).
2: While the decimal length of F_curr is less than D do:
3: Update (F_prev, F_curr, n) ← (F_curr, F_prev + F_curr, n + 1).
4: Return n.
Complexity Analysis
Proposition (Analytical Method). The closed-form evaluation in Theorem 3 requires floating-point operations and space.
Proof. The formula involves a fixed number of logarithm evaluations, one addition, one division, and one ceiling operation.
Proposition (Iterative Method). The iterative approach runs in time and space, where and .
Proof. The loop performs iterations. Each iteration adds two integers with at most digits, which takes time with schoolbook addition. Only two consecutive Fibonacci numbers need to be stored at any time, each requiring space.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> BigInt;
BigInt from_int(int x) {
BigInt r;
while (x > 0) { r.push_back(x % 10); x /= 10; }
if (r.empty()) r.push_back(0);
return r;
}
BigInt add(const BigInt& a, const BigInt& b) {
BigInt r;
int carry = 0, n = max(a.size(), b.size());
for (int i = 0; i < n || carry; i++) {
int s = carry;
if (i < (int)a.size()) s += a[i];
if (i < (int)b.size()) s += b[i];
r.push_back(s % 10);
carry = s / 10;
}
return r;
}
int main() {
BigInt a = from_int(1), b = from_int(1);
int idx = 2;
while ((int)b.size() < 1000) {
BigInt c = add(a, b);
a = b;
b = c;
idx++;
}
cout << idx << endl;
return 0;
}
import math
def solve():
log_phi = math.log10((1 + math.sqrt(5)) / 2)
h = 0.5 * math.log10(5)
n = math.ceil((999 + h) / log_phi)
# Verify iteratively
a, b = 1, 1
idx = 2
while len(str(b)) < 1000:
a, b = b, a + b
idx += 1
assert n == idx
print(n)