pow(2,3) ==> 8.0
Recursion steps :
1. If the value of n is 0
, return 1
.
2. If the value of n is 1
, return x
.
3. If the value of x is 0
, return x
.
4. If n is less than 0, x
becomes 1/x
and n
becomes -n.
5. If n%2 > 0
, return x*pow(x*x, n/2)
.
6. Else return pow(x*x,n/2)
public static double pow(double x, int n) { }
C
Java
Python