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
  • CMPS 290 Programming Assignment Arrays and Recursion – Fibonacci Numbers In this programming assignment you will...

    CMPS 290 Programming Assignment Arrays and Recursion – Fibonacci Numbers In this programming assignment you will be working with arrays and multiple function calls, including a recursive function call. The goal of the assignment will be to, using a pre-set array of sequence numbers, calculate the Fibonacci sequence number for each value in the array (see the example at the bottom if this isn’t clear). Instructions and Requirements: • Create a program that assembles and runs to find the correct...

  • 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...

  • How would I make it so these are dialog boxes instead of just like a regular...

    How would I make it so these are dialog boxes instead of just like a regular question? Here are the instructions. "Ask the user to input a number.  You must use an input dialog box for this input. Be sure to convert the String from the dialog box into an integer (int). The program needs to keep track of the smallest number the user entered as well as the largest number entered. Use a Confirm dialog box to ask the user...

  • 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...

  • Which of the following statements is true? The best way to study algorithm efficiency is to...

    Which of the following statements is true? The best way to study algorithm efficiency is to see how many milliseconds it takes to execute a program All O(log n) sort algorithms will use the same number of CPU cycles to sort identical arrays Big O notation is not useful for estimating the specific number of CPU cycles an algorithm will require A program will take the same number of millseconds to execute an algorithm each time it is run for...

  • NOTE: You are NOT required to write a program for this problem. We wish to determine...

    NOTE: You are NOT required to write a program for this problem. We wish to determine whether at least one three-of-a-kind exists in a hand of N cards, where a three-of-a-kind is three of the same value. Assume the cards have values 1 – 13. For example, the hand with card values {11, 5, 11, 5, 5} does have at least one three-of-a-kind. For each of the following algorithms (A and B), find the simplified total Big-O run time as...

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