Question

In: Computer Science

if this is considered more than one question then please explain part 'I' (Find the exact...

if this is considered more than one question then please explain part 'I' (Find the exact value of f100, f500, and f1000, where fn is the nth Fibonacci number. What are times taken to find out the exact values?). I am having trouble writing the code for both recursive and iterative functions without having the program crash. I assume its because the numbers are so high.

Goal: The main goal of the project is to let students use their prior knowledge, try to use the skills they have learnt to solve real world problems. Using this assignment students are required to learn to use the skills they have learned in their previous classes, solve the problems given and reflect on how they can apply it to solve real world issues.

Deliverables: The students are required to submit a written report along with the programs they have written. The report has to address all the prompts mentioned under the Report section. The report should be properly presented by using appropriate font (Time new roman, preferably font size 12 till 16) and grammar.

Tasks:

  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 n and 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.
  1. Find the exact value of f100, f500, and f1000, where fn is the nth Fibonacci number. What are times taken to find out the exact values?
  2. Find the smallest Fibonacci number greater than 1,000,000 and greater than 1,000,000,000.
  3. 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.

Report: Your report should address all of the below mentioned questions

  1. Describe the Fibonacci series and write briefly what you have done in the assignment.
  2. What are the different skills, programming techniques have you used in order to run the experiments?
  3. 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?
  4. List at least three different applications of the Fibonacci numbers to sciences and describe one of them in details. Think of situation or a real world problem where you can apply concept of Fibonacci numbers to solve it. Explain?
  5. 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? How can you use what you have learned going forward?

Solutions

Expert Solution

Hi... Greetings for the day!

Below is the code for Fibonacci numbers with both using recursion and iteration.

Don't use online compilers for executing the program as those will give timeout errors as they are designed for some load. Use the compiler installed on your system.

Note: Execute only one function at a time with only one input for capturing time taken for each function execution cycle.

To avoid crashing the program, use eclipse or intellij IDEs for better coding and measuring execution times.

Please understand the note mentioned below the program.


#include<bits/stdc++.h>
using namespace std::chrono;
using namespace std;

long long fibRec(int n)
{
   if (n <= 1)
       return n;
   return fibRec(n-1) + fibRec(n-2);
}

long long fibItr(int n)
{
long long previous = 1;
long long current = 1;
long long next = 1;
for (int i = 3; i <= n; ++i)
{
next = current + previous;
previous = current;
current = next;
}
return next;
}

int main ()
{
   int n = 10; //100, 500, 1000
   auto start = high_resolution_clock::now();
   cout << fibRec(n) << " ";
//cout << fibItr(n) << " ";
   auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);

cout << duration.count() << " milliseconds" << endl;     
   getchar();
   return 0;
}


Outputs:

For n=10:

Iterative function:

55
19 milliseconds

Recursive function:

55
17 milliseconds

For n=20:

Iterative function:

6765
25 milliseconds

Recursive function:

6765
99 milliseconds

For n=30:

Iterative function:

832040
30 milliseconds

Recursive function:

832040
50424 milliseconds

Important Note:

If you observe the time execution difference recursive function call will take more time and increased drastically with input number. If you want to calculate large numbers it will take years with recursive function. The reason behind this is internally it will maintain call stack for accessing previously calculated function value. It will store all the function calls and their corresponding results till it reaches base condition then it will pop the stack values and then result will be computed. But for iterative method we are updating the values on the go and there is no memory usage for stack and calculation will be fast.

Time complexity for iterative function is O(n) as at max it will go n iterations.

Time complexity for recursive function is exponential approximately 2^n function calls. T(n) = T(n-1)+T(n-2).

See the below diagram for better understanding.

For every recursion call function is initiating 2 times till it reaches base condition 1 or 0.

I hope this is helpful.


Related Solutions

I need more than just one question answered, please... 1. Which component of speech acts is...
I need more than just one question answered, please... 1. Which component of speech acts is the most difficult to determine? a. the linguistic form b. the context of the message c. the effect on the listener d. the intent of the message 2. If you tell a friend about a movie you watched the previous night, you would be engaging in a a. speech act. b. social register. c. narrative. d. conversation. 3. According to Bates, if a child...
JAVA / I KNOW THERE IS MORE THAN ONE QUESTION BUT THEY ARE SO EASY FO...
JAVA / I KNOW THERE IS MORE THAN ONE QUESTION BUT THEY ARE SO EASY FO YOU I NEED YOUR HELP PLEASE HELP ME. I WILL GIVE UPVOTE AND GOOD COMMENT THANK YOU SO MUCH !!! QUESTION 1 Consider the following program segment: int i = 2; int n = 18; while (i < n) {     if ((i % 2) == 0) i++; else n--; } System.out.println( i ); What is the value of n when the above program...
QUESTION 73 Gluconeogeneis is _________________. A. The exact opposite of glycolysis. B. More energetically favorable than...
QUESTION 73 Gluconeogeneis is _________________. A. The exact opposite of glycolysis. B. More energetically favorable than glycolysis. C. Energetically more costly than glycolysis. D. The amount of energy to perform gluconeogenesis is equal to the amount of energy in metabolizing glucose. QUESTION 72 Electrons extracted from fatty acids in the peroxisomes are transferred to __________________. A. NAD+ B. molecular oxygen C. cytochrome C D. Quinones QUESTION 65 What type of enzyme is required to catabolize an unsaturated fatty acid but...
Hi, I am not able to find this exact question with the steps and correct answers....
Hi, I am not able to find this exact question with the steps and correct answers. Can someone please solve this? Thanks! Suppose you have been hired as a financial consultant to Defense Electronics, Inc. (DEI), a large, publicly traded firm that is the market share leader in radar detection systems (RDSs). The company is looking at setting up a manufacturing plant overseas to produce a new line of RDSs. This will be a five-year project. The company bought some...
Keep in mind that each question has more than one part. Be sure to answer all...
Keep in mind that each question has more than one part. Be sure to answer all parts of the quest. Make certain that you cite sources appropriately. If you do indeed cite a source, explain it – explain why is helps answer the particular question. Explain what resources, competencies, capabilities are, and how they can be used to create value (not the value chain) and competitive advantage. Be sure to consider capabilities and how do they affect resources and competencies....
Each part should be no more than 1 page in length. Part I The rules of...
Each part should be no more than 1 page in length. Part I The rules of accounting provide management with “some” latitude in determining when revenue is earned. Assume that a company normally required acceptance by its customers prior to recording revenue as earned, delivers a product to a customer near the end of the quarter. The company believes that customer acceptance is assured, but cannot obtain it prior to the quarter-end. Recording the revenue would assure “making its numbers”...
Is California a Democratic state? Why or why not? please, I need more than one sentence,...
Is California a Democratic state? Why or why not? please, I need more than one sentence, should be several paragraph. With arguments.
I know this is more than one question. I'm hoping you can address these all at...
I know this is more than one question. I'm hoping you can address these all at once? How are the ways in which organizations choose to measure and evaluate the performance of their segments tied to how managers of those segments get evaluated? What are the ways those approaches can fairly evaluate managers? How can those approaches sometimes unfairly evaluate managers? Can people "game" this system? If so, how? What can be done to ensure both accurate segment performance evaluation...
please provide one-Page (no more than 1-page) discussion on this question: explain why President Trump/US government,...
please provide one-Page (no more than 1-page) discussion on this question: explain why President Trump/US government, in helping corporations, cannot buy company stocks but only buy corporate debts or simply give "grants" to corporations? what if the government bought stocks?
China is considered as one of the most borrowing countries to USA with more than $2...
China is considered as one of the most borrowing countries to USA with more than $2 thousands Billions. why China does not think to sell the debt back to USA?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT