Code for problem 371D.