Solution for problem 39