Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions maths/factorial_iterative.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def factorial_iterative(number: int) -> int:
"""
Return the factorial of a non-negative integer using an iterative method.

>>> factorial_iterative(5)
120
>>> factorial_iterative(0)
1
>>> factorial_iterative(1)
1
"""
if number < 0:
raise ValueError("Input must be a non-negative integer")

result = 1
for i in range(2, number + 1):
result *= i
return result


if __name__ == "__main__":
# simple demonstration
print(factorial_iterative(5)) # expected 120
13 changes: 13 additions & 0 deletions maths/gcd_iterative.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def gcd_iterative(a: int, b: int) -> int:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: a

Please provide descriptive name for the parameter: b

"""
Compute the greatest common divisor (GCD) of two numbers iteratively.
Examples:
>>> gcd_iterative(48, 18)
6
>>> gcd_iterative(7, 5)
1
"""
while b != 0:
a, b = b, a % b
return a