Question

Java (Please answer in a specific way and not just show the program) Determine the largest...

Java (Please answer in a specific way and not just show the program)

Determine the largest values of n for which the corresponding Fibonacci Numbers can be computed within the given time.

n = _____ time required: less than 10 seconds

n = _____ time required: less than 30 seconds

n = _____ time required: less than 1 minute

n = _____ time required: less than 5 minutes

Estimate the value of n for the largest value of fib(n) that can be computed using recursion in one hour on your computer. Explain in detail how you estimated this value. Also describe any additional data you took to arrive at this value. Attach any additional work required.

n = ______ for time required = 1 hour (3600 seconds)

Repeat this problem for the largest value of n for which the corresponding Fibonacci number can be computed in one day (86,400 seconds).

n = ______ for time required = 1 day (86,400 seconds)

Repeat this problem for the largest value of n for which the corresponding Fibonacci number can be computed in one year.

n = ______ for time required = 1 year.

Repeat this problem for the largest value of n for which the corresponding Fibonacci number can be computed in one million years (106years).

n = ______ for time required = 1 million years.

Memoization

The time complexity is exponential so this simplistic recursive approach becomes intractable when we get into the fifties or so. We can greatly increase our time performance with “memoization” – that is, every time we compute a value we make a note (or memo) of it and check that “memo” list before computing.

Add memoization to your Fibonacci program. At what point does the computation time become more than one second?

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

If n = 0, then fib() should return 0. If n = 1, then it should return 1. For n > 1, it should return Fn-1 + Fn-2

Method 1 ( Use recursion )
A simple method that is a direct recursive implementation mathematical recurrence relation given above.

//Fibonacci Series using Recursion

#include<stdio.h>

using namespace std;   

int fib(int n) {

    if (n <= 1)

        return n;

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

}

int main ()

{

    int n = 9;

    cout << fib(n);

    getchar();

    return 0;

}

Time Complexity: T(n) = T(n-1) + T(n-2) which is exponential.
We can observe that this implementation does a lot of repeated work (see the following recursion tree). So this is a bad implementation for nth Fibonacci number.

Method 2 ( Use Dynamic Programming )
We can avoid the repeated work done in method 1 by storing the Fibonacci numbers calculated so far.

//Fibonacci Series using Dynamic Programming

#include<stdio.h>

int fib(int n)

{

  /* Declare an array to store Fibonacci numbers. */

  int f[n+2];   // 1 extra to handle case, n = 0

  int i;

  /* 0th and 1st number of the series are 0 and 1*/

  f[0] = 0;

  f[1] = 1;

  for (i = 2; i <= n; i++)

  {

      /* Add the previous 2 numbers in the series

         and store it */

      f[i] = f[i-1] + f[i-2];

  }

  return f[n];

}

int main ()

{

  int n = 9;

  printf("%d", fib(n));

  getchar();

  return 0;

}

Method 3 ( Space Optimized Method 2 )
We can optimize the space used in method 2 by storing the previous two numbers only because that is all we need to get the next Fibonacci number in series.

// Fibonacci Series using Space Optimized Method

#include<stdio.h>

int fib(int n)

{

  int a = 0, b = 1, c, i;

  if( n == 0)

    return a;

  for (i = 2; i <= n; i++)

  {

     c = a + b;

     a = b;

     b = c;

  }

  return b;

}

int main ()

{

  int n = 9;

  printf("%d", fib(n));

  getchar();

  return 0;

}

After looking at time complexity for all solutions it was concluded that the recursive solution was getting significantly slower for each additional number in the sequence due to exponential time growth. There is, however, a smart way of improving this solution and that is using Memoization.

“Memoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.”

Let’s define our memoize function. It will receive a function as an argument and also return a function. We will also create an empty object that will actually store function calls for us.

function memoize(fn){
  const cache = {}
  return function(){
  }
}

The function that is returned in memoize needs to receive the same argument n as the ones we’d pass to fib. However if we were to write a reusable memoizer that can be applied to functions other that fib, we would want to allow it to receive any number of arguments like so:

function memoize(fn){
  const cache = {}
  return function(...args){
  }
}

From here we will need to look into our memory bank, our cache constant, and if it contains a key with this set of arguments, we will return the corresponding value.

function memoize(fn){
  const cache = {}
  return function(...args){
    if (cache[args]){
      return cache[args];
    }
  }
}

If the function has never been called with the current set of arguments we will have to pass them to the original “slow” function that is the fn passed to memoize as an argument and then save the result to cache. In order to pass an array of arguments we need to use apply() method.

function memoize(fn){
  const cache = {}
  return function(...args){
    if (cache[args]){
      return cache[args];
    }
    newCall = fn.apply(null, args);
    cache[args] = newCall;
    return newCall;
  }
}

To use memoize function for our Fibonacci problem we simply store memoize function call as a constant and pass arguments that would otherwise be passed to fib to this constant.

const fastFib = memoize(fib);

And, finally, we need to make sure that when fib performs recursive calls, it calls the new fast version of itself rather than the original self.

const fastFib = memoize(fib);
function fib(n) {
  if (n < 2) {
    return n;
  }
  return fastFib(n - 1) + fastFib(n - 2);
}

And that’s it! We have significantly improved the time that it will take the recursive solution to run for large numbers, as it will not be growing exponentially anymore.

The memoizer that was written here is pretty universal and can be used to improve runtime for basically any function.

Add a comment
Know the answer?
Add Answer to:
Java (Please answer in a specific way and not just show the program) Determine the largest...
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
  • PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a...

    PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a function yesOrNo which has no parameters. When called, it gets input from the user until the user types either 'yes' or 'no', at which point the function should return True if the user typed 'yes' and False if the user typed 'no'. Any other entries by the user are ignored and another value must be input. For example: Test Input Result print(yesOrNo()) hello blank...

  • JAVA - the program should output as follows: Please enter a seed: 2345 Please enter the...

    JAVA - the program should output as follows: Please enter a seed: 2345 Please enter the size of the array: 1 Array size must be greater than 1. Please reenter: 0 Array size must be greater than 1. Please reenter: -1 Array size must be greater than 1. Please reenter: 8 Please choose an option: 1 Print the array 2 Find the average 3 Find the largest element 4 Count how many times 3 occurred 5 Count how many elements...

  • Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs...

    Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs the user’s choice of the following functions the program exits should also be a user’s choice. Menu Item 1: Fibonacci number iterator, here you are to define an iterator class named FibonacciIterator for iterating Fibonacci numbers. The constructor takes an argument that specifies the limit of the maximum Fibonacci number. For example, prompt the user for size, use the size to call FibonacciIterator(“user input”)...

  • The answer to a = 0.004144... please continue at b By solving this exercise, you will...

    The answer to a = 0.004144... please continue at b By solving this exercise, you will answer to the following questions: How hot is hell? > When will hell freeze over? In order to help you a bit in solving this problem, I am going to give you some information that will simplify the solution process. As such, we are going to base the problem on a few assumptions: We will assume that hell does actually exist and it obeys...

  • Using java create the following program and post the answer as well. The Weighted Interval Scheduling...

    Using java create the following program and post the answer as well. The Weighted Interval Scheduling problem is this: Given a set of weighted intervals, choose a set of non-overlapping intervals such that the total weight is maximal. You may think of the “weight” as the profit for doing the work in the given interval A weighted interval x can be represented by a triple        x = (s, f, v), where          s = start time of x,    f...

  • Write a C++/Java program to solve the problem below using Memoization: Problem Definition: Theres a theif...

    Write a C++/Java program to solve the problem below using Memoization: Problem Definition: Theres a theif that enters a store during the night,and upon his entrance,the stores security systems detects the theif and the alarm goes off.The theif knows that theres a hidden door at the corner of this store and in order to escape,he has to get to it.Since the theif has limited time for his burglary,he can only Rob the stuff he comes by on his path towards...

  • Consider making a function (mini program to complete a specific task) for each of the scenarios...

    Consider making a function (mini program to complete a specific task) for each of the scenarios below. State what the purpose of the function is in your own words, specify input that is needed by the function, what output is expected from the functions, and the step by step process that will obtain the output from the input (the algorithm). In addition to these 4 items also specify test data that can be used for each problem. Remember to describe...

  • I need this program converted to c++ Please help if you can import java.util.*; // Add...

    I need this program converted to c++ Please help if you can import java.util.*; // Add two arrays of the same size (size) // Each array is a representation of a natural number // The returned array will have the size of (size + 1) elements public class Fibonacci { private static String arrToString(int[] arr) {           String s = "";           for (int i = 0; i < arr.length; i++) {               s = s + arr[i];           }...

  • just explain it in english would be enough. modify the standard definition of a binary search...

    just explain it in english would be enough. modify the standard definition of a binary search tree to add a field N.size at each Suppose that we node, which records the size of the subtree under N'Tincluding N itself). A. Explain how to modify the procedure for adding both the case where X is not yet in the tree and is added, and the case where X is already in the tree, and the tree remains unchanged. element X to...

  • I'm trying to understand how to do the problem. I'm not just looking for an answer....

    I'm trying to understand how to do the problem. I'm not just looking for an answer. Please show the formulas used to solve the problem. If you can, please also explain why we are using that formula. Thank you. 8. Valuing Callable Bonds Assets, Inc., plans to issue $5 million of bonds with a coupon rate of 7 percent, a par value of $1,000, semiannual coupons, and 30 years to maturity. The current market interest rate on these bonds is...

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