elegent solution for problem 8