In the following snippet from https://github.com/nayuki/Project-Euler-solutions/blob/master/p003.java :
private static long smallestFactor(long n) {
for (long i = 2, end = Library.sqrt(n); i <= end; i++) {
if (n % i == 0)
return i;
}
return n; // Prime
}
I was a bit confused with the return n part. Is n going to assume the value of i after it’s returned in the if statement? Why?
No. It returns the unchanged parameter to indicate that it is prime.
If it is not prime, it returns the factor that shows it is not prime.