GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Minor optimizations for nth_fibonacci_number, PEP8 formatting. (#2) · johnsonhit/coding-problems@500e0bd

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Other
    • nth_fibonacci_number.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Other/nth_fibonacci_number.py‎

Copy file name to clipboardExpand all lines: Other/nth_fibonacci_number.py
+60-12Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
##############
7171

7272
def nth_fibonacci_1(n):
73-
if (n == 0) or (n == 1):
73+
if n == 0 or n == 1:
7474
return n
7575

7676
return nth_fibonacci_1(n - 1) + nth_fibonacci_1(n - 2)
@@ -99,7 +99,7 @@ def nth_fibonacci_2(n):
9999
##############
100100

101101
def nth_fibonacci_3(n):
102-
dp = [0 for i in range(max(2, n + 1))]
102+
dp = [0] * max(2, n+1)
103103
dp[1] = 1
104104

105105
for i in range(2, n + 1):
@@ -132,14 +132,22 @@ def matrix_mult(a, b):
132132
shouldn't be changed, only change the values after all computations are completed
133133
because 'b' could be the same reference/matrix as 'a').
134134
'''
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
143151

144152

145153
##############
@@ -168,7 +176,7 @@ def nth_fibonacci_6(n):
168176
return res[1][1]
169177

170178
def matrix_pow(mat, n):
171-
if (n == 0) or (n == 1):
179+
if n == 0 or n == 1:
172180
return
173181

174182
# first compute the power of n/2
@@ -223,4 +231,44 @@ def nth_fibonacci_7(n):
223231
print(nth_fibonacci_4(21))
224232
print(nth_fibonacci_5(21))
225233
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+

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.