+ 1
How can a recursive function works perfectly in a for loop with large numbers while calling it too many times raises an error?
What I know is, a recursive function is a function that calls itself multiple times. It raises a RecursionError if it is called too many times. However, in the code example below, the fib() function works perfectly inside a for loop that iterates from 1 to 2000, while calling it outside the loop, passing only 1000 to it immediately raises a RecursionError. Can someone explain me why? https://sololearn.com/compiler-playground/ctNkN4ckuEjK/?ref=app
4 Answers
+ 3
Dragon RB Ah okay, then it's probably because of that lru_cache, that prevents a stack overflow.
0
My understanding is that recursion can be fairly intensive on resources, so maybe that's what occurs here.
I think I read some good threads here about it:
https://www.sololearn.com/discuss/1671835/?ref=app
https://www.sololearn.com/discuss/307407/?ref=app
https://www.sololearn.com/discuss/120350/?ref=app
0
It only works perfect in the loop because of "Execution Timet Out" when it reach 70 in the loop. It's the limitation on the playground that prevents it from causing a stack overflow.
If you run the code on a pc, then you would probably quite quickly get a stack overflow.
0
Jan Nope! I ran the code on my IDE.