+ 3

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

14th Feb 2025, 12:23 PM
Dragon RB
Dragon RB - avatar
19 Respostas
+ 5
Dragon RB The lru cache seems to work better if used repeatedy in the for loop. Jan , i was thinking the same thing, but I modified the code to be able to see the end values and it seems that fib is performing better inside the for loop. I played around with the code a bit... clearing the cache after each iteration stopped the strange performance boost. So the lru_cache is indeed doing something unexpected when used repeatedly. perhaps some memory swapping is happening which allows the cache to store the bigger values in the for loop as opposed to the fib function being called only once when it's outside the for loop... not sure. https://sololearn.com/compiler-playground/chW9m3fjo6p4/?ref=app
15th Feb 2025, 9:09 AM
Bob_Li
Bob_Li - avatar
+ 6
Dragon RB Ah okay, then it's probably because of that lru_cache, that prevents a stack overflow.
14th Feb 2025, 3:50 PM
Jan
Jan - avatar
+ 6
Bob_Li and Jan , you guys are stars! Thanks for your efforts and sharing of knowledge.
15th Feb 2025, 4:37 PM
Ausgrindtube
Ausgrindtube - avatar
+ 4
Bob_Li I can only speak for myself now, but the best thing is to avoid recursive functions.
15th Feb 2025, 10:14 AM
Jan
Jan - avatar
+ 4
Dragon RB , In the for loop, each call to fib(x) always uses the results of previous calls to fib(for n<x) so it doesn't use recursion. Whereas a single call to a single fib(for any x greater than 499) violates the limit on recursive calls set in the system by default. By changing this limit, you can get the calculations you need. https://sololearn.com/compiler-playground/ckgvJ9sMW9g5/?ref=app
15th Feb 2025, 6:46 PM
Vitaly Sokol
Vitaly Sokol - avatar
+ 4
Vitaly Sokol wow. There's a lot of good to know techniques in your code. šŸ‘ but Jan's iterative fib code can do that, too, with less resource allocations. https://sololearn.com/compiler-playground/c3tI84FhqYgQ/?ref=app
16th Feb 2025, 6:08 AM
Bob_Li
Bob_Li - avatar
+ 3
Jan agreed. it's easy to write, but often resource-heavy to execute.
15th Feb 2025, 12:19 PM
Bob_Li
Bob_Li - avatar
+ 3
Vitaly Sokol , Jan šŸ‘šŸ‘šŸ‘
15th Feb 2025, 11:54 PM
Bob_Li
Bob_Li - avatar
+ 3
Bob_Li your right, so, probably my solution is related to Dragon RB's question, it's not only to calculate the nth Fibonacci term, but also to be able to instantly access any fibo value from the range(n)
16th Feb 2025, 6:52 AM
Vitaly Sokol
Vitaly Sokol - avatar
+ 2
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.
14th Feb 2025, 3:06 PM
Jan
Jan - avatar
+ 2
Jan Nope! I ran the code on my IDE.
14th Feb 2025, 3:31 PM
Dragon RB
Dragon RB - avatar
+ 2
Vitaly Sokol , I am not so sure.. As I use the sys.setrecursionlimit() (let's say I'll set the limit to 100000) And then call the fib() function (with lru_cache to significantly boost the performance) with high values like 50000, it instead says "Segmentation fault" even with the use of sys.set_int_max_str_digits(). I did a little research and it's probably kind of like C's problem, not Python's. But how?
16th Feb 2025, 1:42 AM
Dragon RB
Dragon RB - avatar
+ 1
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
14th Feb 2025, 1:07 PM
Ausgrindtube
Ausgrindtube - avatar
+ 1
Bob_Li Thank you!! I understand now.
16th Feb 2025, 1:37 AM
Dragon RB
Dragon RB - avatar
0
What happened
15th Feb 2025, 7:46 AM
Sagar Battula
Sagar Battula - avatar
0
+++++
15th Feb 2025, 7:47 AM
Sagar Battula
Sagar Battula - avatar
0
You can using try and catch
15th Feb 2025, 10:13 PM
Abdelilah Albaroudi
Abdelilah Albaroudi - avatar