Tim Varley Logo
Tim Varley Systems Engineer
Problem #25 hard

1000-digit Fibonacci Number

The Fibonacci sequence is defined by the recurrence relation:

Fn=Fn1+Fn2F_n = F_{n - 1} + F_{n - 2}, where F1=1F_1 = 1 and F2=1F_2 = 1.

Hence the first 12 terms will be:

F1=1F_1 = 1 F2=1F_2 = 1 F3=2F_3 = 2 F4=3F_4 = 3 F5=5F_5 = 5 F6=8F_6 = 8 F7=13F_7 = 13 F8=21F_8 = 21 F9=34F_9 = 34 F10=55F_{10} = 55 F11=89F_{11} = 89 F12=144F_{12} = 144

The 1212th term, F12F_{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?

Implementations

cpp
// https://projecteuler.net/problem=25
// 1000-digit Fibonacci number
//
// The Fibonacci sequence is defined by the recurrence relation:
//
// Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
// Hence the first 12 terms will be:
//
// F1 = 1
// F2 = 1
// F3 = 2
// F4 = 3
// F5 = 5
// F6 = 8
// F7 = 13
// F8 = 21
// F9 = 34
// F10 = 55
// F11 = 89
// F12 = 144
// The 12th term, F12, is the first term to contain three digits.
//
// What is the index of the first term in the Fibonacci sequence to contain 1000 digits?
// Answer: 4782
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include "simple_timer.h"
typedef std::vector<uint32_t> Zbigint;
Zbigint add(const Zbigint& a, const Zbigint& b) {
Zbigint result;
uint32_t carry = 0;
size_t max_size = std::max(a.size(), b.size());
result.reserve(max_size + 1);
for (size_t i = 0; i < max_size || carry; ++i) {
uint32_t sum = carry;
if (i < a.size()) sum += a[i];
if (i < b.size()) sum += b[i];
result.push_back(sum % 1000000000); // base 1e9
carry = sum / 1000000000;
}
return result;
}
size_t get_digit_count(const Zbigint& num) {
if (num.empty()) return 1;
size_t digits = (num.size() - 1) * 9; // 9 digits per uint32_t except last
uint32_t last = num.back();
while (last > 0) {
digits++;
last /= 10;
}
return digits;
}
int z1000_digit_fibinacci_number() {
Zbigint fib1 = {1};
Zbigint fib2 = {1};
int index = 2;
while (get_digit_count(fib1) < 1000) {
Zbigint next = add(fib1, fib2);
fib2 = fib1;
fib1 = next;
index++;
}
return index;
}
#if ! defined UNITTEST_MODE
int main(int argc, char const *argv[])
{
std::cout << "Answer: " << z1000_digit_fibinacci_number() << std::endl;
}
#endif //#if ! defined UNITTEST_MODE

Solution Notes

Mathematical Background

The Fibonacci sequence exhibits exponential growth with a growth rate determined by the golden ratio φ ≈ 1.618. The number of digits d of F_n grows linearly with n:

d ≈ n × log₁₀(φ) - log₁₀(√5)

For 1000 digits: n ≈ (1000 + log₁₀(√5)) / log₁₀(φ) ≈ 4781.8, which matches the answer of 4782.

The sequence grows so rapidly that F_4782 has exactly 1000 digits, while F_4781 has 999 digits.

Algorithm Analysis

All implementations generate Fibonacci numbers iteratively until reaching 1000 digits. The choice of big integer handling varies by language:

  • Built-in big integers (Python, Java, Go, JavaScript with BigInt): Simple iterative generation
  • Custom big integer arithmetic (C++, Rust): Manual addition with digit arrays
  • String manipulation (Ruby): Convert to string to check length

Time complexity is O(n²) where n is the number of digits, due to big integer operations scaling with digit count.

Key Insights

  • Fibonacci numbers grow exponentially, making direct computation feasible even for large indices
  • The index grows logarithmically with the number of digits required
  • Big integer libraries are essential for handling numbers larger than 64-bit integers
  • The transition from F_4781 (999 digits) to F_4782 (1000 digits) demonstrates the rapid growth

Educational Value

This problem teaches:

  • Exponential growth in sequences and its mathematical analysis
  • The limitations of fixed-size integer types
  • Big integer arithmetic implementation and library usage
  • The relationship between algorithmic complexity and problem constraints
  • How mathematical analysis can predict computational requirements