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: 3819297 • 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. I. Find the exact value of f_1000, f_500, and f_1000, where fn 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.

Explanation / Answer

Hi, I have answered your first 2 questions with c++ code below:

1.Fibonacci number using Iterative function:

#include <iostream>

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;

}

2.Fibonacci number using Recursive function:

#include <iostream>

ll fibo_recur(int n)

{

    if (n == 1 || n == 2)

        return 1;

    else

        return fibo_recur(n - 1) + fibo_recur(n - 2);;

}

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_recur(n)<<endl;

    }

    return 0;

}

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