|
70 | 70 | ############## |
71 | 71 |
|
72 | 72 | def nth_fibonacci_1(n): |
73 | | - if (n == 0) or (n == 1): |
| 73 | + if n == 0 or n == 1: |
74 | 74 | return n |
75 | 75 |
|
76 | 76 | return nth_fibonacci_1(n - 1) + nth_fibonacci_1(n - 2) |
@@ -99,7 +99,7 @@ def nth_fibonacci_2(n): |
99 | 99 | ############## |
100 | 100 |
|
101 | 101 | def nth_fibonacci_3(n): |
102 | | - dp = [0 for i in range(max(2, n + 1))] |
| 102 | + dp = [0] * max(2, n+1) |
103 | 103 | dp[1] = 1 |
104 | 104 |
|
105 | 105 | for i in range(2, n + 1): |
@@ -132,14 +132,22 @@ def matrix_mult(a, b): |
132 | 132 | shouldn't be changed, only change the values after all computations are completed |
133 | 133 | because 'b' could be the same reference/matrix as 'a'). |
134 | 134 | ''' |
135 | | - a00 = a[0][0]*b[0][0] + a[0][1]*b[1][0] |
136 | | - a01 = a[0][0]*b[0][1] + a[0][1]*b[1][1] |
137 | | - a10 = a[1][0]*b[0][0] + a[1][1]*b[1][0] |
138 | | - a11 = a[1][0]*b[0][1] + a[1][1]*b[1][1] |
139 | | - a[0][0] = a00 |
140 | | - a[0][1] = a01 |
141 | | - a[1][0] = a10 |
142 | | - a[1][1] = a11 |
| 135 | + |
| 136 | + # We know that a is a 2x2 matrix: |
| 137 | + # a[0] is the first row of a, which contains a[0][0], a[0][1] |
| 138 | + # Python "unrolls" a00, a01 = a[0], |
| 139 | + # which effectively makes it: |
| 140 | + # a00 = a[0][0] |
| 141 | + # a01 = a[0][1] |
| 142 | + |
| 143 | + a00, a01 = a[0] |
| 144 | + a10, a11 = a[1] |
| 145 | + b00, b01 = b[0] |
| 146 | + b10, b11 = b[1] |
| 147 | + a[0][0] = a00 * b00 + a01 * b10 |
| 148 | + a[0][1] = a00 * b01 + a01 * b11 |
| 149 | + a[1][0] = a10 * b00 + a11 * b10 |
| 150 | + a[1][1] = a10 * b01 + a11 * b11 |
143 | 151 |
|
144 | 152 |
|
145 | 153 | ############## |
@@ -168,7 +176,7 @@ def nth_fibonacci_6(n): |
168 | 176 | return res[1][1] |
169 | 177 |
|
170 | 178 | def matrix_pow(mat, n): |
171 | | - if (n == 0) or (n == 1): |
| 179 | + if n == 0 or n == 1: |
172 | 180 | return |
173 | 181 |
|
174 | 182 | # first compute the power of n/2 |
@@ -223,4 +231,44 @@ def nth_fibonacci_7(n): |
223 | 231 | print(nth_fibonacci_4(21)) |
224 | 232 | print(nth_fibonacci_5(21)) |
225 | 233 | print(nth_fibonacci_6(21)) |
226 | | -print(nth_fibonacci_7(21)) |
| 234 | +print(nth_fibonacci_7(21)) |
| 235 | + |
| 236 | +from timeit import default_timer |
| 237 | +# Comparision of how long each method takes: |
| 238 | +iters = 100 |
| 239 | +num = 25 |
| 240 | +start = default_timer() |
| 241 | +for _ in range(iters): |
| 242 | + nth_fibonacci_1(num) |
| 243 | +end = default_timer() - start |
| 244 | +print("nth_fibonacci_1 took %fs" % end) |
| 245 | +start = default_timer() |
| 246 | +for _ in range(iters): |
| 247 | + nth_fibonacci_2(num) |
| 248 | +end = default_timer() - start |
| 249 | +print("nth_fibonacci_2 took %fs" % end) |
| 250 | +start = default_timer() |
| 251 | +for _ in range(iters): |
| 252 | + nth_fibonacci_3(num) |
| 253 | +end = default_timer() - start |
| 254 | +print("nth_fibonacci_3 took %fs" % end) |
| 255 | +start = default_timer() |
| 256 | +for _ in range(iters): |
| 257 | + nth_fibonacci_4(num) |
| 258 | +end = default_timer() - start |
| 259 | +print("nth_fibonacci_4 took %fs" % end) |
| 260 | +start = default_timer() |
| 261 | +for _ in range(iters): |
| 262 | + nth_fibonacci_5(num) |
| 263 | +end = default_timer() - start |
| 264 | +print("nth_fibonacci_5 took %fs" % end) |
| 265 | +for _ in range(iters): |
| 266 | + nth_fibonacci_6(num) |
| 267 | +end = default_timer() - start |
| 268 | +print("nth_fibonacci_6 took %fs" % end) |
| 269 | +start = default_timer() |
| 270 | +for _ in range(iters): |
| 271 | + nth_fibonacci_7(num) |
| 272 | +end = default_timer() - start |
| 273 | +print("nth_fibonacci_7 took %fs" % end) |
| 274 | + |