10
10
...
11
11
...
12
12
-> [f(n),f(n-1)] = [[1,1],[1,0]]^(n-1) * [f(1),f(0)]
13
- So we just need the n times multiplication of the matrix [1,1],[1,0]].
14
- We can decrease the n times multiplication by following the divide and conquer approach.
15
-
13
+ So we just need the n times multiplication of the matrix [1,1],[1,0]].
14
+ We can decrease the n times multiplication by following the divide and conquer approach.
16
15
"""
17
16
from __future__ import print_function
18
17
@@ -35,11 +34,11 @@ def identity(n):
35
34
return [[int (row == column ) for column in range (n )] for row in range (n )]
36
35
37
36
38
- def nth_fibonacci (n ):
37
+ def nth_fibonacci_matrix (n ):
39
38
"""
40
- >>> nth_fibonacci (100)
41
- 354224848179261915075L
42
- >>> nth_fibonacci (-100)
39
+ >>> nth_fibonacci_matrix (100)
40
+ 354224848179261915075
41
+ >>> nth_fibonacci_matrix (-100)
43
42
-100
44
43
"""
45
44
if n <= 1 :
@@ -55,7 +54,13 @@ def nth_fibonacci(n):
55
54
return res_matrix [0 ][0 ]
56
55
57
56
58
- def nth_fibonacci_test (n ):
57
+ def nth_fibonacci_bruteforce (n ):
58
+ """
59
+ >>> nth_fibonacci_bruteforce(100)
60
+ 354224848179261915075
61
+ >>> nth_fibonacci_bruteforce(-100)
62
+ -100
63
+ """
59
64
if n <= 1 :
60
65
return n
61
66
fib0 = 0
@@ -68,31 +73,31 @@ def nth_fibonacci_test(n):
68
73
def main ():
69
74
print (
70
75
"0th fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n "
71
- % (nth_fibonacci (0 ), nth_fibonacci_test (0 ))
76
+ % (nth_fibonacci_matrix (0 ), nth_fibonacci_bruteforce (0 ))
72
77
)
73
78
print (
74
79
"1st fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n "
75
- % (nth_fibonacci (1 ), nth_fibonacci_test (1 ))
80
+ % (nth_fibonacci_matrix (1 ), nth_fibonacci_bruteforce (1 ))
76
81
)
77
82
print (
78
83
"2nd fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n "
79
- % (nth_fibonacci (2 ), nth_fibonacci_test (2 ))
84
+ % (nth_fibonacci_matrix (2 ), nth_fibonacci_bruteforce (2 ))
80
85
)
81
86
print (
82
87
"3rd fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n "
83
- % (nth_fibonacci (3 ), nth_fibonacci_test (3 ))
88
+ % (nth_fibonacci_matrix (3 ), nth_fibonacci_bruteforce (3 ))
84
89
)
85
90
print (
86
91
"10th fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n "
87
- % (nth_fibonacci (10 ), nth_fibonacci_test (10 ))
92
+ % (nth_fibonacci_matrix (10 ), nth_fibonacci_bruteforce (10 ))
88
93
)
89
94
print (
90
95
"100th fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n "
91
- % (nth_fibonacci (100 ), nth_fibonacci_test (100 ))
96
+ % (nth_fibonacci_matrix (100 ), nth_fibonacci_bruteforce (100 ))
92
97
)
93
98
print (
94
99
"1000th fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n "
95
- % (nth_fibonacci (1000 ), nth_fibonacci_test (1000 ))
100
+ % (nth_fibonacci_matrix (1000 ), nth_fibonacci_bruteforce (1000 ))
96
101
)
97
102
98
103
0 commit comments