Tail Recursive Factorial
Background
Factorial computations are a classic way to demonstrate recursion. However, standard recursion can lead to a stack overflow for large numbers. Tail recursion solves this by passing an accumulator (the state) in the recursive call, allowing compilers/interpreters to optimize the stack (though Python doesn't do this natively, it's still an important algorithmic pattern).
Task
Calculate the factorial of $n$ ($n!$) using a tail-recursive function.
Specifications
- Input: An integer
n. - Output: The factorial of
n.
Constraints
- The function must be recursive.
- The recursive call must be the very last operation in the function (tail recursion).
- Do not use iterative loops like
fororwhile. - You must use an accumulator argument.
Example
Instructions
- Implement the
factorial_tailfunction using the tail-recursion paradigm. - Initialize the accumulator appropriately.
- Validate your code with the provided tests.