Luís Ramalho bio photo

Luís Ramalho

Engineering, Product, Business & Management

Twitter LinkedIn Github Stackoverflow

CountFactors is a problem where one must compute the factors of a given number. Note that before attempting this problem, you should read the material on prime and composite numbers .

We incremente the count by two because based on one divisor, we can find the symmetric divisor. In other words, if a is a divisor of n then n/a is also one. The only case when that does not happen is when is if the number is in the form k^2, meaning that the symmetric divisor of k is also k.

int i = 1;
int result = 0;
while (i < Math.sqrt(n)) {
    if (n % i == 0) {
        result += 2;
    }
    i++;
}
if (Math.pow(i, 2) == n) {
    result += 1;
}
return result;

The full solution can be found here.