0

Hello everyone I made a recursive factorial to compare it with a normal one and the problem is that it only reaches the number 5 and there it gets corrupted

Error: RecursionError: maximum recursion depth exceeded while calling a Python object

import time

def factorial(n):
    res=1
    while n>1:
        res*=n
        n-=1
    return res

def factorial_r(n):
    print(n)
    if n==1:
        return 1
    return n*factorial_r(n-1)

if __name__=="__main__":
    n=1000
    c=time.time()
    factorial(n)
    f = time.time()
    print(f-c)

    c =time.time()
    factorial_r(n)
    f = time.time()
    print(f-c)

It's factorial_r Is there something that I am not understanding well? something i did wrong?

1

1 Answer 1

1

I don't know which version of Python you're using, but the problem is Python does have a recursion limit. Meaning, the stack depth is limited. That is actually a useful feature to avoid accidental infinite recursion loops.

See What is the maximum recursion depth in Python, and how to increase it?

Where they suggest

import sys
sys.setrecursionlimit(1500)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.