Search⌘ K

The factorial function

Explore the implementation of the factorial function using recursion and understand its mathematical significance. Learn how factorial helps count different arrangements of items and combinations, along with the special case of zero factorial. This lesson clarifies the basics of recursion through a key algorithm concept.

We'll cover the following...

For our first example of recursion, let's look at how to compute the factorial function. We indicate the factorial of nn by n!n!. It's just the product of the integers 11 through nn. For example, 5!5! equals 123451⋅2⋅3⋅4⋅5, or 120120. (Note: Wherever we're talking about the factorial function, all exclamation points refer to the factorial function and are not for emphasis.)

You might wonder why we would possibly care about the factorial function. It's very useful for when we're trying to count how many different orders there are for things or how many different ways we can combine things. For example, how many different ways can we arrange nn things? We have nn choices for the first thing. For each of these nn choices, we are left with n1n-1 choices for the second thing, so that we have n(n1)n⋅(n−1) ...