Solve problem 15 much more efficiently