Solved Problem 6 using reduce