solution for problem 5 (full marks)