Input: n =24Output: 8Explanation: Divisors are 1,2,3,4,6,8,12,24.
Notes
The number-of-divisors function is multiplicative: if n = p1^e1 * p2^e2 * ... * pk^ek (prime powers) then the number of divisors is (e1+1)*(e2+1)*...*(ek+1).
#include<vector>usingnamespace std;
classSolution {
public:longlong countDivisors(longlong n) {
if (n ==1) return1;
longlong ans =1;
for (longlong p =2; p * p <= n; ++p) {
if (n % p !=0) continue;
int cnt =0;
while (n % p ==0) { n /= p; ++cnt; }
ans *= (cnt +1);
}
if (n >1) ans *=2; // remaining prime factor
return ans;
}
};
classSolution {
publiclongcountDivisors(long n) {
if (n == 1) return 1;
long ans = 1;
for (long p = 2; p * p <= n; ++p) {
if (n % p != 0) continue;
int cnt = 0;
while (n % p == 0) { n /= p; ++cnt; }
ans *= (cnt + 1);
}
if (n > 1) ans *= 2;
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
funcountDivisors(n0: Long): Long {
var n = n0
if (n ==1L) return1Lvar ans = 1Lvar p = 2Lwhile (p * p <= n) {
if (n % p ==0L) {
var cnt = 0Lwhile (n % p ==0L) { n /= p; cnt++ }
ans *= (cnt + 1)
}
p++ }
if (n > 1L) ans *=2Lreturn ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution:
defcountDivisors(self, n: int) -> int:
if n ==1:
return1 ans =1 p =2while p * p <= n:
if n % p ==0:
cnt =0while n % p ==0:
n //= p
cnt +=1 ans *= (cnt +1)
p +=1if n >1:
ans *=2return ans
Precompute all primes up to sqrt(max_n) using a sieve once, then factor each query n by dividing only by those primes. This saves time when answering many queries or factoring large n repeatedly.
#include<vector>usingnamespace std;
classSolution {
public:private: vector<int> sieve(int limit) {
vector<char> isPrime(limit +1, true);
isPrime[0] = isPrime[1] = false;
for (int p =2; p * p <= limit; ++p) if (isPrime[p])
for (int j = p * p; j <= limit; j += p) isPrime[j] = false;
vector<int> primes;
for (int i =2; i <= limit; ++i) if (isPrime[i]) primes.push_back(i);
return primes;
}
public:longlong countDivisors(longlong n, const vector<int>& primes) {
if (n ==1) return1;
longlong ans =1;
for (int p : primes) {
if (1LL* p * p > n) break;
if (n % p !=0) continue;
int cnt =0;
while (n % p ==0) { n /= p; ++cnt; }
ans *= (cnt +1);
}
if (n >1) ans *=2;
return ans;
}
};
import java.util.ArrayList;
import java.util.List;
classSolution {
private List<Integer>sieve(int limit) {
boolean[] isPrime =newboolean[limit + 1];
java.util.Arrays.fill(isPrime, true);
isPrime[0]= isPrime[1]=false;
for (int p = 2; p * p <= limit; ++p) if (isPrime[p])
for (int j = p * p; j <= limit; j += p) isPrime[j]=false;
List<Integer> primes =new ArrayList<>();
for (int i = 2; i <= limit; ++i) if (isPrime[i]) primes.add(i);
return primes;
}
publiclongcountDivisors(long n, List<Integer> primes) {
if (n == 1) return 1;
long ans = 1;
for (int p : primes) {
if (1L * p * p > n) break;
if (n % p != 0) continue;
int cnt = 0;
while (n % p == 0) { n /= p; ++cnt; }
ans *= (cnt + 1);
}
if (n > 1) ans *= 2;
return ans;
}
}
classSolution:
def_sieve(self, limit: int) -> list[int]:
is_prime = [True] * (limit +1)
is_prime[0] = is_prime[1] =False p =2while p * p <= limit:
if is_prime[p]:
for j in range(p * p, limit +1, p):
is_prime[j] =False p +=1return [i for i in range(2, limit +1) if is_prime[i]]
defcountDivisors(self, n: int, primes: list[int]) -> int:
if n ==1:
return1 ans =1for p in primes:
if p * p > n:
breakif n % p !=0:
continue cnt =0while n % p ==0:
n //= p
cnt +=1 ans *= (cnt +1)
if n >1:
ans *=2return ans
⏰ Time complexity (Method 2): sieve O(limit log log limit) to build primes plus O(#primes + factorization) per query; overall for limit = sqrt(maxN) and Q queries cost is O(limit log log limit + Q * sqrt(n)/log n) in practice.
🧺 Space complexity: O(limit) for the sieve and O(1) additional per query (excluding output).