I am trying to find a factorial of a large number using recursion in Python 3.6. Although I have set the recursion limit to 10**9, still running the code makes the kernel go dead. I know that iterative solutions are much better, yet I wanted to know the reason for this
import sys
mod = 10**9+7
sys.setrecursionlimit(10**9)
def fac(n):
if(n == 1):
return 1
return (fac(n-1)%mod*n%mod)%mod
print(fac(10000))
%does nothing. You need to add parentheses around(n%mod)if you want it to do what you intend. Also, it makes no sense to use recursion for factorial. It's a simple iteration. You shouldn't need to increase the stack limit at all. Factorial is not a good candidate for recursion.mod? why notfac(n-1)*n? Ismodneeded to demonstrate the stack overflow?