problem 14
problem 14 + cached iterative