Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
pp:tailend [2016/05/05 17:57] vrares [Fibonacci sequence] |
pp:tailend [2016/05/05 18:46] (current) vrares [Fibonacci sequence] |
||
---|---|---|---|
Line 5: | Line 5: | ||
===== Fibonacci sequence ===== | ===== Fibonacci sequence ===== | ||
- | Let us consider the following recursive function that computes the n-th Fibonacci number (n = 0 is the first one): | + | Let us consider the following recursive function that computes the n-th Fibonacci number: |
<code> | <code> | ||
Line 16: | Line 16: | ||
</code> | </code> | ||
- | The code works well and behaves as expected, but let us discuss the above function in terms of efficiency: | + | The code works well and behaves as expected, but let us discuss the above function in terms of efficiency. The function calls are shown below: |
+ | |||
+ | <code> | ||
+ | fibo(4) //ninth evaluation, fibo(4) = fibo(3) + fibo(2) = 2 + 1 = 3 | ||
+ | | | ||
+ | +->fibo(3) //fifth evaluation, fibo(3) = fibo(2) + fibo(1) = 1 + 1 = 2 | ||
+ | | | | ||
+ | | +->fibo(2) //third evaluation, fibo(2) = fibo(1) + fibo(0) = 1 + 0 = 1 | ||
+ | | | | | ||
+ | | | +->fibo(1) //first evaluation, fibo(1) = 1 | ||
+ | | | | | ||
+ | | | +->fibo(0) //second evaluation, fibo(0) = 0 | ||
+ | | | | ||
+ | | +->fibo(1) //fourth evaluation, fibo(1) = 1 | ||
+ | | | ||
+ | +->fibo(2) //eighth evaluation, fibo(2) = fibo(1) + fibo(0) = 1 + 0 = 1 | ||
+ | | | ||
+ | +->fibo(1) //sixth evaluation, fibo(1) = 1 | ||
+ | | | ||
+ | +->fibo(0) //seventh evaluation, fibo(0) = 0 | ||
+ | </code> | ||
+ | |||
+ | <stack diagram to be inserted here> | ||
+ | <work in progress> |