Problem 17 solution