Condensed the solution code
Solve problem 23