0

this is a simple implementation of the euclidian algorithm but it always returns None The sample input is A=270 and B = 192 should return 6 I am not neccesarily looking for code just an explaination on why it returns none instead of an integer. Thanks

Code:

def GCD(a, b):
    if a == 0:
        print("B") # for debugging
        return int(b)
    elif b == 0:
        print("A") # for debugging
        return int(a)
    else:
        GCD(int(b), int((a%b))) # recursive function call

print(GCD(int(input("A:")),int(input("B:"))))

Output:

A:270
B:192
A
None
  • Hint 1: Imagine if, instead of recursively calling `GCD(int(b), int((a%b)))`, you instead wrote `karls_GCD(int(b), int((a%b)))`, where `karls_GCD` is a GCD algorithm that I wrote and you `import`ed into the code. Do you see why there would be a problem, *even assuming my code is correct*? The recursive call is *not different*. Hint 2: on the lines of code where you wrote `return int(b)` and `return int(a)`, why did you **not** instead just write `int(b) `and `int(a)`? Why would that not work? Again, the recursive call is *not different*; you're still *calling* something. – Karl Knechtel Jan 03 '22 at 17:49
  • I got it, thanks – Ranger 4860 Jan 03 '22 at 17:51

0 Answers0