I have a code that basically solves one of the problem in Project Euler and aims to find out LCM of first 20 natural numbers.
def GCD(a, b): #Euclid's algorithim
if (b == 0):
return a
else:
GCD(b, a % b)
def LCM(a, b): #LCM(a,b) = a*b/GCD(a,b)
x = GCD(a, b)
return ((a * b)/x)
def RFIND(a, b):
if (b == 20):
return a
else:
RFIND(LCM(a, b), b + 1)
print RFIND(2, 1)
But I face an error while running it.
return ((a * b)/x)
TypeError: unsupported operand type(s) for /: 'int' and 'NoneType'
May I know Why?
Try
def GCD(a, b): #Euclid's algorithim
if (b == 0):
return a
else:
return GCD(b, a % b)
You have to return the value returned by the recursive call. RFIND has a similar problem.
You've implemented your recursion incorrectly.
def GCD(a, b): #Euclid's algorithim
...
return GCD(b, a % b)
def RFIND(a, b):
...
return RFIND(LCM(a, b), b + 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With