There’s nothing that makes recursion inherently more efficient. Moreover, the same algorithm implemented with recursion is likely to be less efficient due to function call overhead. Worse than that, and this is exactly your case, using recursions of large depth is likely to cause stack overflow (no pun intended). Unless tail-recursion optimization is in use, which is not in Python.
Python does not have any limits (within reasonable ranges) of integer number size, and this is not the problem. Re-implement the function using a loop, and you’ll do fine. Except that n ** n
does not do what you seem to think it does.
3
solved Deal with extremely large numbers with Python Programming [closed]