def factorial_iter(num): '''Returns the factorial of num using iteration.''' total = 1 for i in range(2, num + 1): total *= i return total def factorial(num): '''Returns the factorial of num using recursion.''' print("computing {0}!".format(num)) if num == 0: print("computed 0! = 1") return 1 else: answer = num * factorial(num - 1) print("computed {0}! = {1}".format(num, answer)) return answer def strlen_iter(my_str): '''Return the length of my_str without using len.''' count = 0 for char in my_str: count += 1 return count def strlen(my_str): '''recursive strlen''' if my_str == '': return 0 # base case else: # recursive case return 1 + strlen(my_str[1:]) def num_digits(num): '''Return the number of digits in (positive) num''' if num < 10: # base case return 1 else: # recursive case return 1 + num_digits(num / 10) def power(base, n): '''Return base raised to the power n.''' if n == 0: # base case return 1 else: # recursive case return base * power(base, n - 1) def fib(n): '''Returns the nth fibonacci number.''' if n == 0 or n == 1: # base cases return 1 else: # recursive case return fib(n - 1) + fib(n - 2)