-
Notifications
You must be signed in to change notification settings - Fork 2
/
gcd.py
executable file
·44 lines (36 loc) · 1.01 KB
/
gcd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3
"""Greatest Common Divisor"""
# Some characterstics of this program used for testing:
#
# * check_args() does not have a 'return' statement.
# * check_args() raises an uncaught exception when given the wrong number
# of parameters.
import sys
def check_args():
if len(sys.argv) != 3:
# Rather than use sys.exit let's just raise an error
raise Exception("Need to give two numbers")
for i in range(2):
try:
sys.argv[i+1] = int(sys.argv[i+1])
except ValueError:
print("** Expecting an integer, got: %s" % repr(sys.argv[i]))
sys.exit(2)
pass
pass
def gcd(a,b):
""" GCD. We assume positive numbers"""
# Make: a <= b
if a > b:
(a, b) = (b, a)
pass
if a <= 0:
return None
if a == 1 or b-a == 0:
return a
return gcd(b-a, a)
if __name__=='__main__':
check_args()
(a, b) = sys.argv[1:3]
print("The GCD of %d and %d is %d" % (a, b, gcd(a, b)))
pass