Question: When I testing the code by @Test public void fibonacci() { // when we call calculate with x = 2, it's a fibonacci function assertEquals
When I testing the code by
@Test public void fibonacci() { // when we call calculate with x = 2, it's a fibonacci function assertEquals(233, RecursiveFibonacci.calculate(13, 2)); }
It keeping saying java.lang.StackOverflowError. I think It because of this line 'output += calculate(n - i, x);' Can anyone help me fix it, thanks!

public class RecursiveFibonacci f ok * Fibonacci function with a twist. * Consider the normal fibonacci function : F(n) = F(n-1) + F(n-2). * That function can be represented here as F(n)-calculate(n, 2). k The concept here is that calculate(n, x) = calculate(n-1, x) + calculate(n-2, x) + calculate(n-3, x) + +calculate(n -x, x) k @param n The number to calculate the special fibonacci of. * @param x sk The number of recursive terms added to create the next term. arequire x 1 public static int calculate(int n, int x) return 1; else f int output = 0; for (int i-l; ?cx; ++1) { output += calculate( n: n-i, x); return output
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
