From the course: Python: Recursion

Unlock the full course today

Join today to access over 24,200 courses taught by industry experts.

Recursive exponentiation algorithm in Python

Recursive exponentiation algorithm in Python - Python Tutorial

From the course: Python: Recursion

Recursive exponentiation algorithm in Python

- [Instructor] This is another practice exercise for recursion, raising one number to the power of another, sometimes called exponentiation. Here, we are considering positive numbers only. So, as before, make sure you understand what is required. Have a look at the test. This will give you a very clear idea of what the input and output should be. And then have a go at completing the algorithm for yourself, remembering to include a base case and the other two laws of recursion. Before you start, though, I'm going to show you the iterative version, just to give you an idea of the ingredients involved. So, for the iterative one, I'm going to set the base equal to a and then I'm going to do for i in her range of n minus one. I am going to do a times equals, that's shorthand for a equals a times base. So, I multiply the base by the base and the minus one times and that will give me the result. Okay, so, let's just run it but…

Contents