Reputation: 11468
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.
Upvotes: 0
Views: 131
Reputation: 25096
As freakish suggested, you could do this:
def harmonic_sum(n):
return sum([1/float(x) for x in range(1, n + 1)])
Upvotes: 1
Reputation: 50560
import sys
sys.setrecursionlimit(9000)
The number 9000
is arbitrary. Pick something relevant for your application.
Upvotes: 1