Adding caching for the recursive solution.

Speed up from O(2^n) to O(n)