Problem 14 solution