factors of n optimised code code example
Example: c++ find number of divisors
// https://www.geeksforgeeks.org/count-divisors-n-on13/
int countDivisors(int n) {
int cnt = 0;
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
// If divisors are equal,
// count only one
if (n / i == i)
cnt++;
else // Otherwise count both
cnt = cnt + 2;
}
}
return cnt;
}