Answers

Question and Answer:

  Home  Programming Concepts

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

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;

}

}

 116 views

More Questions for you: