Reputation: 492
The code given below is only working correctly for some of the inputs such as gcdIter(2, 12) which gives me correct output i.e 2 but if I give input as gcdIter(220,120) it gives me 110 instead of 20. I need some help with the logic.
def gcdIter(a, b):
'''
a, b: positive integers
returns: a positive integer, the greatest common divisor of a & b.
'''
if a<b:
while a>0:
if b%a==0:
print('the gcd is : '+''+str(a))
break
else:
a -= 1
else:
while b>0:
if a%b==0:
print('the gcd is :'+''+str(b))
break
else:
b -= 1
Upvotes: 0
Views: 3995
Reputation: 1121
it's simple like that.No need to check
a<b
ora>b
def gcdIter(a, b):
while b:
a, b = b, a%b
print('the gcd is :'+str(a))
Upvotes: 5