>>> def fib(n): ... """ Calculates the nth Fibonacci number recursively """ ... if n == 1 or n == 2: ... return 1 ... else: ... return fib(n-1) + fib(n-2) ... >>> fib(10) 55 >>> fib(20) 6765 >>> fib(30) 832040 >>> fib(32) 2178309 # takes about 1-2 seconds >>> fib(34) 5702887 # takes about 3 seconds >>> fib(36) 14930352 # takes about 7 seconds >>> def fib2(n): ... a, b = 1, 1 ... for i in range (n-1): ... a, b = a+b, a ... return b ... >>> fib2(36) 14930352 >>> fib2(360) 769246427201094785080787978422393713094534885688979999504447628313150135520 >>> runfile('/Volumes/home/schar/Desktop/recursion.py', wdir='/Volumes/home/schar/Desktop') >>> factorial(5) 120 >>> factorial(-5) [...] File "/Volumes/nfshome/schar/Desktop/cs150/recursion.py", line 23, in factorial res = n * factorial(n-1) File "/Volumes/nfshome/schar/Desktop/cs150/recursion.py", line 18, in factorial if n == 0: RecursionError: maximum recursion depth exceeded in comparison >>> power(2, 5) 32 # edited to add print statement >>> runfile('/Volumes/nfshome/schar/Desktop/cs150/recursion.py', wdir='/Volumes/nfshome/schar/Desktop/cs150') >>> power(2, 5) calling power( 2 , 5 ) calling power( 2 , 4 ) calling power( 2 , 3 ) calling power( 2 , 2 ) calling power( 2 , 1 ) calling power( 2 , 0 ) 32 >>> power(2, 32) calling power( 2 , 32 ) calling power( 2 , 31 ) calling power( 2 , 30 ) calling power( 2 , 29 ) calling power( 2 , 28 ) calling power( 2 , 27 ) calling power( 2 , 26 ) calling power( 2 , 25 ) calling power( 2 , 24 ) calling power( 2 , 23 ) calling power( 2 , 22 ) calling power( 2 , 21 ) calling power( 2 , 20 ) calling power( 2 , 19 ) calling power( 2 , 18 ) calling power( 2 , 17 ) calling power( 2 , 16 ) calling power( 2 , 15 ) calling power( 2 , 14 ) calling power( 2 , 13 ) calling power( 2 , 12 ) calling power( 2 , 11 ) calling power( 2 , 10 ) calling power( 2 , 9 ) calling power( 2 , 8 ) calling power( 2 , 7 ) calling power( 2 , 6 ) calling power( 2 , 5 ) calling power( 2 , 4 ) calling power( 2 , 3 ) calling power( 2 , 2 ) calling power( 2 , 1 ) calling power( 2 , 0 ) 4294967296 >>> fast_power(2, 32) calling fast_power( 2 , 32 ) calling fast_power( 2 , 16 ) calling fast_power( 2 , 8 ) calling fast_power( 2 , 4 ) calling fast_power( 2 , 2 ) calling fast_power( 2 , 1 ) calling fast_power( 2 , 0 ) 4294967296 >>> fast_power(2, 31) calling fast_power( 2 , 31 ) calling fast_power( 2 , 30 ) calling fast_power( 2 , 15 ) calling fast_power( 2 , 14 ) calling fast_power( 2 , 7 ) calling fast_power( 2 , 6 ) calling fast_power( 2 , 3 ) calling fast_power( 2 , 2 ) calling fast_power( 2 , 1 ) calling fast_power( 2 , 0 ) 2147483648 >>> runfile('/Volumes/nfshome/schar/Desktop/cs150/turtle_recursion.py', wdir='/Volumes/nfshome/schar/Desktop/cs150') >>> spiral_demo(80, 40) >>> spiral_demo(80, 1) >>> spiral_demo(80, 2) >>> figure_demo(120, 3) >>> figure_demo(120, 4) >>> figure_demo(120, 5) >>> figure_demo(120, 6) >>> broccoli_demo() >>> broccoli_demo() drew 40 segments >>> runfile('/Volumes/nfshome/schar/Desktop/cs150/turtle_recursion.py', wdir='/Volumes/nfshome/schar/Desktop/cs150') >>> broccoli_demo() drew 3280 segments