I've seen some other similar topics, but I can't apply the solutions to fix the error given by the following code:
def mult_recursive(a,b):
if b == 1:
return a
else:
return a + mult_recursive(a,b-1)
def factorial_recursive(n):
if n == 0:
return 1
elif n == 1:
return 1
else:
return mult_recursive(n,factorial_recursive(n-1))
print (factorial_recursive(8))
You get that error because you've exceeded maximum recursion depth. What exactly you misunderstand? Go line by line and calculate the depth of calls you make. If you exceed max recursion depth, you're done. It seems that for
n==8 you exceed it. For lowers you don't.
And that makes sense, since for
n==8 you have
factorial_recursive(n-1) == 5040 and thus
mult_recursive(n, factorial_recursive(n-1)) does
5040 nested calls. This is definitely too much.
For
n == 7 you only do
720 nested calls. By default max recursion is
1000 (might depend on your machine/python version) so there you go.