Recursion and Factorials


Recursion and Factorials

Michael
Today we’re introducing recursion: a way to solve a problem by having a function call itself. We’ll use factorials to see how that works. For a positive integer n, n factorial, written n!, is the product of all the whole numbers from n down to 1.

Maria
So four factorial is four times three times two times one, which is twenty-four. Two factorial is two, and one factorial is one. And there’s one special value to remember: zero factorial is defined to be one.

Michael
Right. That value is part of the definition, even though it may seem surprising at first. Now imagine we want to write a function called fact that takes a nonnegative integer and returns its factorial. To make it recursive, we need to find a way to express a factorial using a smaller factorial.

Maria
Look at the product for n factorial: n times n minus one times n minus two, and so on down to one. Everything after the first n is exactly the product for n minus one factorial. So we can write n factorial as n times n minus one factorial.

Michael
That gives us a recursive rule: n! equals n times (n minus one)!. For example, two factorial becomes two times one factorial, and since one factorial is one, the result is two. It also works for one: one factorial is one times zero factorial, and zero factorial is one.

Maria
But we can’t use that rule for every input. If n is zero, it would say zero factorial equals zero times negative one factorial. Negative one factorial isn’t defined here, so the rule breaks down at zero.

Michael
That’s why a complete recursive definition needs two cases. When n is at least one, n! is n times (n minus one)!. When n is zero, n! is one. The zero case is the stopping point, also called the base case.

Maria
And the rule for n at least one is the recursive case: it calls for a smaller factorial. Each time we apply it, the input moves closer to zero, where the base case gives us an answer instead of asking for another factorial.

Michael
Let’s follow that for three factorial. Three is at least one, so three factorial is three times two factorial. Two factorial is two times one factorial, and one factorial is one times zero factorial. Finally, zero factorial is one.

Maria
Putting those pieces together gives three times two times one times one, which equals six. That’s the same result we’d get from the original product definition, but the recursive definition reaches it through a chain of smaller problems.

Michael
So a recursive factorial function follows those two cases: return one when the input is zero; otherwise, return the input multiplied by the factorial of the input minus one. The base case matters because it ends the chain of function calls.

Maria
Exactly. The key idea is that recursion combines a smaller version of the problem with a clear stopping case. For factorials, the smaller problem is the factorial of n minus one, and the stopping case is zero factorial, which equals one.

Ask anything
We've read this whole study set, so questions about it get the best answers.