# L-10 MCS 275 Web 3 Feb 2008 : fibonacci def Fibonacci(n): """ returns the n-th Fibonacci number """ if n == 0: return 0 elif n == 1: return 1 else: return Fibonacci(n-1) + Fibonacci(n-2) def Fibotrace(n,k,c): """ returns (f,c) where f is the n-th Fibonacci number and c the number of function calls prints execution trace using control parameter k """ s = k*' ' + 'F(%d) = ' % n if n == 0: print s + '0' return (0,c) elif n == 1: print s + '1' return (1,c) else: print s + 'F(%d) + F(%d)' % (n-1,n-2) (f1,c1) = Fibotrace(n-1,k+1,c+1) (f2,c2) = Fibotrace(n-2,k+1,c+1) return (f1+f2,c1+c2) def F(n): """ iterative algorithm for n-th Fibonacci number """ if n == 0: return 0 else: a = 0 b = 1 for k in range(2,n+1): c = a + b a = b b = c return b def main(): """ prompts user for n and computes n-th Fibonacci number """ n = input("Give n : ") (f,c) = Fibotrace(n,0,0) print 'F(%d) = %d' % (n,f) print 'number of calls : %d' % c n = input("Give n : ") print 'F(%d) = %d' % (n,F(n)) main()