Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ccess Apps M (1,405) saki... LaGuardia Commun... Apple D Disney MInbox 1. Write

ID: 3822190 • Letter: C

Question

ccess Apps M (1,405) saki... LaGuardia Commun... Apple D Disney MInbox 1. Write an iterative C++ function that inputs a nonnegative integer n and returns the nth Fibonacci number. 2. Write a recursive C++ function that inputs a nonnegative integer nand returns the nth Fibonacci number. 3. Compare the number of operations and time taken to compute Fibonacci numbers recursively versus that needed to compute them iteratively. 4. Use the above functions to write a C++ program for solving each of the following computational problems l. Find the exact value of foo, tsoo, and fiooo, where fr is the nth Fibonacci number. What are times taken to find out the exact values? ll. Find the smallest Fibonacci number (1)greater than 1,000,000, and (2) greater than 1,000,000,000. Ill. Find as many prime Fibonacci numbers as you can. It is unknown whether there are infinitely many of these. Find out the times taken to find first 10, 20.30, 40...up to 200 and draw a graph and see the pattern. Initial report submission to "Discussions", due on Sunday April 16: 1. Prepare a project report in WORD document to describe how you implement above tasks, including (1) problem analysis and solution plan, (2) source code, (3) discussion on experimental results in tables or graphs, (4) reflection and 2. Your reflection should address all of the below mentioned questions a) Describe the Fibonacci series and write briefly what you have done in the b) What are the different skills, programming techniques have you used in order to run the experiments? c) What did you observe when you did the comparisons in Task 3 and Task 4? Explain the graph that you have drawn from Task 4.lll? d) List at least three different applications of the Fibonacci numbers and describe one them details. Think of situation or a real world problem where you can apply concept of Fibonacci numbers to solve it Explain? e) write a paragraph, explaining what you have done in this assignment. What were the challenges you have faced when solving these problems How can you improve the programs you have written to solve these

Explanation / Answer

Solution:-

The below given code will take input a number n and return the nth Fibonacci number. This program can calculate nth Fibonacci number by iterative approach and recursive approach.

Below given code is solution for questions (1) and (2) as it described the iterative and Recursive approach to calculate nth Fibonacci number.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

include<iostream>
#include <ctime>

using namespace std;

/* Fibonacci Series: recursive version */
int Fibonacci_Recursive(int n)
{
if(n<=0) return 0;
   else if(n==1) return 1;
   else return Fibonacci_Recursive(n-1)+Fibonacci_Recursive(n-2);  
}

// Fibonacci Series: iterative version
int Fibonacci_Iterative(int n)
{
   int fib[] = {0,1,1};
   for(int i=2; i<=n; i++)
   {
       fib[i%3] = fib[(i-1)%3] + fib[(i-2)%3];
       //cout << "fib(" << i << ") = " << fib[i%3] << endl;
   }
   return fib[n%3];
}

int main(void)
{      
   int n;
   int long recursiveResult = 0;
   int long iterativeresult = 0;
   cout << "n = ";
   cin>>n;
  
   if(n < 0){
   cout << " kindly enter nonnegative integer :" << endl;
   return 0;
   }
  
   const long double sysTime = time(0);
const long double sysTimeMS = sysTime*1000;
cout << "System Time in milliseconds is " << sysTimeMS << "." << endl;
  
   // calculate the fib(i) from scratch for each i <= a using your recursive function
   cout << "nth Fibonacci number From recursive technique : " << "fib(" << n << ") = " << Fibonacci_Recursive(n) << endl;
   cout << endl;

const long double endSysTime = time(0);
const long double endSysTimeMS = sysTime*1000;
cout << " Recursive approch done " << endSysTimeMS << "." << endl;
  
cout << "Time taken by recursive approch " << endSysTimeMS - sysTimeMS << endl;

   // or calculate fib(a) once and output the intermediate results from the looping version
   cout << "Nth Fibonacci number from iterative technique : " << Fibonacci_Iterative(n) << endl;
   cout << endl;
  
   const long double end2SysTime = time(0);
const long double end2SysTimeMS = sysTime*1000;
cout << "Iterative approch done " << end2SysTimeMS << "." << endl;
  
cout << " Time difference between both the approch :" << end2SysTimeMS - endSysTimeMS << endl;
  
   return 0;
}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

(3) Time complexity of Iterative method to calculate nth Fibonacci number :-

The operation comparison of iterative and Recursive function for Fibonacci number is given below.

As per operation comparisons we can observe that iterative method is far better than recursive method. Recursive method is a function of itself and Everytime it calculate the itself recursively. So for f(1) it is simple but for f(10) it will call itself 9 times and Everytime it will calculate itself from start, as for f(n) it calculate f(n-1) Everytime. On the other hand iterative function is a loop which does only one Operation at an iteration a single iteration is a single execution of a process.

The time complexity of Recursive method is O (2^n) that is exponential and iterative method has time complexity is O (n) that is linear. So it is very slow comparatively iterative method.

The space complexity is same for Both approaches that is O(n).

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

(4)

(I) The exact value of f100, f500 and f1000 are cal calculated nyby iterative approach and running time difference is given below -

f100 = 3736710778780434371 Iterative approch done 1492259751000 ms.

f500 = 2171430676560690477 Iterative approch done   1492259780000 ms

f1000 = 817770325994397771 Iterative approch done   1492259800000 ms

Time difference = f500 - f100 = 29000 ms or 29 sec
Time difference = f100 - f500 = 1492259800000 - 1492259780000 = 20000ms

(II) The smallest Fibonacci number (1) greater than 1,000,000, and (2) greater than 1,000,000,000.

31th element in series would be greater than 1,000,000 - 1346269
45th element in series would be greater than 1,000,000,000 - 1134903170

(III) The below given program returns the Fibonacci series of prime numbers less than 10000.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Note : in above code just take long long data type instead of int and you can get any nth number in series.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote