#------------------------------------------------------------------------------- # Recursion.py # functions that call themselves #------------------------------------------------------------------------------- # factorial() def factorial(n): """Returns the factorial of n if n>=0, returns None otherwise.""" if n==0: return 1 elif n>0: return n*factorial(n-1) # end if-elif # end factorial() # Fibonacci() def Fibonacci(n): """Returns the nth Fibonacci number if n>=0, returns None otherwise.""" if n==0: return 0 elif n==1: return 1 elif n>=2: return Fibonacci(n-1)+Fibonacci(n-2) # end if-elif # end Fibonacci() # Ackermann() def Ackermann(m, n): """Returns values of the Ackermann function if m>=0, n>=0, None otherwise.""" if m==0 and n>=0: return n+1 elif m>0 and n==0: return Ackermann(m-1, 1) elif m>0 and n>0: return Ackermann(m-1,Ackermann(m, n-1)) # end if-elif # end Ackermann() # T() def T(m, n): if m==0 and n>=0: return n+1 elif m>0 and n==0: return T(m-1, 1) elif m>0 and n>0: return T(m-1, n) + T(m, n-1) # end if-elif # end Ackermann() #-- main program -------------------------------------------------------------- if __name__ == '__main__': # eliminate global variables print('100! =', factorial(100)) print('Fibonacci(25) =', Fibonacci(25)) print('Ackermann(3, 6) =', Ackermann(3, 6)) print('T(10, 12) =', T(10, 12)) # end if #-- end main program ----------------------------------------------------------