print("*****************\n\nRECURSION READING PROBLEM 0") def R5(n): if n == 0: print("Forward with (base):", n) else: print("Forward with:", n) R5(n-1) print("R5({}): ".format(1)) R5(1) print("R5({}): ".format(3)) R5(3) # UNFOLDING EXAMPLE print("*****************\n\nRECURSION READING PROBLEM 1") def R4(n): if n == 0: print("forward with (base):", n) returnVal = 1 print("back with:",returnVal) return returnVal else: print("forward with:", n) returnVal = 2 * R4(n-1) print("back with:",returnVal) return returnVal #print("R4({}) = {}".format(1, R4(1))) print("R4({}) = {}".format(3, R4(3))) print("*****************\n\nRECURSION READING PROBLEM 2") def R2(n): if n == 9: return -1 return 1 + R2(n+1) print("R2({}) = {}".format(9, R2(9))) print("R2({}) = {}".format(4, R2(4))) print("*****************\n\nRECURSION READING PROBLEM 3") def R1(n): if n < - 3: return 0 return 1 + R1(n-1) print("R1({}) = {}".format(-8, R1(-8))) print("R1({}) = {}".format(4, R1(4))) print("*****************\n\nRECURSION READING PROBLEM 4") def Fib(n): if n == 0 or n == 1: return 1 return Fib(n-1) + Fib(n-2) print("Fib({}) = {}".format(3, Fib(3))) print("Fib({}) = {}".format(7, Fib(7))) print("*****************\n\nRECURSION READING PROBLEM 5") def G(x, b): if x == b: return 0 return 1 + G(x+1,b) print("G({},{}) = {}".format(3,100, G(3,100))) print("G({},{}) = {}".format(100,100, G(100,100))) print("G({},{}) = {}".format(98,100, G(98,100))) print("*****************\n\nRECURSION READING PROBLEM 6") def H(x): if x < 0: r = -1 else: r = 10 * H(x - 2) return r print("H({}) = {}".format(0, H(0))) print("H({}) = {}".format(-3, H(-3))) print("H({}) = {}".format(4, H(4))) print("H({}) = {}".format(5, H(5))) print("H({}) = {}".format(20, H(20))) print("*****************\n\nREADING PROBLEM 7") # This program ALMOST returns the sum of the cubes between # a and b (inclusive). Fill in the base case(s) appropriately. def F(a,b): return a**3 + F(a+1, b-1) + b**3 #print("F({},{}) = {}".format(2, 5, F(2,5)))