Code for problem 41.