From the course: Programming Foundations: Algorithms

Unlock the full course today

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

Calculating power and factorial

Calculating power and factorial - Python Tutorial

From the course: Programming Foundations: Algorithms

Calculating power and factorial

- [Instructor] In this example, we'll put recursion to work to solve a couple of math problems. We're going to build the power and factorial functions. So let's open up recursion_start. Okay, so first, a very quick review of some basic math. The power function calculates the value of a number multiplied by itself a certain number of times. For example, two to the fourth power is 16. So that's two times two times two times two. The factorial function is written as a number followed by an exclamation point, and is equal to that number multiplied by each of the numbers that comes before it. So that's five. Factorial is five times four, times three, times two, times one, which is 120. And there's a special case which zero factorial is one because, well, you know, math. So let's write the code to implement these functions using recursion, and first we'll tackle the power function. So remember, we're multiplying an argument a…

Contents