January 2020
Intermediate to advanced
532 pages
13h 31m
English
First, let's analyze how bad the performance is by revising the function to keep track of the number of executions. The code is as follows:
function fib(n) if n < 3 return (result = 1, counter = 1) else result1, counter1 = fib(n - 1) result2, counter2 = fib(n - 2) return (result = result1 + result2, counter = 1 + counter1 + counter2) endend
Every time the fib function is called, it keeps tracks a counter. If the value of n is smaller than 3, then it returns the count of 1 along with the result. If n is a larger number, then it aggregates the counts from the recursive calls to fib function.
Let's run it several times with various input values:
This simple example just illustrated how quickly ...
Read now
Unlock full access