Recursive method for x^n optimised for when n is even
It’s exactly the same principle as for x^n == x*(x^(n-1)): Insert your recursive function for x^(n/2) and (…)^2, but make sure you don’t enter an infinite recursion for n == 2 (as 2 is even, too): if (n % 2 == 0 && n > 2) return power(power(x, n / 2), 2); } Alternatively, you … Read more