Python Implementation of Problem 25

View source code here on GitHub!

Includes

  • fib()

Problem Solution

Project Euler Problem 25

I modified the fib() function from #2 to suit this problem

Problem:

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?

python.src.p0025.main() int
 1"""
 2Project Euler Problem 25
 3
 4I modified the fib() function from #2 to suit this problem
 5
 6Problem:
 7
 8The Fibonacci sequence is defined by the recurrence relation:
 9
10    Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
11
12Hence the first 12 terms will be:
13
14    F1 = 1
15    F2 = 1
16    F3 = 2
17    F4 = 3
18    F5 = 5
19    F6 = 8
20    F7 = 13
21    F8 = 21
22    F9 = 34
23    F10 = 55
24    F11 = 89
25    F12 = 144
26
27The 12th term, F12, is the first term to contain three digits.
28
29What is the index of the first term in the Fibonacci sequence to contain 1000
30digits?
31"""
32from .lib.fibonacci import fib
33
34
35def main() -> int:
36    check = 10**999
37    for idx, x in enumerate(fib(), 1):
38        if x > check:
39            return idx
40    return -1  # pragma: no cover

Tags: fibonacci-number