Question

I'm going through a programming book and one of the examples is about fibonacci numbers, and how a recurring function finds the fibonacci number for the nth one along.

The code looks like this:

Int fib (int n)
{
If (n<3)
Return (1);
Else
Return (fib(n-1))+(fib(n-2))
}

Now this isn't exact because I'm typing from my phone and I have a understanding how the code is working, it's calling itself until it returns 1, then it's adds up the return values until you have the correct fibonacci number for the position in the sequence.

So I don't need help with the code. What I do need help with is understanding why this works. How does adding all the returns give the correct answer?

Please can someone explain why this is working. Thanks. It's driving me mad.

Was it helpful?

Solution

A Fibonacci number is defined as the sum of the two preceding Fibonacci numbers. That gives the following:

1 1 2 3 5 8 13 ...

So for the 3rd number (1 1 2) you would take the result of finding the previous - i.e. 2nd (1 1 2) number and add it to the number before the previous - i.e. 1st (1 1 2) number.

You also have to understand that the program needs to calculate the value of the two preceding numbers before it can calculate the number you want to know. Therefore it keeps calling itself - using the same method - until it has calculated everything.

OTHER TIPS

Recursion is like this:

  1. A child couldn't sleep, so her mother told her a story about a little frog,
  2. who couldn't sleep, so the frog's mother told her a story about a little bear,
  3. who couldn't sleep, so the bear's mother told her a story about a little weasel...
  4. who fell asleep.
  5. ..and the little bear fell asleep;
  6. ...and the little frog fell asleep;
  7. ...and the child fell asleep.

source

I would suggest to understand how recursion works. Basically fib function is executing itself with a smaller argument until argument comes down to 2, then it just returns 1.

fib(1) = 1
fib(2) = 1
fib(3) = fib(2) + fib(1) = 1 + 1 = 2
fib(4) = fib(3) [ fib(2) + fib(1) = 1 + 1 = 2 ] + fib(2) = 2 + 1
...

One way to understand how it works is to run it in a debugger step-by-step.

It's the definition of Fibonacci numbers.

n-th Fibonacci number returns the sum of (n-1)th and (n-2)th Fibonacci numbers.

So we can prove by inductive reasoning that, if fib(n-1) and fib(n-2) give the valid (n-1)-th and (n-2)-th Fibonacci number, fib(n) = fib(n-1)+fib(n-2) will be the valid n-th Fibonacci number.

The base step is that fib(1) and fib(2) are correct (that's it fib(1)=fib(2)=1)...

The trick to understanding recursion is understanding the stack.

I'm at line 2 in a function called main, all my local variables are stored in my stack frame:

+------------------+
| main() variables | The stack frame
+------------------+

I then call fib(3), so the computer pushes my current position (EIP) to the stack, then creates a new stack frame for fib and adds that on too. I can only ever access the top stack frame:

+------------------+
| fib()  n = 5     | Top of stack (current frame)
+------------------+
| EIP: main @ 2,1  |
+------------------+
| main() variables | Bottom of stack
+------------------+

On line 4 of fib, it calls fib again, so the same happens again:

+------------------+
| fib()    n = 4   | Top of stack
+------------------+
| EIP: fib @ 4,1   |
+------------------+
| fib()    n = 5   |
+------------------+
| EIP: main @ 2,1  |
+------------------+
| main() variables | Bottom of stack
+------------------+

And it does this again and again as the function is recursively called. The stack grows until something returns, at which point, at line 2 of fib, it returns 1. This pops the top stack frame and discards it, it then returns execution to the saved execution pointer and the program continues where it left off

+------------------+
| fib()    n = 3   | Top of stack
+------------------+
    ... etc ...
+------------------+
| EIP: main @ 2,1  |
+------------------+
| main() variables | Bottom of stack
+------------------+

Eventually you end up back in the calling function (or you get a stack overflow as it grows too large). The key thing to remember is that each time the function is called, it gets a new stack frame containing all your local variables, and your previous position is saved. That's recursion.

The main problem is that in teaching people recursion, everyone always uses the Fibonacci sequence which means having two recursive function calls on one line. This is unnecessarily confusing, as I'm sure you'll agree!

A fibonacci number is defined as the sum of the two previous fibonacci numbers. Which are each defined as the sum of the two previous fibonacci numbers. Etcetera, etcetera, until you reach 1. Understand? Any random fibonacci number can be defined as the sum of two fibonacci numbers; those can be recursively defined as the sum of two fibonacci numbers, etc. That is, the definition of a fibonacci number is fundamentally recursive; that is, the definition of it involves what it defines.

This stuff can be tricky, but it's very fundamental to understanding recursion and computer science. Keep working on it; it'll click eventually.

It's called recursion

This video tutorial should give you a better picture of how Fibonacci recursion works

[link] http://www.schooltrainer.com/study-material/computer-science/stepping-through-recursive-fibonacci-function.html

By definition, fibonacci numbers are the sum of the two previous numbers in the series (where the first two are 0 and 1).

So, when you get the fibonacci number at one location, you can re-write it as the sum of the previously two fibonacci numbers.

Using recursion, you go through this process until the the "previously two fibonacci numbers" are 1 and 1 (in the case of this algorithm), and then proceed to adding the numbers together "back up" the recursion until you get back to your original location in the sequence.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top