Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active February 22, 2018 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/9af9ecfb6e4c690d1ff46c31f5416851 to your computer and use it in GitHub Desktop.
Save komasaru/9af9ecfb6e4c690d1ff46c31f5416851 to your computer and use it in GitHub Desktop.
Python script to compute G.C.D. with Eublid's algorithm.
#! /usr/local/bin/python3.6
"""
G.C.D. Computation with Euclid's algorithm
"""
import sys
import traceback
class GcdEuclid:
""" Class for G.C.D. Euclid's algorithm """
def gcd(self, a, b):
""" G.C.D. Calculation
:param int a: A value
:param int b: B value
"""
try:
return a if b == 0 else self.gcd(b, a % b)
except Exception as e:
raise
if __name__ == '__main__':
if len(sys.argv) < 3:
print("USAGE: ./gcd_euclid.py A B")
sys.exit(0)
try:
a, b = list(map(int, sys.argv[1:3]))
if a == 0 or b == 0:
print("Should be integers greater than 0.")
sys.exit(0)
obj = GcdEuclid()
print("GCD({}, {}) = {}".format(a, b, obj.gcd(a, b)))
except Exception as e:
traceback.print_exc()
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment