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
21 changes: 20 additions & 1 deletion maths/greatest_common_divisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,40 @@
"""




def gcd(a, b):
"""Calculate Greatest Common Divisor (GCD)."""
return b if a == 0 else gcd(b % a, a)


"""
This method is more efficient.
This method is not acquire more memory cause is no use of any stacks(chunk of a memory space).
while above method is good one but acquire more memory for huge number because of more recursive call.

"""
def gcd_by_iterative(x,y):
while y:
x,y=y,x%y
"""Now return final answer that is GCD"""
return x


def main():
"""Call GCD Function."""
try:
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()