9

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.

Joseph
  • 3,899
  • 10
  • 33
  • 52
  • 3
    If you are asking this, it must be because you don't understand (a) recursion or (b) what the Fibonacci series is. What is it? – CesarGon Nov 03 '10 at 23:55
  • http://www.google.com/search?q=recursion :-) – Anycorn Nov 03 '10 at 23:56
  • I understand how the code works and understand what fibonacci numbers are, also I get that recursion is happening because the function is calling itself I just can't figure out why its working. – Joseph Nov 04 '10 at 00:02

9 Answers9

13

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

tpae
  • 6,286
  • 2
  • 37
  • 64
8

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.

Leonid
  • 22,360
  • 25
  • 67
  • 91
  • 3
    Actually, the OP stated that he understands recursion fine. He just cant understand why the recursive function can generate Fibonacci numbers. My guess is the OP doesn't understand Fibonacci numbers or have a "textbook" understanding (That is, if asked he can tell you it's the sum of two previous Fibonacci numbers but actually doesn't understand what that really mean, hence not see that what it is is really recursion. This is just enough to pass exams but unfortunately not enough to apply it in real life). – slebetman Nov 04 '10 at 00:06
5

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.

4

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)...

peoro
  • 25,562
  • 20
  • 98
  • 150
  • 1
    Is there a easier way to explain this? This is the problem I'm getting stuck with. I just can't wrap my head around it – Joseph Nov 04 '10 at 00:17
  • What's not clear to you? fib(n) IS fib(n-1)+fib(n-2) by definition. That means that, the n-th Fibonacci number is (equals) the sum of the two previous Fibonacci numbers. -- It's just like saying: the n-th Natural number is (equals) the (n-1)-th + 1; the seventh natural number (ie: 7) is 6+1. This is the definition of Natural numbers – peoro Nov 04 '10 at 00:21
2

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!

Gareth Davidson
  • 4,857
  • 2
  • 26
  • 45
1

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.

Paul Sonier
  • 38,903
  • 3
  • 77
  • 117
  • How comes we do n-1 + n-2? I'm not making the connection of how taking away 1 or 2 and repeating finds out the correct fibonacci number. – Joseph Nov 04 '10 at 00:25
  • @Joseph, the definition of a fibonacci number (n) is that it is the sum of the previous two fibonacci numbers (n - 1, n - 2). You're not taking away 1 or 2 from the number itself; you're going backward in the sequence of numbers that make up the fibonacci numbers. If your fibonacci number (n) is 13, then the fibonacci number (n-1) is 8, not 12. – Paul Sonier Nov 04 '10 at 16:31
1

It's called recursion

Ken Bloom
  • 57,498
  • 14
  • 111
  • 168
1

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

questborn
  • 2,735
  • 2
  • 16
  • 17
0

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.

Remus
  • 1,433
  • 3
  • 14
  • 24