I played with memoization and recursion in python 3.3
Ignoring the fact that python is the wrong language for this, I found that I get inconsistent results between using functools.lru_cache for memoize and not using functools.lru_cache
I do not change the recursion limit - it remains by default, for me it is 1000.
To test the problem, I wrote a simple recursive function to sum the numbers from 1 to i
def sumtil(i):
"""Recursive function to sum all numbers from 1 through i"""
if i == 1:
return 1
else:
return i+sumtil(i-1)
sumtil(998)
sumtil(999)
Performing this function in normal mode, I can conveniently use sumtil(998)it without allowing recursion restrictions. sumtil(999)or higher throws an exception.
, @functools.lru_cache(), 3 , sumtil(333)
import functools
@functools.lru_cache(maxsize=128)
def sumtil(i):
"""Recursive function to sum all numbers from 1 through i"""
if i == 1:
return 1
else:
return i+sumtil(i-1)
sumtil(332)
sumtil(333)
332 * 3 = 996, 333 * 3 = 999, , lru_cache , .
functools.lru_cache memoize ?