def mike(a,b):
d=min(a,b)
e=d
if max(a,b)%d==0 and e%d==0:
return d
else:
return mike(max(a,b),d-1)
print(mike(12,18))
I cannot find a way to have the value of 'e' as a fixed value of the minimum among the two integers, which in case is 12. As the code moves on so does the value of 'e'. But I want it to have a fixed value of the least among the two. Sorry for the noob question. Help please.
So you don't need to funcions, you can just add e as an optional argument
def mike(a,b,e=None):
d=min(a,b)
if not e:
e=d
if max(a,b)%d==0 and e%d==0:
return d,e
else:
return mike(max(a,b),d-1,e)
print(mike(12,18))
That way you can call it recursively, and still track and output the minimum of the original function
I don't understand why you set e=d and why you need d-1 in recursion
def mike(a,b):
rest = max(a, b) % min(a, b)
if rest == 0:
return min(a, b)
else:
return mike(min(a, b), rest)
Calc of gcd.
(48,30)
Divide the biggest by the lowest and get the rest. 48 / 30 = 1 (rest 18)
Divide the previous divisor by the rest 30 / 18 = 1 (rest 12)
Divide the previous divisor by the rest again 18 / 12 = 1 (rest 6)
Divide the previous divisor by the rest again 12 / 6 = 2 (rest 0)
gcd is the divisor when rest == 0
I eventually did this at the end. Thanks for all the answers though. I know I made it a way more complicated.
def mike(x,y):
e=min(x,y)
def mi(a,b):
d=min(a,b)
if max(a,b)%d==0 and e%d==0:
return d
else:
return mi(max(a,b),d-1)
zz=mi(x,y)
return zz
print(mike(12,18))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.