It's just a matter of experience.
In the other version you knew not to go above the square root of a million (your test loop only went up to 1000).
You just forgot about it in this version.