Summary - Factorial
- Algorithms course
- Beginner
- Lesson 48/80
- 4 min
Compute and print a number's factorial in a mini task.
Summary - Factorial
In this group you learned the factorial definition, recursive and iterative solutions and base case selection. Both methods are O(n).
The recursive solution is readable but uses O(n) memory. The iterative solution is more efficient with O(1) memory.
Practice Full factorial
Write factorial_full(n). Compute recursively. Print 720 with print(factorial_full(6)).
Starter code
def factorial_full(n):
# Full factorialSample solution
def factorial_full(n):
# Full factorial
if n <= 1:
return 1
return n * factorial_full(n - 1)
print(factorial_full(6))Expected output
720
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Summary - Factorial: match the terms
| Term | Meaning |
|---|---|
n! |
n*(n-1)! |
0! |
1 |
Recursive |
O(n) memory |
Iterative |
O(1) memory |
2 Factorial
n! = n * ____
n! = n * (n-1)!
Explanation: (n-1)! is right.
3 Which method CANNOT compute factorial?
- Recursive
- Linear search Correct answer
- Iterative
- Loop
Explanation: Linear search does not compute factorial.