This is the problem in my textbook:
Greatest Common Divisor of two integers, p and q
(a) Base Case. If p = q: return p.
(b) p < q. If p < q: return GCD(q, p).
(c) p > q. If p > q: return GCD(p, p - q)
It seems as though my solution matches the above statements. But it generates a recursion error:
File "G:\python\function.py", line 8, in GCD
return GCD(p,p-q)
RuntimeError: maximum recursion depth exceeded
My code:
def GCD(p,q):
"""Greatest Common Divisor of two integers, p and q."""
if q == p:
return p
elif p < q:
return GCD(q,p)
elif p > q:
return GCD(p,p-q)
GCD(21,28)
Didn't I follow the statements of the problem?