forked from codepath/compsci_guides
-
Notifications
You must be signed in to change notification settings - Fork 0
Calculate Factorial
Jessica Sang edited this page Sep 14, 2024
·
1 revision
Unit 1 Session 1 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Will
n
always be a positive integer?- Yes.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Create a function that calculates the factorial of a positive integer n
.
1) Create and initialize a variable to store the result
2) For each number from 1 to n, multiply the result by that number
3) Return the result variable
- Make sure your result variable is correctly initialized! What is the smallest possible result for our factorial function?
def factorial(n):
result = 1
for num in range(1, n + 1):
result *= num
return result