0

its my first year in my CS course and so some help will be appreciated. I try to make the Euclidian algorithm, and while it goes just fine, at the end it doesnt return the GCD (5) but None

def eykl(x, y):
    if x == y:
        return x
    elif x > y:
        l = x - y
        x = y
        print(f"{l}, {x}")
        eykl(l, x)
    else:
        l = y - x
        y = x
        print(f"{l}, {y}")
        eykl(l, y)
print(eykl(255, 155))
Than1
  • 169
  • 4

1 Answers1

2

You need to return the recursive results.

def eykl(x, y):
   if x == y:
       return x
   elif x > y:
       l = x - y
       x = y
       return eykl(l, x)
       ^^^^^^^^
   else:
       l = y - x
       y = x
       return eykl(l, y)
       ^^^^^^^
   print(eykl(255, 155))
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128