From the course: Python: Recursion

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

The factorial function: Iterative version

The factorial function: Iterative version - Python Tutorial

From the course: Python: Recursion

Start my 1-month free trial

The factorial function: Iterative version

- [Narrator] The factorial function in mathematics, is a function which takes an integer for example five, I'll just show you this as a comment so you can see what's going on. So we take five and then we multiply it by one less and then one less again, and then one less again, all the way down to multiplying it by one. So five factorial would be five times four, times three, times two, times one, which will come out as 120. When learning how to run recursive algorithms, it can be very helpful to implement an iterative version first. This enables us to ensure that we fully understand what is required and also to make sure we're familiar with any elements of our programming language, which we're likely to need in both an iterative and recursive version. Later on this is not so true as when you have some experience you will start to reason about recursive algorithms in purely recursive terms. Now you might want to…

Contents