Write an O(log2(N)) algorithm to find X^N?

Submitted by: Administrator
int computeXn(int x, int n)

{

if(n == 2)

{

return x*x;

}

else if(n % 2 == 0)

{

int y = computeXn(x, n/2);

return y*y;

}

else if(n % 2 == 1)

{

int y = computeXn(x, n/2);

return y*y*x;

}

}
Submitted by: Administrator

Read Online Programming Concepts Job Interview Questions And Answers