我正在解決一個涉及查找歷史交易價格的問題。如果價格不適用于時間戳,請回傳上一個時間戳并查找該值。但是,在擺弄 recursion limitssys.setrecursionlimit(limit)時,我發現代碼的遞回部分在將限制設定得太高時不會完全執行。因此,我著手進一步調查這個問題。我撰寫了一個函式,用于在 Python 中列印第 n 個 n_bonacci 系列。這是代碼
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)
它適用于較低的值(1 - 2000)。但是,它不會在更高的時候執行任何操作,并且終端不會輸出任何內容。我把這個問題開始的數字歸零。它從 2206 開始。在 Python 中運行代碼時,我發現它只會在某些時候執行。輸出附在下面。
@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.
uj5u.com熱心網友回復:
你幾乎肯定會用完堆疊空間。遞回在很大程度上取決于可用于存盤先前條件的堆疊記憶體量。
執行緒匯入允許您更改堆疊空間的數量;我建議您嘗試一下,然后在新執行緒中運行您的遞回函式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/434915.html
標籤:python python-3.x recursion
上一篇:聚合陣列的子集
