# reads in 2 integers, prints their greatest common divisor
# ECS 10, May 29, 2009
# Matt Bishop
# read in a number
# parameters: none
# returns: 2 values
# 1st: the number, if read successfully
# 0, on EOF
# -1, if not EOF and not successful
# 2nd: True, if list read successfully; False, if not
def getnum():
# read the input
# catch any problems and bail out at once
try:
# read in ... something (a number, supposedly)
n = input("Enter an integer: ")
# this causes a TypeError if not a number
n += 0
except EOFError:
# didn't enter anything
return 0, False
except (NameError, TypeError, SyntaxError):
# entered something bogus
print "Please enter an integer"
return -1, False
# return the value read
return n, True
# recursively compute the GCD of two numbers
# parameters: m, n are the numbers
# returns: the number of positive integers in the list
def gcd(m, n):
# base case: if n is 0, the other is the GCD
if n == 0:
return m
# recursion here
# this just is the Euclidean algorithm
else:
return gcd(n, m % n)
# this puts it all together
def main():
# get the first number
m, okay = getnum()
# if it's not an integer, give an error
if okay and m != int(m):
print m, "is not an integer"
# all's well -- get the next number
elif okay:
n, okay = getnum()
# if it's not an integer, give an error
if okay and n != int(n):
print n, "is not an integer"
# got both numbers -- print the GCD
elif okay:
print "The greatest common divisor of", m, "and", n, "is", gcd(m,n)
main()