Question
* PROBLEM 5: The implementation of terribleFibonacci is TERRIBLE! Write a more efficient version of fibonacci. Do not change runFibonacciLoop or runFibonacciSomeValues. To make fibonacci
* PROBLEM 5: The implementation of terribleFibonacci is TERRIBLE! Write a more efficient version of fibonacci. Do not change runFibonacciLoop or runFibonacciSomeValues. To make fibonacci run faster, you want it so that each call to fibonacci(n) computes the fibonacci numbers between 0 and n once, notover and over again.
*
* Comment: You will want to use a local variable of type "long" rather than
* type "int", for the reasons discussed above.
*
* Comment: At some point, your fibonacci numbers might become negative.
* This is normal and expected.
* http://en.wikipedia.org/wiki/Integer_overflow We discuss this at length
* in our systems classes.
*
* You may not use any "fields" to solve this problem (a field is a variable
* that is declared "outside" of the function declaration --- either before
* or after).
*/
public static void runFibonacciLoop () {
for (int N = 0; N < 100; N++)
StdOut.format ("fibonacci(%2d)=%d ", N, fibonacci (N));
}
public static void runFibonacciSomeValues () {
StdOut.format ("fibonacci(%2d)=%d ", 13, fibonacci (13));
StdOut.format ("fibonacci(%2d)=%d ", 7, fibonacci (7));
StdOut.format ("fibonacci(%2d)=%d ", 21, fibonacci (21));
}
public static long fibonacci (int n) {
return 0; // TODO
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started