MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_025/sol3.py:28–52  ·  view source on GitHub ↗

Returns the index of the first term in the Fibonacci sequence to contain n digits. >>> solution(1000) 4782 >>> solution(100) 476 >>> solution(50) 237 >>> solution(3) 12

(n: int = 1000)

Source from the content-addressed store, hash-verified

26
27
28def solution(n: int = 1000) -> int:
29 """Returns the index of the first term in the Fibonacci sequence to contain
30 n digits.
31
32 >>> solution(1000)
33 4782
34 >>> solution(100)
35 476
36 >>> solution(50)
37 237
38 >>> solution(3)
39 12
40 """
41 f1, f2 = 1, 1
42 index = 2
43 while True:
44 i = 0
45 f = f1 + f2
46 f1, f2 = f2, f
47 index += 1
48 for _ in str(f):
49 i += 1
50 if i == n:
51 break
52 return index
53
54
55if __name__ == "__main__":

Callers 1

sol3.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected