Problem 5 solution