final solution for problem 29