Home > other >  Recursive function in Python shows irregular behavior - it executes only sometimes
Recursive function in Python shows irregular behavior - it executes only sometimes

Time:02-27

I was working on a problem involving looking up historical trading prices. If the price is not available for a timestamp, go back to the previous timestamp and look up the value. However, upon fiddling with recursion limits sys.setrecursionlimit(limit), I found that the recursion part of the code would not execute altogether upon setting a limit too high. So, I set out to investigate this issue further. I wrote a function for printing the n-th n_bonacci series in Python. Here's the code for that

import sys

sys.setrecursionlimit(10000)


def n_bonacci_series(n,  k=1, memo={}):
    if n in memo:
        return memo[n]
    elif n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        memo[n] = n_bonacci_series(n - 1, k,  memo)   \
            n_bonacci_series(n - 2, k, memo)
        return memo[n]


if __name__ == '__main__':
    n = n_bonacci_series(int(sys.argv[1]), int(sys.argv[2]), {})
    print(n)

It works well with lower values (1 - 2000). However, it would not execute anything upon going higher, and the terminal would output nothing. I zeroed in on the number where this problem started. It started with 2206. When running the code in Python, I found that it would execute only sometimes. The output is attached below.

@Animesh ➜ scripts python .\metallic_ratios.py 2206 5
47560826085308642128638691782026383110062745693928884703260494102838475008504779896361090854101455457025401324143044776114166180160702554656438274920361681150989106875091171050407797787515069337246074248523724768358072059714670001942858736056802300373066705493637091637305975712573716191127269704968186479287183399647662677946469037303681204676246925752667295746028054079126466258038707553782482000197471467485598685351978786172553903287644120133460701701937983
@Animesh ➜ scripts python .\metallic_ratios.py 2206 5
47560826085308642128638691782026383110062745693928884703260494102838475008504779896361090854101455457025401324143044776114166180160702554656438274920361681150989106875091171050407797787515069337246074248523724768358072059714670001942858736056802300373066705493637091637305975712573716191127269704968186479287183399647662677946469037303681204676246925752667295746028054079126466258038707553782482000197471467485598685351978786172553903287644120133460701701937983
@Animesh ➜ scripts python .\metallic_ratios.py 2206 5
47560826085308642128638691782026383110062745693928884703260494102838475008504779896361090854101455457025401324143044776114166180160702554656438274920361681150989106875091171050407797787515069337246074248523724768358072059714670001942858736056802300373066705493637091637305975712573716191127269704968186479287183399647662677946469037303681204676246925752667295746028054079126466258038707553782482000197471467485598685351978786172553903287644120133460701701937983
@Animesh ➜ scripts python .\metallic_ratios.py 2206 5
@Animesh ➜ scripts python .\metallic_ratios.py 2206 5
47560826085308642128638691782026383110062745693928884703260494102838475008504779896361090854101455457025401324143044776114166180160702554656438274920361681150989106875091171050407797787515069337246074248523724768358072059714670001942858736056802300373066705493637091637305975712573716191127269704968186479287183399647662677946469037303681204676246925752667295746028054079126466258038707553782482000197471467485598685351978786172553903287644120133460701701937983
@Animesh ➜ scripts python .\metallic_ratios.py 2206 5
@Animesh ➜ scripts python .\metallic_ratios.py 2206 5
@Animesh ➜ scripts python .\metallic_ratios.py 2206 5
@Animesh ➜ scripts python .\metallic_ratios.py 2206 5
@Animesh ➜ scripts python .\metallic_ratios.py 2206 5
47560826085308642128638691782026383110062745693928884703260494102838475008504779896361090854101455457025401324143044776114166180160702554656438274920361681150989106875091171050407797787515069337246074248523724768358072059714670001942858736056802300373066705493637091637305975712573716191127269704968186479287183399647662677946469037303681204676246925752667295746028054079126466258038707553782482000197471467485598685351978786172553903287644120133460701701937983
@Animesh ➜ scripts python .\metallic_ratios.py 2206 5
47560826085308642128638691782026383110062745693928884703260494102838475008504779896361090854101455457025401324143044776114166180160702554656438274920361681150989106875091171050407797787515069337246074248523724768358072059714670001942858736056802300373066705493637091637305975712573716191127269704968186479287183399647662677946469037303681204676246925752667295746028054079126466258038707553782482000197471467485598685351978786172553903287644120133460701701937983
@Animesh ➜ scripts python .\metallic_ratios.py 2206 5
@Animesh ➜ scripts python .\metallic_ratios.py 2206 5
47560826085308642128638691782026383110062745693928884703260494102838475008504779896361090854101455457025401324143044776114166180160702554656438274920361681150989106875091171050407797787515069337246074248523724768358072059714670001942858736056802300373066705493637091637305975712573716191127269704968186479287183399647662677946469037303681204676246925752667295746028054079126466258038707553782482000197471467485598685351978786172553903287644120133460701701937983
@Animesh ➜ scripts python .\metallic_ratios.py 2206 5
@Animesh ➜ scripts

I'm using Python 3.9.7, my processor is Intel Core i5 7200U with 12 GBs of RAM (2133Mhz)

I absolutely do not understand why this is happening. Can someone help me in investigating this issue? If you're running the code in your system, play around with the value of n in order to get to that point of uncertainty, as I think it may have something to do with system configurations.

CodePudding user response:

You're almost certainly running out of stack space. Recursion is heavily dependent upon the amount of stack memory available for storing previous conditions.

The thread import allows you to change the amount of stack space; I suggest you try that, then run your recursive function in a new thread.

  • Related