PHP Code:
#include <stdio.h>
int power(int num, int n);
void main()
{
printf("%d", power(3, 15));
}
int power(int num, int n)
{
if (n == 1)
{
return num;
}
else
{
int result = 0;
int i = 1;
int t = 0;
while ( i < n ) //find the maximum power of two
{
t = i;
i = i << 1;
}
i = 1;
result = num;
while ( i < t )
{
result = result * result;
i = i << 1;
} //get num^t
i = n - t;
result = result * power(num, i); //Calculate the remaing (n - t)
return result;
}
}
Leave a comment: