The product through n can be described as n times the product through n - 1. This relationship gives factorial a recursive case. The base case is still zero factorial equals one, exactly as in the iterative version.
For factorial of three, the first call waits to multiply three by the answer for two. That call waits to multiply two by the answer for one. The call for one waits for zero, which returns one without another call. The pending multiplications then finish on the way back up.
smaller_product = 6
next_factor = 4
print(next_factor * smaller_product) # 24
The example shows one combination step. Your recursive function must obtain the smaller product by calling itself. Returning n times n - 1 would combine only two factors and stop too early. Forgetting to return the recursive expression would make callers receive None instead of the completed product.
This fresh variation is named factorial_recursive(n) and returns (product, calls). Count the current call as one, including the base case. For a positive n, obtain both values from the smaller call, multiply its product by n, and add one to its call count. Counting calls differs from the preceding lesson's count of multiplications: the base call returns directly but still exists.
Use the supplied _check_n helper for integer inputs from zero through 100. The result for zero is a product of one with one call. There are n + 1 calls and n multiplications. Under the bounded-arithmetic model the time is O(n), but stack space is also O(n), unlike the loop's constant number of variables. Each waiting frame must remember its n until the smaller result returns.
The result's integer size still grows, so exact arithmetic costs are not constant for unbounded n. Keep this implementation small and compare its reasoning with the loop rather than assuming recursion is faster. Both compute the same mathematical product; they store unfinished work differently.
The return lane shows only the product; your result also counts all four calls, including the base call.
The final task is one recursive combination: unpack the smaller result, multiply its product, and add the current call to its count. It does not need any new validation or setup.
_check_n(n) before doing any work.Edit main.py, then press Run to inspect the supplied probe. Press Submit to check the operation on additional inputs. You do not need to copy code from another lesson.