Skip to content
Snippets Groups Projects
Commit db58c4e8 authored by Michael Mutote's avatar Michael Mutote
Browse files

22202956

Exercise 4.9 question 2 done.
the files are split as the code snippets are considerably longer
parent 01f661d6
No related branches found
No related tags found
No related merge requests found
# question 2
def fibonacci(n):
# inefficient recursive fibonacci, counts down from "n" while each iteration spawns 2 recursive calls
if n == 0:
return 0
if n == 1:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)
def fast_fibonacci(n, n_minus_2=0, n_minus_1=1):
# efficient recursive fibonacci, by going forward with each iteration spawning only 1 recursive call
if n == 0:
return 0
if n == 1:
return n_minus_1
else:
return fast_fibonacci(n - 1, n_minus_1, n_minus_1 + n_minus_2)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment