I am trying to write a code to calculate GCD of two numbers(n and m) recursively. I want my answer to update in gcd_val as the recursion progresses. I tried debugging and can't understand why it chooses the earlier gcd_val as compared to the value obtained in its sub-call(inner recursive call). I want to know why is this happening and what should be the correct way to do it? I get the answer 2 when it should be 4.
def find_gcd(n, m, gcd_val):
factor_list = [2, 3, 5]
if n == 0 or m == 0:
return 0
for f in factor_list:
if n % f == 0 and m % f == 0:
find_gcd(int(n/f), int(m/f), gcd_val)
gcd_val *= f
return gcd_val
print(find_gcd(20, 8, 1))