Recursion with versus without memoization
I got homework in school to calculate Catalan number with recursion: 1st without memoization def catalan_rec(n): res = 0 if n == 0: return 1 else: for i in range (n): res += (catalan_rec(i))*(catalan_rec(n-1-i)) return res 2nd with: def catalan_mem(n, memo = None): if memo == None: memo = {0: 1} res = 0 if n not in memo: for i in range (n): res += (catalan_mem(i))*(catalan_mem(n-1-i)) memo[n] = res return memo[n] The weirdest thing happened to me: the memoization takes twice much time! When it should be the other way around! Can someone please explain this to me? This question inspired me to