Practice question · Numerical answer
The factorial is defined by fact(n) = n * fact(n-1) with fact(0) = 1. Compute fact(5).
Hints
- Unwind to the base case, then multiply back up.
- fact(4) is 24.
Show the answer
120
Why
5 4 3 2 1 = 120, with the base case contributing the final factor 1. Checking a recursive definition on a small value you can verify by hand is the same sanity check a base case performs in an induction proof.
Practise Recursion
The app has 6 more questions on this lesson, and keeps your place in the course. Mathematics I is free to start.
More questions on Recursion
- A recursive function calls itself, which sounds like it should never finish. Why does a correct recursion…
- Each definition is evaluated at n = 5. Sort by whether the recursion terminates.
- Order what happens when a recursive factorial computes 3 factorial.
- Computing fact(4) recursively, how many calls to fact are made in total, counting the original call and the…
- Complete the condition for a recursion to terminate.
- A recursive Fibonacci function is elegant and unusably slow; the loop version is ugly and instant. Where does…