Here is a code that finds the value of the function T given by the recurrence relation for a given value of n:
def T(n):
  if n <= 0:
    return 1
  else:
    return T(n-1) + (n-1) * T(n-2)
    
print T(3)
#output
2
However, the code invokes the function T twice. I have added the print statement before the second return to show this:
def T(n):
  if n <= 0:
    return 1
  else:
    print (T(n-1) + (n-1) * T(n-2))
    return T(n-1) + (n-1) * T(n-2)
    
print T(3)
#output
1
2
1
2
Is there an efficient way of invoking the function T only once? I'm using Python 2. I'd like something short and simple, nothing too complicated.
 
    