0

How can the maximum recursion depth modified? I think, in Python, maximum recursion possible is 987 because of this.

def harmonic_sum(n):
    if(n==1):
        return 1
    else:
        h_sum=(1/n)+harmonic_sum(n-1)
    return h_sum
>>> harmonic_sum(986)
7.471379033179059
>>> harmonic_sum(987)
RuntimeError: maximum recursion depth exceeded in comparison

I wrote a recursive function to calculate the power of 2 to nth and it also fails when it reaches 987.

Is there some ways to modify the maximum recursion depth allocated?

Please don't suggest an iterative way to solve the problem. This is my effort to make recursion well-established in my head. I am surfing internet and solving as much recursive problem possible with timer in my hand.

8
  • 2
    Just a suggestion: why don't you change it to iteration? Commented Oct 31, 2013 at 20:23
  • No, No...I am going to do EVERYTHING for 3 upcoming month using recursion and avoiding loops in python. I am so sick and tired of not able to use recursion promptly. List comprehension is good but my goal is to learn recursion in such a way that it becomes my second nature Commented Oct 31, 2013 at 20:30
  • 2
    @Jack_of_All_Trades Then the answer is: You can't. CPython doesn't do tail call optimizations or allow increasing the stack space arbitrarily. If you want to practice recursion like that, choose a different language or (more useful) only use recursion in cases where the recursion isn't trivial to transform into a loop. There are enough cases like this, and most of them don't overflow the stack (e.g. tree and graph traversal, parsers, backtracking algorithms, and so on). Commented Oct 31, 2013 at 20:32
  • @delnan: that being said, I should be always careful not to exceed the recursion depth while using python. Thanks. Commented Oct 31, 2013 at 20:34
  • 2
    I think it's great you're trying to really learn recursion. But if you're going to learn it by completely abandoning loops for a while, then you really, really should use some other language. There are several good choices, among them Scheme and Clojure (both are flavors of Lisp; there are plenty of others). Commented Oct 31, 2013 at 21:34

2 Answers 2

1
import sys
sys.setrecursionlimit(9000)

The number 9000 is arbitrary. Pick something relevant for your application.

Sign up to request clarification or add additional context in comments.

3 Comments

-1 If you put this too high and recurse accordingly, it'll just crash the whole process. It doesn't change how much stack space is available, it just changes when Python throws an exception to avoid running out of said stack space. See stackoverflow.com/q/19320903/395760 for an example of this advise going wrong.
@delnan: It's hard for me to fault this answer, because it does literally answer the question being asked. So I don't think it's worth a downvote. Whether setting the recursion limit is a good idea is another matter. I happen to agree that it's not a good idea, so if I were answering, I'd try very hard to discourage the OP from doing it... and then I'd tell him how to anyway.
@JohnY If this answer pointed at other options and mentioned the dangers/shortcomings of setting the recursion limit too high, I wouldn't have downvoted. But this answer, as it stands, is simply misleading: It implies that sys.setrecursionlimit solves OP's problem, and is the best/a good solution at that, even though it doesn't (in a subtle way that's bound to bite you in the ass). Maybe I'm reacting more harshly because I've already dealt with the consequences of thoughtless increase of the recursion limit.
1

As freakish suggested, you could do this:

def harmonic_sum(n):
    return sum([1/float(x) for x in range(1, n + 1)])

2 Comments

No, No...I am going to do EVERYTHING for 3 upcoming month using recursion and avoiding loops in python. I am so sick and tired of not able to use recursion promptly. List comprehension is good but my goal is to learn recursion in such a way that it becomes my second nature.
@Jack_of_All_Trades That's a fine goal. In my experience, I use recursion first because it makes it more readable. Only once I've hammered it out in recursion, will I try to make it iterable (because recursion is a memory hog in a lot of cases).

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.