Speed up problem 09
Improve logic for problem 09