All Euler problems
Project Euler

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.

Source sync May 21, 2026
Problem #0025
Level Level 00
Solved By 169,335
Languages C++, Python
Answer 4782
Length 423 words
number_theorysequencealgebra

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 (Fn)n≥1(F_n)_{n \geq 1} defined by F1=F2=1F_1 = F_2 = 1 and Fn=Fn−1+Fn−2F_n = F_{n-1} + F_{n-2} for n≥3n \geq 3.

Definition 2 (Golden Ratio). φ=1+52\varphi = \frac{1 + \sqrt{5}}{2} and ψ=1−52\psi = \frac{1 - \sqrt{5}}{2}. These are the roots of the characteristic polynomial x2−x−1=0x^2 - x - 1 = 0.

Theorems and Proofs

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

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

Proof. The recurrence Fn=Fn−1+Fn−2F_n = F_{n-1} + F_{n-2} has characteristic equation x2−x−1=0x^2 - x - 1 = 0 with roots φ\varphi and ψ\psi. Since φ≠ψ\varphi \neq \psi, the general solution of the recurrence is Fn=Aφn+BψnF_n = A\varphi^n + B\psi^n for constants A,BA, B determined by initial conditions.

From F1=1F_1 = 1: Aφ+Bψ=1A\varphi + B\psi = 1. From F2=1F_2 = 1: Aφ2+Bψ2=1A\varphi^2 + B\psi^2 = 1. Since φ2=φ+1\varphi^2 = \varphi + 1 and ψ2=ψ+1\psi^2 = \psi + 1 (both satisfy x2=x+1x^2 = x + 1), the second equation becomes Aφ+Bψ+A+B=1A\varphi + B\psi + A + B = 1, hence A+B=0A + B = 0. Substituting B=−AB = -A into the first equation gives A(φ−ψ)=1A(\varphi - \psi) = 1. Since φ−ψ=5\varphi - \psi = \sqrt{5}, we conclude A=1/5A = 1/\sqrt{5} and B=−1/5B = -1/\sqrt{5}. □\square

Lemma 1 (Nearest Integer Property). For all n≥1n \geq 1,

∣Fn−φn5∣=∣ψ∣n5<12.\left|F_n - \frac{\varphi^n}{\sqrt{5}}\right| = \frac{|\psi|^n}{\sqrt{5}} < \frac{1}{2}.

In particular, Fn=⌊φn5+12⌋F_n = \left\lfloor \frac{\varphi^n}{\sqrt{5}} + \frac{1}{2} \right\rfloor (the nearest integer to φn/5\varphi^n / \sqrt{5}).

Proof. By Binet’s formula, Fn−φn/5=−ψn/5F_n - \varphi^n/\sqrt{5} = -\psi^n/\sqrt{5}, so the absolute error is ∣ψ∣n/5|\psi|^n / \sqrt{5}. Since ∣ψ∣=(5−1)/2≈0.618|\psi| = (\sqrt{5} - 1)/2 \approx 0.618, we have for all n≥1n \geq 1:

∣ψ∣n5≤∣ψ∣5=5−125=12−125<12.\frac{|\psi|^n}{\sqrt{5}} \leq \frac{|\psi|}{\sqrt{5}} = \frac{\sqrt{5} - 1}{2\sqrt{5}} = \frac{1}{2} - \frac{1}{2\sqrt{5}} < \frac{1}{2}.

The nearest-integer characterization follows immediately. □\square

Theorem 2 (Digit Count of Fibonacci Numbers). The number of decimal digits of FnF_n (for n≥1n \geq 1) is

⌊log⁡10Fn⌋+1=⌊nlog⁡10φ−12log⁡105⌋+1.\lfloor \log_{10} F_n \rfloor + 1 = \lfloor n \log_{10}\varphi - \tfrac{1}{2}\log_{10} 5 \rfloor + 1.

Proof. A positive integer mm has dd digits if and only if 10d−1≤m<10d10^{d-1} \leq m < 10^d, equivalently d=⌊log⁡10m⌋+1d = \lfloor \log_{10} m \rfloor + 1. By Lemma 1, Fn=φn/5+δnF_n = \varphi^n / \sqrt{5} + \delta_n where ∣δn∣<1/2|\delta_n| < 1/2. Therefore

log⁡10Fn=nlog⁡10φ−12log⁡105+log⁡10 ⁣(1+δn5φn).\log_{10} F_n = n \log_{10}\varphi - \tfrac{1}{2}\log_{10} 5 + \log_{10}\!\left(1 + \frac{\delta_n \sqrt{5}}{\varphi^n}\right).

The correction term log⁡10(1+δn5/φn)\log_{10}(1 + \delta_n\sqrt{5}/\varphi^n) tends to 0 exponentially fast and is small enough that it does not affect the integer part of log⁡10Fn\log_{10} F_n for all n≥1n \geq 1. (Formally: ∣δn5/φn∣=∣ψ/φ∣n<0.382n|\delta_n\sqrt{5}/\varphi^n| = |\psi/\varphi|^n < 0.382^n, and ∣log⁡10(1+x)∣<∣x∣/ln⁡10<0.44⋅0.382n<0.17|\log_{10}(1 + x)| < |x|/\ln 10 < 0.44 \cdot 0.382^n < 0.17 even for n=1n = 1, while the fractional part of nlog⁡10φ−12log⁡105n\log_{10}\varphi - \frac{1}{2}\log_{10}5 is bounded away from 0 and 1 for the relevant nn.) Hence ⌊log⁡10Fn⌋=⌊nlog⁡10φ−12log⁡105⌋\lfloor \log_{10} F_n \rfloor = \lfloor n \log_{10}\varphi - \frac{1}{2}\log_{10} 5 \rfloor. □\square

Theorem 3 (Index Formula). The smallest index nn such that FnF_n has at least DD digits is

n=⌈(D−1)+12log⁡105log⁡10φ⌉.n = \left\lceil \frac{(D - 1) + \frac{1}{2}\log_{10} 5}{\log_{10} \varphi} \right\rceil.

Proof. By Theorem 2, FnF_n has at least DD digits if and only if

⌊nlog⁡10φ−12log⁡105⌋≥D−1,\lfloor n\log_{10}\varphi - \tfrac{1}{2}\log_{10}5 \rfloor \geq D - 1,

which holds if and only if nlog⁡10φ−12log⁡105≥D−1n\log_{10}\varphi - \frac{1}{2}\log_{10}5 \geq D - 1 (since we seek the first such nn, the floor equals exactly D−1D - 1 at the transition). Rearranging:

n≥(D−1)+12log⁡105log⁡10φ.n \geq \frac{(D-1) + \frac{1}{2}\log_{10}5}{\log_{10}\varphi}.

The smallest integer satisfying this is the ceiling. □\square

Numerical Evaluation for D=1000D = 1000

log⁡10φ=log⁡10 ⁣(1+52)≈0.20898764024997873\log_{10}\varphi = \log_{10}\!\left(\tfrac{1+\sqrt{5}}{2}\right) \approx 0.20898764024997873 12log⁡105≈0.34948500216800940\tfrac{1}{2}\log_{10} 5 \approx 0.34948500216800940 n≥999+0.34948500…0.20898764…=999.34948…0.20898764…≈4781.8594…n \geq \frac{999 + 0.34948500\ldots}{0.20898764\ldots} = \frac{999.34948\ldots}{0.20898764\ldots} \approx 4781.8594\ldots n=⌈4781.8594…⌉=4782.n = \lceil 4781.8594\ldots \rceil = 4782.

Verification.

  • ⌊4781⋅log⁡10φ−12log⁡105⌋=⌊998.859…⌋=998  ⟹  F4781\lfloor 4781 \cdot \log_{10}\varphi - \frac{1}{2}\log_{10}5 \rfloor = \lfloor 998.859\ldots \rfloor = 998 \implies F_{4781} has 999 digits.
  • ⌊4782⋅log⁡10φ−12log⁡105⌋=⌊999.068…⌋=999  ⟹  F4782\lfloor 4782 \cdot \log_{10}\varphi - \frac{1}{2}\log_{10}5 \rfloor = \lfloor 999.068\ldots \rfloor = 999 \implies F_{4782} has 1000 digits. ✓\checkmark

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 DD 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 O(1)O(1) floating-point operations and O(1)O(1) space.

Proof. The formula involves a fixed number of logarithm evaluations, one addition, one division, and one ceiling operation. □\square

Proposition (Iterative Method). The iterative approach runs in O(nD)O(nD) time and O(D)O(D) space, where n≈4782n \approx 4782 and D=1000D = 1000.

Proof. The loop performs n−2n - 2 iterations. Each iteration adds two integers with at most DD digits, which takes O(D)O(D) time with schoolbook addition. Only two consecutive Fibonacci numbers need to be stored at any time, each requiring O(D)O(D) space. □\square

Answer

4782\boxed{4782}

Code

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

C++ project_euler/problem_025/solution.cpp
#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;
}