Problem 8 solution