File tree 1 file changed +57
-0
lines changed
1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
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 ())))
You can’t perform that action at this time.
0 commit comments