Adds memoization to speed up Problem 12 solution