Recursion: factorial
easy 16 minRecursion is a function solving a problem by calling itself on a smaller copy of it.
Check yourself
One question at a time. Each answer explains why.
Question 1 of 10
What are the two essential parts of every recursive function?
What happens if you forget the base case?
What is the space complexity of recursive factorial?
What does factorial(0) return?
How many frames are on the call stack at the deepest point of factorial(6)?
A function is written `if n == 1: return 1` then `return n * f(n - 1)`. What does f(0) do?
In Python, you call the recursive factorial(5000) with the default settings. What happens?
Which is true about tail recursion in CPython?
Naive recursive fib(4) (two calls per step) makes how many calls in total?
In `factorial(n)`, the frame for factorial(3) is waiting. What will it do when its call returns?
Pick an answer to see why.
Not quite. The base case stops the recursion and the recursive case shrinks the problem and calls the function again.
Not quite. Without a base case the function keeps calling itself, pushing frames until the stack runs out. Python raises RecursionError.
Not quite. All n frames are on the call stack at once when the base case runs, so the space is O(n).
Not quite. 0! = 1 by definition: there is exactly one way to arrange nothing. The base case n <= 1 returns 1.
Not quite. factorial(6), (5), (4), (3), (2) and (1) are all alive when factorial(1) hits the base case: 6 frames.
Not quite. From 0 the calls go to -1, -2 and so on and never equal 1, so the base case is never reached.
Not quite. 5,000 frames exceed the default limit of about 1,000. Python integers never overflow, but the stack does.
Not quite. CPython does not perform tail call optimisation. Every tail call still pushes a frame.
Not quite. fib(4), fib(3), fib(2) twice, fib(1) three times and fib(0) twice: 1 + 1 + 2 + 3 + 2 = 9 calls.
Not quite. Each frame still has `n * ...` left to do. factorial(3) receives 2 from factorial(2) and returns 3 × 2 = 6.
Flashcards
Say your answer, then reveal it.
Card 1 of 11
What are the two parts of every recursive function?
A base case that answers the smallest input directly and stops the calls, and a recursive case that calls the function on a smaller input and combines the result.
Say your answer, then reveal it.
What is the recursive definition of factorial?
n! = n × (n-1)! for n > 1, with 0! = 1! = 1. In code: if n <= 1 return 1, else return n * factorial(n - 1).
Say your answer, then reveal it.
Why is the base case n <= 1 and not n == 1?
n <= 1 also catches 0, since 0! = 1. With n == 1, factorial(0) jumps past the base case and recurses forever.
Say your answer, then reveal it.
What does BRAT stand for?
Base case first, Return a value, Approach the base case, Trust the recursive call.
Say your answer, then reveal it.
What happens on the call stack during factorial(4)?
Four frames are pushed, factorial(4) down to factorial(1). The base case returns 1, then the frames pop in reverse: 2, 6, 24.
Say your answer, then reveal it.
Time and space of recursive factorial?
O(n) time (n calls of constant work) and O(n) space (n stack frames alive at the base case). The loop is O(n) time and O(1) space.
Say your answer, then reveal it.
What is Python's default recursion limit, and what error do you get past it?
About 1,000 frames (sys.getrecursionlimit()). Going deeper raises RecursionError: maximum recursion depth exceeded.
Say your answer, then reveal it.
Does tail recursion save stack space in Python?
No. CPython does not optimise tail calls, so tail recursion still uses O(n) frames. It does make the function easy to turn into a loop.
Say your answer, then reveal it.
What is the leap of faith?
Assume the recursive call already works for the smaller input, and only decide how to combine its result. Induction guarantees this is sound.
Say your answer, then reveal it.
How do you read time and space off a recursion tree?
Time is the number of nodes times the work per node. Space is the height of the tree, the deepest chain of frames.
Say your answer, then reveal it.
Picture that makes recursion stick?
Russian dolls: each opens to a smaller doll until the solid one (the base case), then you close them in reverse. Or a line where each person says: I am 1 plus everyone behind me.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.