Solution for problem 40