Code for problem 39F.