Skip to content

Another method added for GCD #1387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 22, 2019
15 changes: 14 additions & 1 deletion maths/greatest_common_divisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
"""



# This method is more efficient not acquire more memory cause is no use of any stacks like in recursive as next below mentioned.
def gcd_by_iterative(x,y):
while y:x,y=y,x%y
Copy link
Member

Choose a reason for hiding this comment

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

Please split this over two lines for readability.
Please add doctests to both functions. See CONTRIBUTING.md for details.


return x



def gcd(a, b):
"""Calculate Greatest Common Divisor (GCD)."""
return b if a == 0 else gcd(b % a, a)
Expand All @@ -16,10 +25,14 @@ def main():
nums = input("Enter two Integers separated by comma (,): ").split(",")
num_1 = int(nums[0])
num_2 = int(nums[1])

print(f"gcd({num_1}, {num_2}) = {gcd(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")


Copy link
Member

Choose a reason for hiding this comment

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

Please lose this whitespace.

if __name__ == "__main__":
main()