Added more general iterative solution
Problem 2: iterative solution