Fibonacci numbers are often used as an intro into recursion because they are naturally recursive. In fact, implementing them recursively is trivial in any language. On a side note, it is usually not the best way to implement Fibonacci sequence for practical purposes.
By definition, Fib(X) = Fib(X - 1) + Fib(X - 2). This is recursion right there. The only thing which is missing is how we stop the recursion, and we know that Fib(0) is the same as Fib(1) and is 1.
How do we translate this to the C language? Very simple, almost one-to-one mapping!
unsigned int fib(unsigned int k) {
// First, check our exit (stop) conditions:
if (k == 0 || k == 1) return 1;
// Now recursive part
return fib(k - 1) + fib(k - 2);
}
Answer from SergeyA on Stack OverflowFibonacci sequence using recursion in C - Stack Overflow
Fibonacci without recursion in C: stack smashed?
Can Fibonacci numbers be calculated using recursion in O(N) without memo?
[C++] Converting Fibonacci from Recursion to Tail Recursion
Tail recursion requires that recursive calls you make are immediately returned, and that you don't do any processing of the return value of a recursive call.
In your regular function, fib() is called twice and added together. Think about how you would modify the signature of fib so you only have to call it once (and as a consequence, the runtime would be made linear instead of exponential).
Once that's done, it's easy to make it tail recursive.
edit: I originally said there can be only one recursive call and it has to be at the bottom to make a function tail recursive which is incorrect. The only requirement is that a recursive call is returned immediately.
More on reddit.comIs there any disadvantage of using the recursive method in calculating the Fibonacci series in C?
What’s the easiest way to understand and calculate the Fibonacci series in the C language?
Is zero (0) considered a Fibonacci number?
Videos
Fibonacci numbers are often used as an intro into recursion because they are naturally recursive. In fact, implementing them recursively is trivial in any language. On a side note, it is usually not the best way to implement Fibonacci sequence for practical purposes.
By definition, Fib(X) = Fib(X - 1) + Fib(X - 2). This is recursion right there. The only thing which is missing is how we stop the recursion, and we know that Fib(0) is the same as Fib(1) and is 1.
How do we translate this to the C language? Very simple, almost one-to-one mapping!
unsigned int fib(unsigned int k) {
// First, check our exit (stop) conditions:
if (k == 0 || k == 1) return 1;
// Now recursive part
return fib(k - 1) + fib(k - 2);
}
Almost every recursion function contains two part : the particular part and then the recursive part.
So to write this function your algorithm will look like this
if (condition_separate_particular_part)
{
//here the code for part
}
else
{
//the recursive part
}
Now to determine the recursive part you will try to find how to explain the element "i" using it's predecessor elements "i-1" "i-2" ....
Like this it will be easy for you every time.
Note that sometimes it's useful to start to find iterative way to make it easy for your self.
I'm damn sure that you are new so that's why you feel lost a bit. But trust me you will habit soon if you exercise more. Try and you gonna see. ;).
Let me know if you find difficulties to find more exercises/examples. I will try to help you :).
Here is and example. Just type Exercise with solution for recursive functions on C and you will find a lot to exercise ;).
Now here both recursive and iterative code
Recursive
int fib(int n){
if (n < 2) // here is particular case
return n;
else // here is the recursion
return fib(n-1) + fib(n-2);
}
printf("%d\n", fib(10));
iterative
int fib(int n) {
int first = 0, second = 1;
int tmp;
while (n--) {
tmp = first+second;
first = second;
second = tmp;
}
return first;
}