Skip to content

Commit cfe0d16

Browse files
Create gcd_iterative.py
Add iterative GCD implementation
1 parent da16917 commit cfe0d16

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

maths/gcd_iterative.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def gcd_iterative(a: int, b: int) -> int:
2+
"""
3+
Compute the greatest common divisor (GCD) of two numbers iteratively.
4+
5+
Examples:
6+
>>> gcd_iterative(48, 18)
7+
6
8+
>>> gcd_iterative(7, 5)
9+
1
10+
"""
11+
while b != 0:
12+
a, b = b, a % b
13+
return a

0 commit comments

Comments
 (0)