Naive solution to problem 5
Solve problem 5 more intelligently