Refactored problem 30 to add a functional version