Skip to content

Instantly share code, notes, and snippets.

@PatchRowcester
Created April 23, 2013 02:32
Show Gist options
  • Save PatchRowcester/5440375 to your computer and use it in GitHub Desktop.
Save PatchRowcester/5440375 to your computer and use it in GitHub Desktop.
Iterative way to calculate the GCD of two numbers.
def gcdIter(a,b):
x = min(a,b)
for i in range(min(a,b)):
if a%x==0 and b%x==0:
break
else:
x -= 1
return x
print('This is a program to calculate the GCD of two integers.')
a = int(raw_input('Enter first number: '))
b = int(raw_input('Enter second number: '))
z = gcdIter(a,b)
print('The GCD of the two numbers is: ' + str(z))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment