As you may know (or not), that the easiest way to find the Nth
number of the Fibonacci Sequence is to use recursive function
It looks like this
[code]
#include <stdio.h>
long fib(int num) {
Printable View
As you may know (or not), that the easiest way to find the Nth
number of the Fibonacci Sequence is to use recursive function
It looks like this
[code]
#include <stdio.h>
long fib(int num) {
Recursion often has other benefits that can make up for lost performance. In particular, the readability and elegance of the code, and maintainability.
If the body of the function is heavy, then the function-call overhead can quickly become insignificant. If the function body is small and light, as in your Fibonacci example, then the overhead for the recursive calls can be significant in comparison.
hmm.....
i thought that there would be a little bug in case
" num = 0 " & "num less than 0"
so, i suggests that it would be
" if(num<2)return num; " hence, the error gonna be debuged.
^^
infinity loop
if num=2Quote:
long fib(int num) {
if(num==1) return 1;
else {
return fib(num-1) + fib(num-2);
}
}[/b]
then return fib(1)+fib(0)
after that fib(0) cause [b]fib(-1) and fib(-2)[b]
and so on...