I'm doing some homework and I'm struggling with a specific question. There is a similar question in my assignment so I need to get the hang of this.
Here's the code:
public static double power2(double base, int n) {
switch (n) {
case 1:
return base;
case 2:
return base * base;
default:
if (n % 2 == 0) /* n is even */ {
return power2(power2(base, n / 2), 2);
} else /* n is odd */ {
return power2(power2(base, n / 2), 2) * base;
}
}
}
I have the base case, which I believe to be 0, n=1; However, getting to T(n) is where I'm struggling.
It needs to be similar T(n-1)+c, n>1.
I need to express the code with a recursive formula.
Can anyone ELI5 this for me?