Much faster. Runs to 999999 in < 2 minutes, was > 20 minutes with naive approach