Ask your own question, for FREE!
Computer Science 14 Online
OpenStudy (anonymous):

i use aptana studio for executing python file... i made a small program for finding exponent... as in (b^n)... so i used recursion... and wrote: def expo(b,n) if(n==1): return b else: return b * expo(b,n-1) but when i ran it it gave some error as recursion limit exceeded.... why did it happen?...

OpenStudy (anonymous):

The default limit is usually 1000, so if your exponent 'n' is larger than this you will get this error. From the python manual "sys.getrecursionlimit() Return the current value of the recursion limit, the maximum depth of the Python interpreter stack. This limit prevents infinite recursion from causing an overflow of the C stack and crashing Python. It can be set by setrecursionlimit()."

OpenStudy (anonymous):

One other note, since python does not support tail-recursion this is not an option so the best option is likely the iterative approach as below. def expo(b,n): res = 1 for i in range(n): res *= b return res

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!