“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
There are better ways to find a solution to this problem instead of using Recursion. Recursion is a disaster as someone has already mentioned. Try giving 20 or more elements in your series and your program will take awful lot of time to compute. For every element in series you’re finding the element before using recursion though you theoretically you would have computed it before!!!
to return fib of n (printing takes only minor adjustments):
int fib( int n ){
if n == 0 return 1;
else return fib(n-1) + fib(n-2);
}
Iterative version:
int fibo (int n)
{
int i, current, one_back, two_back;
if (n <= 1)
return 1;
else {
one_back = 1;
two_back = 1;
for ( i = 2; i <= n; i++) {
current = one_back + two_back;
one_back = two_back;
two_back = current;
} /* end for loop */
} /* end else block */
return current;
}
I believe the top most solution would be more efficient if we tested n <=2 rather than checking n==0. This would remove unnecessary iterations.
int fib( int n ){
if (n<=2) return 1;
else return fib(n-1) + fib(n-2);
}
ya, but the sequence is defined as being 0 at the first index, not 1:
0 1 1 2 3 5 8 …
Recursion is a disaster for Fibonacci sequence in terms of run time.
print nMembers elements of the series:
void fib(int nMembers)
{
int num1 = 0;
int num2 = 1;
int cnt = 0;
for (cnt=0; cnt<=nMembers; cnt++)
{
if (cnt <2)
{
printf(”%u,”,cnt);
continue;
}
printf(”%d,”,num2 = num1 + num2);
num1 = num2-num1;
}
}
All the above codes can be reduced to a simple form.
int fib(int n){
return(n==0?1:(fib(n-1)+fib(n-2)))}
There are better ways to find a solution to this problem instead of using Recursion. Recursion is a disaster as someone has already mentioned. Try giving 20 or more elements in your series and your program will take awful lot of time to compute. For every element in series you’re finding the element before using recursion though you theoretically you would have computed it before!!!
int fibo(int n)
{
if(n==1
void fibo(int n){
int a=-1, b= 1;
while(nâ-){
printf(â%d\tâ,b+=a);
a = b-a;
}
}
void fib(int max) {
int x = 0;
int y = 1;
printf(”%d\t”, b = x);
for(int i = 1; i *less then* max; i++){
printf(”%d\t”, b = y);
int temp = y;
y = x + y;
x = temp;
}
}
> int fib( int n ){
> if n == 0 return 1;
> else return fib(n-1) + fib(n-2);
> }
This program will NOT work at all!! Just trace it and you will see that fib(1) = 1 + fib(-1), fib(-1) = fib(-2) + fib(-3), …..
> int fib( int n ){
> if (n else return fib(n-1) + fib(n-2);
> }
This one is also is not correct! Using this algorithm you will get fib(3) = 2!! But fib(3) = 2 + 1 = 3!
int fibo(int n)
{
if(n==1 || n==2)
return 1;
return fibo(n-1)+fibo(n-2);
}
Using the closed form:
int fib(int n){
double goldRatio = 0.5 + sqrt(1.25);
double ans1 = pow(goldRatio,n(double))/sqrt(5);
double ans2 = pow(1-goldRatio,n(double))/sqrt(5);
return floatToInt(ans1 - ans2);
}
Better without recursion! If you can, write the simplest
void fibonacci(int initial/*initial value*/, int count)
{
int previous = initial - 1;
for(int i = 0; i
void fibonacci(int initial/*initial value*/, int count)
{
int previous = initial - 1;
for(int i = 0; i
Leave an Answer/Comment