1

I had this question sent to me by a potential employer, and my answer apparently wasn't up to snuff. Can anyone help?

Implement a function that calculates power(A,B), where A and B are positive integers, assuming there is no power function in your programming language. Also, assume A and B are of Big Integer type so that there is no arithmetic overflow. What is the computational complexity of your function? Can you come up with a solution that runs with log(B) time?

1 Answers1

2

I'll just give you a hint. Suppose B is 45. Then A45 = A32 * A8 * A4 * A1. You can compute A1, A2, A4, A8, etc. by starting with A and squaring it in every iteration.

ajb
  • 31,309
  • 3
  • 58
  • 84