solved problem 31 in swift