Exercise: Iterative Factorial
Leer en Español | Solve Online
Background/Motivation
The factorial of a non-negative integer $n$, denoted by $n!$, is the product of all positive integers less than or equal to $n$. Factorials are used in many areas of mathematics, including combinatorics, algebra, and mathematical analysis. Calculating a factorial is a classic example used to teach loop-based accumulation and is often compared with its recursive implementation.
The Task
Implement a function factorial_iterative(n: int) -> int that calculates the factorial of $n$ using a loop.
Specifications
- Function Name:
factorial_iterative - Arguments:
n(int) - Return Type:
int - Expected Output: The value of $n!$. For $n=0$, $0! = 1$.
Constraints
- $0 \le n \le 50$
Example
Instructions
- Open
exercises/factorial_iterative/solution.py. - Implement the
factorial_iterativefunction. - Change
SUBMIT = FalsetoSUBMIT = Trueat the top of the file when you are ready to be graded. - Run
python solution.pylocally to verify your solution with the built-in self-tests.