solution to project euler, problem 14, using dynamic programming