How to explain this algorithm for calculating the power of a number?

后端 未结 6 1756
生来不讨喜
生来不讨喜 2020-12-15 01:12

I am currently reading Skiena\'s \"The Algorithm Design Manual\".

He describes an algorithm for calculating the power of a number i.e. calculate a^n.

6条回答
  •  情深已故
    2020-12-15 02:10

    This function is recursive, meaning that when called, it is going to call itself over and over again until some final condition is met. In this case, there are three final conditions that will stop the function from calling itself and return the result.

    If I were you I would try to apply the algorithm manually on a couple of different values in order to understand.

提交回复
热议问题