Home > Software design >  Can someone explain how is result in this function -2 ? Python
Can someone explain how is result in this function -2 ? Python

Time:10-07

Can someone explain to me how is output -2 and why? If it is possible to write the procedure how it came to be

def f(a):
  if a > 10:
    return f(a-1) - f(a-3)
  else:
    return 1

b = 13
c = f(b)
print(c)

CodePudding user response:

You are creating series of recurences, at the end output is just 1-1-1-1 = -2

First f(13) = f(12) - 1 = f(11) - 1 - 1 = 1 - 1 - 1 - 1

CodePudding user response:

You have to understand what the function does. It will nest itself until the value of "a" is less than 10 and then return 1. Expect for the first loop which will always return f(a-1) - f(a-3). So basically you have (1-1) - (1-3) = 0 - 2 = -2

  • Related