Solved problem 4

This is not a good solution. It needs optimising.