problem15 with memoization (refactor)