I need some help with a program I'm writing for my Programming II class at universtiy. The question asks that one calculates the Fibonacci sequence using recursion. One must store the calculated Fibonacci numbers in an array to stop unnecessary repeated calculations and to cut down to the calculation time.
I managed to get the program working without the array and memorization, now I'm trying to implement that and I'm stuck. I'm not sure how to structure it. I've Googled and skimmed through some books but haven't found much to help me solve how to implement a solution.
import javax.swing.JOptionPane;
public class question2
{
static int count = 0;
static int [] dictionary;
public static void main(String[] args)
{
int answer;
int num = Integer.parseInt(javax.swing.JOptionPane.showInputDialog("Enter n:"));
javax.swing.JOptionPane.showMessageDialog(null,
"About to calculate fibonacci(" + num + ")");
//giving the array "n" elements
dictionary= new int [num];
if (dictionary.length>=0)
dictionary[0]= 0;
if (dictionary.length>=1)
dictionary[0]= 0;
dictionary[1]= 1;
//method call
answer = fibonacci(num);
//output
JOptionPane.showMessageDialog(null,"Fibonacci("+num+") is "+answer+" (took "+count+" calls)");
}
static int fibonacci(int n)
{
count++;
// Only defined for n >= 0
if (n < 0) {
System.out.println("ERROR: fibonacci sequence not defined for negative numbers.");
System.exit(1);
}
// Base cases: f(0) is 0, f(1) is 1
// Other cases: f(n) = f(n-1) + f(n-2)/
if (n == 0)
{
return dictionary[0];
}
else if (n == 1)
{
return dictionary[1];
}
else
return dictionary[n] = fibonacci(n-1) + fibonacci(n-2);
}
}
The above is incorrect, the end of my fib method is the main problem. I've no idea how to get it to add the numbers recursively to the correctly parts of the array.
What is Memoization? Memoization is a way to potentially make functions that use recursion run faster. As I'll show in an example below, a recursive function might end up performing the same calculation with the same input multiple times.
Memoization is an enhancement procedure used to speed up computer programs by keeping the values of distinct function calls and returning the stored input when the same function is invoked again.
In mathematics, things are often defined recursively. For example, the Fibonacci numbers are often defined recursively. The Fibonacci numbers are defined as the sequence beginning with two 1's, and where each succeeding number in the sequence is the sum of the two preceeding numbers. 1 1 2 3 5 8 13 21 34 55 ...
Time Complexity: Hence the time taken by recursive Fibonacci is O(2^n) or exponential.
You need to distinguish between already calculated number and not calculated numbers in the dictionary, which you currently don't: you always recalculate the numbers.
if (n == 0)
{
// special case because fib(0) is 0
return dictionary[0];
}
else
{
int f = dictionary[n];
if (f == 0) {
// number wasn't calculated yet.
f = fibonacci(n-1) + fibonacci(n-2);
dictionary[n] = f;
}
return f;
}
public static int fib(int n, Map<Integer,Integer> map){
if(n ==0){
return 0;
}
if(n ==1){
return 1;
}
if(map.containsKey(n)){
return map.get(n);
}
Integer fibForN = fib(n-1,map) + fib(n-2,map);
map.put(n, fibForN);
return fibForN;
}
Similar to most solutions above but using a Map instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With