Problem 31: complete solution.