Update problem 23

Replaced section of code with 4 loops with more efficient code.