-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpe25.py
More file actions
46 lines (35 loc) · 918 Bytes
/
pe25.py
File metadata and controls
46 lines (35 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'''
Problem 25
The Fibonacci sequence is defined by the recurrence relation:
F{n} = F{n−1} + F{n−2}, where F{1} = 1 and F{2} = 1.
The 12th 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?
Ans: 4782
'''
from functools import lru_cache
@lru_cache()
def fibo(n: int) -> int:
'''Returns the nth fibonacci number using a memoization wrapper'''
if n < 2:
return n
return fibo(n-2) + fibo(n-1)
def fiboLength(x: int) -> int:
'''
Parameters
----------
x : int
the desired length of the fibonacci number.
Returns
-------
idx : int
the index of the fibonacci number of length x.
'''
idx = 1
fib = 1
while len(str(fib)) < x:
fib = fibo(idx)
idx += 1
return idx - 1
if __name__ == "__main__":
print(fiboLength(1000))