def factorial_iterative(n): '''returns the factorial of n''' total = 1 for i in range(2, n + 1): total *= i return total def factorial_recursive(n): print("computing {0}!".format(n)) if n == 0: # base case print("computed 0! = 1") return 1 else: # recursive case result = n * factorial(n - 1) print("computed {0}! = {1}".format(n, result)) return result def strlen_iterative(s): '''returns the length of s without using len''' count = 0 for char in s: count += 1 return count def strlen_recursive(s): if s == '': # base case return 0 else: # recursive case return 1 + strlen(s[1:]) def num_digits(n): '''return the number of digits in n''' if n < 10: # base case return 1 else: # recursive case return 1 + num_digits(n / 10) def power(base, n): '''returns base raised to the power n''' if n == 0: # base case return 1 else: # recursive case return base * power(base, n - 1)