maximum recursion depth exceeded in comparison

后端 未结 6 1911
故里飘歌
故里飘歌 2021-01-06 13:21

I wrote this piece of code to compute the number of combinations:

def fact(n):
    return 1 if(n == 1) else n * fact(n - 1)

def combinations(n,k):
    retur         


        
相关标签:
6条回答
  • 2021-01-06 14:01

    You should try to avoid recursion for such a simple function as the factorial of a number. Recursion is really powerful, but sometimes it is overused for no reason.

    Here is the code for the iterative version of factorial function:

    def fact(n):
        result = 1
        for i in range(2, n + 1):
            result *= i
        return result
    

    Notice what Maxime says in the previous answer, that is exactly the problem you are having: your function doesn't contemplate the factorial of 0.

    0 讨论(0)
  • 2021-01-06 14:03

    The default recursion limit in python 3.x version onwards is 2000 only, If you call the same function again and again more than 2000 times, you will get maximum recursion depth error. The ideal approach would be to write a logic without recursion. But If you still stick to recursion, change the default recursion limit by:

    import sys

    sys.setrecursionlimit(10000)# It sets recursion limit to 10000.

    But the above may not meet all your needs in certain contexts.

    0 讨论(0)
  • 2021-01-06 14:04
    sys.setrecursionlimit(some_number)
    
    0 讨论(0)
  • 2021-01-06 14:07

    Try to replace:

    def fact(n):
        return 1 if(n == 1) else n * fact(n - 1)
    

    to:

    def fact(n):
        return 1 if(n <= 1) else n * fact(n - 1)
    

    Because if you pass 2 identical numbers, you would try to compute fact(0) (which would call fact(-1) and fact(-2), etc until the maximum recursion depth error).

    0 讨论(0)
  • 2021-01-06 14:12

    something like this works well:

    def factorial_by_recursion(max_number, current_number, somme):
        if current_number < max_number:
            somme += current_number
            return factorial_by_recursion(max_number, current_number + 1, somme)
        else:
            return somme
    
    0 讨论(0)
  • 2021-01-06 14:14

    Your Recursion depth out of the limit.

    1. Recursion is not the most idiomatic way to do things in Python, as it doesn't have tail recursion optimization thus making impractical the use of recursion as a substitute for iteration (even if in your example the function is not tail-recursive, that wouldn't help anyway). Basically, that means that you shouldn't use it for things that have a complexity greater than linear if you expect your inputs to be large.

    2. If you have a platform that supports a higher limit, you can set the limit higher:

      sys.setrecursionlimit(some_number) sys.setrecursionlimit(some_number)

      This function set the maximum depth of the Python interpreter stack to limit. This limit prevents infinite recursion from causing an overflow of the C stack and crashing Python. The highest possible limit is platform-dependent. A user may need to set the limit higher when she has a program that requires deep recursion and a platform that supports a higher limit. This should be done with care because a too-high limit can lead to a crash.

    ref:
    Python recursive function error: “maximum recursion depth exceeded”
    Python max recursion , question about sys.setrecursionlimit()

    0 讨论(0)
提交回复
热议问题