Skip to content

Commit 39c40e7

Browse files
suadjelilicclauss
authored andcommitted
added solution 3 for problem_25 (TheAlgorithms#1478)
* added solution 4 for problem_20 * added solution 3 for problem_25
1 parent 8b52e44 commit 39c40e7

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

project_euler/problem_25/sol3.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
The Fibonacci sequence is defined by the recurrence relation:
4+
5+
Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
6+
7+
Hence the first 12 terms will be:
8+
9+
F1 = 1
10+
F2 = 1
11+
F3 = 2
12+
F4 = 3
13+
F5 = 5
14+
F6 = 8
15+
F7 = 13
16+
F8 = 21
17+
F9 = 34
18+
F10 = 55
19+
F11 = 89
20+
F12 = 144
21+
22+
The 12th term, F12, is the first term to contain three digits.
23+
24+
What is the index of the first term in the Fibonacci sequence to contain 1000
25+
digits?
26+
"""
27+
28+
29+
def solution(n):
30+
"""Returns the index of the first term in the Fibonacci sequence to contain
31+
n digits.
32+
33+
>>> solution(1000)
34+
4782
35+
>>> solution(100)
36+
476
37+
>>> solution(50)
38+
237
39+
>>> solution(3)
40+
12
41+
"""
42+
f1, f2 = 1, 1
43+
index = 2
44+
while True:
45+
i = 0
46+
f = f1 + f2
47+
f1, f2 = f2, f
48+
index += 1
49+
for j in str(f):
50+
i += 1
51+
if i == n:
52+
break
53+
return index
54+
55+
56+
if __name__ == "__main__":
57+
print(solution(int(str(input()).strip())))

0 commit comments

Comments
 (0)