Solved problem 31 using recursive solution!