Computing Power in c++ powerof(x , n)
This function takes log(n) time and auxilury space is big O 1;
#include <iostream>
long long Powerof(int x, int n)
{
long long result = 1;
while (n > 0)
{
if (n % 2 != 0)
{
result = result * x;
}
x = x * x;
n = n / 2;
}
return result;
}
int main()
{
std::cout<<Powerof(4, 5);
return 0;
}
0 Comments