before I start I want to clarify that I am not looking for code examples to get the answer; that would defeat the object of Project Euler.
The problem
If you start div
at 2 and count up instead of down, and divide it out from the number when the modulo is zero, you gain two big advantages that are useful here:
div
is prime, since it can't be composite because any prime factors smaller than it would already have been divided out.You could then also break once div*div
is greater than the remaining number, as you know at that point that it must be a prime. This is because any divisors greater than the square root are "paired" with one less than the square root. However, since this is an "easy" problem, this optimization is not needed here (although it is useful for later problems).