I am trying to implement Euclid's algorithm for computing the greatest common divisor using recursion. Below is the code for the same.
def euclid(a,b):
if a>b:
r=a%b
if (r == 0):
return b
else:
a=b
b=r
euclid(a,b)
print(euclid(20,4)) # Returns 4
print(euclid(20,8)) # Returns None
For the first data set, I get the correct result. But for euclid(20,8)
I get a return of None
.
While checking in the debugger, I did see the return value of b
become 4
but then for some reason, my code jumps to euclid(a,b)
and returns None
.
The major takeaway here would be to understand why the code does not return 4
but jumps to the euclid(a,b)
and return None
.
Please refrain from giving alternative code solutions but you are very much encouraged to point out the reason for the current behaviour of the code.