working solution for problem 3,5,8