largestPrimeFactor function
Returns the largest prime factor of n
.
For example, 120 returns 5.
Implementation
int largestPrimeFactor(int n) {
int maxp = 1;
for (int i = 0, p = 2;;) {
if (p * p > n) break;
if (n % p != 0) {
i += 1;
p = primes.getPrime(i);
} else {
if (p > maxp) maxp = p;
n ~/= p;
}
}
if (n > maxp) maxp = n;
return maxp;
}