I have to write a simple recursion method to calculate m(i) = (1/2) + (2/3) + ... + i/(i+1). I feel like this should be incredibly simple, but I cannot figure it out. I know that I have to loop by decrementing, but I just can’t get it. So far I have the following, which I know is completely wrong.
public class Recursion {
public static void main(String[] args) {
double n = run(10);
System.out.print("Result: " + n);
}
public static double run(double nb) {
double result = 0;
if(nb == 2){
return 1/2;
}
result += run(nb - 2) / run(nb - 1);
return result;
}
}
Use this recurrence relation:
In code, it might look like this:
Note that there’s no need for the argument to be floating point, just the return value.