#include <stdio.h>
#include <stdlib.h>
double power(double x, int n)
{
double value, result;
printf("call power(%lf, %d) \n", x, n);
if (n == 0) {
printf("return 1\n");
return 1;
}
else if ((n % 2) == 0) {
value = power(x*x, n / 2);
printf("return %lf \n", value);
return value;
}
else {
value = power(x*x, (n - 1) / 2);
result = x * value;
printf("return %lf * %lf \n", x, value);
return result;
}
}
int main(void)
{
double x, value_slow, value;
int n;
x = 2;
n = 10;
value = power(x, n);
printf("power(%lf, %d) = %lf \n", x, n, value);
}