The Fibonacci sequence is the series of integers 0,1, 1, 2, 3, 5, 8,13, 21, 34,
ID: 3594147 • Letter: T
Question
The Fibonacci sequence is the series of integers 0,1, 1, 2, 3, 5, 8,13, 21, 34, 55,89 See the pattern? Each element in the series is the sum of the preceding two items. There is a recursive formula for calculating the nth number of the sequence (the oth number if Fib(0)-0): FD(N) = / N, if N-0 or 1 if N> 1 Fib(N-2) Fib(N-1), a. Write a recursive version of the function Fibonacci b. Write a nonrecursive version of the function Fibonacci c. Write a program to test the recursive and iterative versions ofthe function Fibonacci. (Test the N for different values, such as n-5, 10, 30, 40, 50.)Explanation / Answer
#include<iostream>
#include<time.h>
using namespace std;
int Fibr(int n){
if (n==0 || n==1)
return 1;
else
return(Fibr(n-1) + Fibr(n-2));
}
unsigned long Fibnr(int n){
unsigned long result[1000];
result[0] = 1;
result[1] = 1;
for (int i = 2; i<=n; i++){
result[i] = result[i-1] + result[i-2];
}
return result[n];
}
int main(){
cout << Fibr(5) << " " << Fibr(10) << " " << Fibr(30) << " " << Fibr(40) << " " << Fibr(50) << endl;
cout << Fibnr(5) << " " << Fibnr(10) << " " << Fibnr(30) << " " << Fibnr(40) << " " << Fibnr(50) << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.