Sunday, February 9, 2014

Recursive Complexity - Fibonacci

The factorial - a simple recursive

N! = N * (N-1) * (N-2) * .....*2 * 1

This is a very simple recursive program:

int Factorial(int n){

if(n == 0){

return i;

}else{

return n * Factorial(n-1);

}

}

Not all Recursive method are very efficient, such as the recursive in Fibonacci:

Fibonacci: F(n) = F(n-1) + F(n-2), n > 1

1       We can see that we have to calculate each F(n) for many times, for example, if we want to get F(5), we have to calculate F(3) for two times, and calculate the F(2) for three times.

So for some aspects, recursive is not always a good way, it depends on the problems you want to solve.

 

 

No comments:

Post a Comment