Count Divisors

83.8k
0

Given a positive integer N, find and return the total count of its divisors.

A divisor is any positive integer that divides N completely, leaving a remainder of 0.

Example 1

Input: n = 12

Output: 6

Explanation: The positive divisors of 12 are 1, 2, 3, 4, 6, and 12. So, the total count is 6.

Example 2

Input: n = 25

Output: 3

Explanation: The positive divisors of 25 are 1, 5, and 25. So, the total count is 3.

Brute Force Approach

The first observation is very direct: every positive divisor of N must lie somewhere between 1 and N. So if the goal is to find all divisors, one simple way is to check every number in that range.

The next observation is that a number should be counted only when it divides N exactly. That means the remainder must be 0. Once this is noticed, the logic becomes straightforward: test each number from 1 to N and count the ones that leave no remainder.

Algorithm

  • Start by keeping a variable named count as 0, because no divisor has been confirmed yet. This variable will store how many valid numbers have been found so far.

  • Move through every number from 1 to N, because every positive divisor of N must lie in this range. No value outside this range can be a positive divisor of N.

  • For each number i, check whether N % i == 0. This remainder check matters because only numbers that divide N exactly should be counted.

  • Whenever the remainder becomes 0, increase count by 1. That step records the fact that one more valid divisor has been discovered.

  • After all numbers have been tested, return count, because by then every possible positive divisor has either been accepted or rejected.

Dry Run

Count Divisors  Brute Force Dry Run

Count Divisors Brute Force Dry Run

Solution

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
/*
Returns the count of positive divisors of n
by checking every number from 1 to n.
*/
int countDivisors(int n) {
// Stores the total number of valid divisors found.
int count = 0;
for (int i = 1; i <= n; i++) {
/* Count only those numbers that divide n exactly. */
if (n % i == 0) {
count++;
}
}
return count;
}
};
// Driver code starts
int main() {
int n = 12;
Solution obj;
cout << obj.countDivisors(n) << endl;
return 0;
}

Complexity Analysis

Time Complexity: O(N), because every number from 1 to N is checked.

Space Complexity: O(1), because only a few variables are used.

Optimal Approach

The smarter idea starts with a small pattern. When 2 divides 12, 6 also appears automatically. When 3 divides 12, 4 also appears. So divisors do not show up alone. They arrive in pairs.

That observation changes the whole approach. Instead of walking all the way to N, only the smaller side of each pair needs to be searched. Once a divisor i is found, its partner N / i is already known. This is why checking only up to sqrt(N) is enough. The only place where extra care is needed is a perfect square such as 36, because the pair becomes (6, 6), and the same divisor must not be counted twice.

Algorithm

  • Start by keeping a variable count as 0, because the answer has not been built yet. This variable will collect the number of divisors as valid pairs are found.

  • Check numbers starting from 1 while i * i <= N. This limit is important because anything larger than sqrt(N) would only repeat information that already comes from a smaller paired divisor.

  • For every i, first test whether it divides N exactly. If it does not, there is no pair to count, so the process simply moves forward.

  • If i divides N, then one divisor pair has been discovered. Usually that means two divisors should be added: i and N / i.

  • Before adding 2, check whether i * i == N. If that happens, both values in the pair are actually the same number, so only 1 should be added. This protects the answer from double-counting the square root in perfect squares.

  • After all possible smaller divisors have been tested, return count, because every valid divisor pair has already been covered.

Key Points

  • For a perfect square like 36, the square root must be counted only once.

Dry Run

Count Divisors Optimal Dry Run

Count Divisors Optimal Dry Run

Solution

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
/*
Returns the count of positive divisors of n
by using divisor pairs up to sqrt(n).
*/
int countDivisors(int n) {
// Stores the total number of divisors counted so far.
int count = 0;
for (int i = 1; i * i <= n; i++) {
// Only exact divisors can form a valid divisor pair.
if (n % i == 0) {
// Count only one divisor when i is the square root of n.
if (i * i == n) {
count += 1;
} else {
// Count both i and n / i because they are distinct divisors.
count += 2;
}
}
}
return count;
}
};
// Driver code starts
int main() {
int n = 12;
Solution obj;
cout << obj.countDivisors(n) << endl;
return 0;
}

Complexity Analysis

Time Complexity: O(sqrt(N)), because only numbers up to the square root are checked.

Space Complexity: O(1), because no extra data structure is needed.

Interview follow-up Questions

If i divides N, then N / i also divides N. So divisors are found in pairs, and checking the smaller side of the pair is enough.

MathsIntroduction to DSA

Read Similar Blogs

Comments0