-
Notifications
You must be signed in to change notification settings - Fork 244
Calculate Factorial
Sar Champagne Bielert edited this page Apr 8, 2024
·
6 revisions
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