Question

long fib(int n) { long result;    if (n==0) result = 0; else if (n==1 ||...

long fib(int n)
{
long result;
  
if (n==0)
result = 0;
else if (n==1 || n==2)
result = 1;
else
result = fib(n-1) + fib(n-2);
  
return result;
}

[C language]

Question : Why is this code is so slow? Explain

ex2)

Make the program from exercise 1 faster by trading space for time. Create an array of int that can be used as

a cache. Each index in the array should correspond to a value that you can compute once, then quickly return.

Pick two different sizes for your cache (one small, one large, you’re going to need to figure out what reasonable values for small and large are on your own). Run the same experiment that you did in exercise 1 for both the small cache and the large cache and plot the results in the same way.

Report:

Upload the updated chart. Make a note in any empty cell why the program runs faster (“Because we’re trading space for time” is not a good enough answer…) and explain how the size of the cache affects the runtime.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

please upvote.

Add a comment
Know the answer?
Add Answer to:
long fib(int n) { long result;    if (n==0) result = 0; else if (n==1 ||...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT