Part 1 – Implement the Recursive Version of the Fibonacci Function implement a r
ID: 3630628 • Letter: P
Question
Part 1 – Implement the Recursive Version of the Fibonacci Functionimplement a recursive version of a function to calculate a given Fibonacci number if you are given the population of the first two generations. Remember to include both a base case and a recursive case in your function design.
Part 2 – Another Way to Go About Things
Now, implement the following versions of the calculation:
Version 1 – Implement an Iterative Version of the Fibonacci Function
Implement an iterative version of the function to calculate a given Fibonacci number.
Version 2 – Implement a Direct Calculation for the Fibonacci Function
Binet’s Formula allows you to calculate the nth number of the Fibonacci Sequence directly.
I've created a program that calculates the Fibonacci any number of the Fibonacci sequece, but im pretty sure it not what the program is asking for..
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
int fib(int,int,int);
int main()
{ int first,second,next;
int n;
cout<<"Enter the first generation number: ";
cin>>first;
cout<<"Enter the second generation number: ";
cin>>second;
cout<<"Enter the order of the Fibonacci number: ";
cin>>n;
next=fib(first,second,n);
cout<<"The "<<n<<" th order Fibonacci number is "<<next<<" ";
system("pause");
return 0;
}
int fib(int first,int second,int n)
{if(n==0)
return first;
else if(n==1)
return second;
else return fib(first,second,n-1)+fib(first,second,n-2);
}
------------------------
#include <iostream>
#include <math.h>
using namespace std;
int fib(int);
int main()
{ int next;
int n;
cout<<"Enter the order of the Fibonacci number: ";
cin>>n;
next=fib(n);
cout<<"The "<<n<<" th order Fibonacci number is "<<next<<" ";
system("pause");
return 0;
}
int fib(int n)
{
return (int)((pow(1.+sqrt(5),n)-pow(1.-sqrt(5),n))/(pow(2.,n)*sqrt(5)));
}
#include <iostream>
using namespace std;
int fib(int,int,int);
int main()
{ int first,second,next;
int n;
cout<<"Enter the first generation number: ";
cin>>first;
cout<<"Enter the second generation number: ";
cin>>second;
cout<<"Enter the order of the Fibonacci number: ";
cin>>n;
next=fib(first,second,n);
cout<<"The "<<n<<" th order Fibonacci number is "<<next<<" ";
system("pause");
return 0;
}
int fib(int first,int second,int n)
{int i,next;
for(i=2;i<=n;i++)
{next=first+second;
first=second;
second=next;
}
return next;
}