To begin with, when you look at the divisors, you no longer need to go beyond the root square of the number, because each divisor under the square root has the equivalent above.
n = a * b => a <= sqrt(n) or b <= sqrt(n)
Then you need to calculate the other side of the division:
double root = Math.sqrt(num); for (int x = 1; x < root; x++) { if (num % x == 0) { counter += 2; } }
The square root is special because it counts only once if it is an integer:
if ((double) ((int) root) == root) { counter += 1; }
source share