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

Write an iterative C++ function that inputs a nonnegative integer n and returns

ID: 3816724 • Letter: W

Question

Write an iterative C++ function that inputs a nonnegative integer n and returns the nth Fibonacci number. Write a recursive C++ function that inputs a nonnegative integer n and returns the nth Fibonacci number. Compare the number of operations and time taken to compute Fibonacci numbers recursively versus that needed to compute them iteratively. Use the above functions to write a C++ program for solving each of the following computational problems l. Find the exact value of f_100, f_500, and f_1000, where f_n is the nth Fibonacci number. What are times taken to find out the exact values? II. Find the smallest Fibonacci number (1)greater than 1,000,000, and (2) greater than 1,000,000,000. III. 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 conclusion. 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 assignment. 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.III? 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 problems?

Explanation / Answer

1. /* C++ Program to Find Fibonacci Numbers using Iteration */

#include <cstring>

#include <iostream>

#include <cstdlib>

#define ll long long

using namespace std;

ll fibo_iter(int n)

{

    int previous = 1;

    int current = 1;

    int next = 1;

    for (int i = 3; i <= n; ++i)

    {

        next = current + previous;

        previous = current;

        current = next;

    }

    return next;

}

int main()

{

    int n;

    while (1)

    {

        cout<<"Enter the integer n to find nth fibonnaci no.(0 to exit): ";

        cin>>n;

        if (n == 0)

            break;

        cout<<fibo_iter(n)<<endl;

    }

    return 0;

}

3. In both implementations I assumed a correct input i.e. n >= 1. The first code is much longer but its complexity is O(n) i.e. linear, while the second implementation is shorter but has exponential complexity O(fib(n)) = O(^n) ( = (1+5)/2) and thus is much slower. One can improve the recursive version by introducing memoization(i.e. remembering the return values of the function you have already computed). This is usually done by introducing an array where you store the values.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote