Solution for problem 40 and 41