We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c1c2b3f commit b4fb04aCopy full SHA for b4fb04a
maths/factorial_iterative.py
@@ -1,16 +1,24 @@
1
-def factorial_iterative(n: int) -> int:
+def factorial_iterative(number: int) -> int:
2
"""
3
- Return the factorial of n using an iterative approach.
4
- Raises ValueError for negative inputs.
+ Return the factorial of a non-negative integer using an iterative method.
+
5
+ >>> factorial_iterative(5)
6
+ 120
7
+ >>> factorial_iterative(0)
8
+ 1
9
+ >>> factorial_iterative(1)
10
11
- if n < 0:
12
+ if number < 0:
13
raise ValueError("Input must be a non-negative integer")
14
15
result = 1
- for i in range(1, n + 1):
16
+ for i in range(2, number + 1):
17
result *= i
18
return result
19
20
21
if __name__ == "__main__":
22
# simple demonstration
23
print(factorial_iterative(5)) # expected 120
24
0 commit comments