Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why 'NoneType' is returned in Python?

Tags:

python

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?

like image 801
Shubham Avatar asked Jun 13 '26 09:06

Shubham


2 Answers

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.

like image 159
Sven Marnach Avatar answered Jun 15 '26 01:06

Sven Marnach


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)
like image 21
Ignacio Vazquez-Abrams Avatar answered Jun 15 '26 03:06

Ignacio Vazquez-Abrams