0

For the below code,

def squareroot(a):
    x = 1
    def apply(x):
        if x*x == a:
            return x
        else:
            return apply((x + a/x)/2)

result = squareroot(2)
print(result)

the following error is reported:

RuntimeError: maximum recursion depth exceeded in comparison

Do we have any 'option' to pass python interpreter which increases the size of stack frame before running this code?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
overexchange
  • 15,768
  • 30
  • 152
  • 347
  • Languages like C has separate stack segment which is limited in size. But "everything is an object" in python. For example `code` `stack frames` are also stored as objects in python. I learnt objects are stored in heap area. Do we have any constraint to maintain limit for the depth of stackframes in heap? – overexchange Feb 19 '15 at 09:05
  • Not answer to the question, but I would add "if (x + a/x)/2 == x: return x" to avoid infinite recursion – tzunghaor Feb 19 '15 at 09:14
  • 1
    For the topic given in the title, see answers for the question hinted by @KasraAD. For this specific code I would recommend however to change to an iterative implementation (i.e. loop) - its not a problem _requiring_ recursion and also does not suggest this very strongly either (imho). – guidot Feb 19 '15 at 09:47

0 Answers0