Solved problem 9. Ported old solutions to Swift 1.1.