java - How can I "unroll" this sigma notation as a recursive method -
in java programming class, have lab assignment (not worth points) implement several recursive methods. have completed recursive method based on given recursive function, , have completed necessary factorial recursive method remaining portion, sigma series having hard time wrapping head around.
we given formula:
s(n) = sigma[(s(n - i) - 1) / i!, = 1, n] , s(0) = 0
and have written out results s(1)-s(5) (using graphing calculator verify answers go), having difficulty figuring out how correctly implement recursive process.
i have built "sigma" method works appropriately best of knowledge, , think have issues "formula" method. worst of all, formula looks right (to best of thinking) , code getting stuck in infinite loop.
// ... rest of code omitted brevity private static double sequence2(int i) { if (i <= 0) { return 0; } return (sequence2(max - i) - 1) / factorial(i); // max defined in other code } private static double sigma(int n) { if (n <= 0) { return 0; } return sequence2(n) + sigma(n - 1); } private static int factorial(int n) { if (n <= 1) { return 1; } return n * factorial(n - 1); }
where should begin figuring out how correctly unroll recursive sequence?
according formula you've linked recursion should be(changing name of variables correlate) :
private static double sequence2(int n) { if (n == 0) { return 0; } return (sequence2(n-1) - 1) / factorial(n-1); }
this shall generate sequence in reverse order s(n-1), s(n-2) ... s(1)
Comments
Post a Comment