Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 15 additions & 55 deletions maths/greatest_common_divisor.py
Original file line number Diff line number Diff line change
@@ -1,76 +1,36 @@
"""
Greatest Common Divisor.

Wikipedia reference: https://en.wikipedia.org/wiki/Greatest_common_divisor

gcd(a, b) = gcd(a, -b) = gcd(-a, b) = gcd(-a, -b) by definition of divisibility
Greatest Common Divisor (GCD) Program
"""


def greatest_common_divisor(a: int, b: int) -> int:
"""
Calculate Greatest Common Divisor (GCD).
>>> greatest_common_divisor(24, 40)
8
>>> greatest_common_divisor(1, 1)
1
>>> greatest_common_divisor(1, 800)
1
>>> greatest_common_divisor(11, 37)
1
>>> greatest_common_divisor(3, 5)
1
>>> greatest_common_divisor(16, 4)
4
>>> greatest_common_divisor(-3, 9)
3
>>> greatest_common_divisor(9, -3)
3
>>> greatest_common_divisor(3, -9)
3
>>> greatest_common_divisor(-3, -9)
3
Calculates the Greatest Common Divisor (GCD) using recursion.
"""
return abs(b) if a == 0 else greatest_common_divisor(b % a, a)
# Bug fixed: Correct recursive definition
return abs(a) if b == 0 else greatest_common_divisor(b, a % b)


def gcd_by_iterative(x: int, y: int) -> int:
"""
Below method is more memory efficient because it does not create additional
stack frames for recursive functions calls (as done in the above method).
>>> gcd_by_iterative(24, 40)
8
>>> greatest_common_divisor(24, 40) == gcd_by_iterative(24, 40)
True
>>> gcd_by_iterative(-3, -9)
3
>>> gcd_by_iterative(3, -9)
3
>>> gcd_by_iterative(1, -800)
1
>>> gcd_by_iterative(11, 37)
1
Calculates the GCD using an iterative method (more memory efficient).
"""
while y: # --> when y=0 then loop will terminate and return x as final GCD.
while y:
x, y = y, x % y
return abs(x)


def main():
"""
Call Greatest Common Divisor function.
"""
"""Main function for user input and output."""
try:
nums = input("Enter two integers separated by comma (,): ").split(",")
num_1 = int(nums[0])
num_2 = int(nums[1])
print(
f"greatest_common_divisor({num_1}, {num_2}) = "
f"{greatest_common_divisor(num_1, num_2)}"
)
print(f"By iterative gcd({num_1}, {num_2}) = {gcd_by_iterative(num_1, num_2)}")
except (IndexError, UnboundLocalError, ValueError):
print("Wrong input")
num_1 = int(nums[0].strip())
num_2 = int(nums[1].strip())

print(f"greatest_common_divisor({num_1}, {num_2}) = {greatest_common_divisor(num_1, num_2)}")

Check failure on line 29 in maths/greatest_common_divisor.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/greatest_common_divisor.py:29:89: E501 Line too long (101 > 88)
print(f"gcd_by_iterative({num_1}, {num_2}) = {gcd_by_iterative(num_1, num_2)}")

except (IndexError, ValueError):
print(" Invalid input! Please enter two valid integers separated by a comma.")


if __name__ == "__main__":
Expand Down
Loading