PDA

View Full Version : Try to solve Fibonacci Sequence in common way.



Karun
15-08-2008, 09:44 PM
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) {

Edkung_
16-08-2008, 09:03 AM
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.

nanonez
22-12-2008, 11:41 PM
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.

^^

retool2
30-12-2008, 12:26 PM
infinity loop

long fib(int num) {
if(num==1) return 1;
else {
return fib(num-1) + fib(num-2);
}
}[/b]
if num=2
then return fib(1)+fib(0)
after that fib(0) cause [b]fib(-1) and fib(-2)[b]
and so on...