Skip to main content

All divisors of a Number

Problem Statement:​

Given an integer n, print all the divisors of N in the ascending order.

  • Example:

    Examples:

    Input :n =20
    Output:1 2 4 5 10 20
    Explanation:20 is completely divisible by 1, 2, 4, 5, 10 and 20.

    Input:n =21191
    Output:1 21191
    Explanation: As 21191 is a prime number, it has only 2 factors(1 and the number itself).


✅ Solution: Divisor Enumeration up to √N​

class Solution {
public:
void print_divisors(int n) {
vector<int> res;

// Iterate from 1 to √n
for(int i = 1; i <= sqrt(n); i++){
if(n % i == 0){
if(n / i == i){
// i is a perfect square divisor (e.g., √n)
res.push_back(i);
}
else{
// Add both divisors: i and n/i
res.push_back(i);
res.push_back(n / i);
}
}
}

// Sort all divisors before printing
sort(res.begin(), res.end());

// Print each divisor
for(auto it : res) cout << it << " ";
}
};


📝 How It Works​

  • For each number i from 1 to √n, check if i divides n.
  • If yes, both i and n/i are divisors.
  • Take care not to insert duplicates (when i == n/i, like for perfect squares).
  • Finally, sort and print all collected divisors.

⏱️ Time & Space Complexity​

MetricValue
⏱️ TimeO(√N + K log K) where K is number of divisors
💾 SpaceO(K) to store divisors

⚠️ Edge Cases​

  • n = 1 → output is just 1
  • n = prime → output is 1 and n
  • Handles perfect squares correctly (e.g., 36 includes 6 only once)

  • Count number of divisors
  • Sum of all divisors
  • GCD / LCM based problems
  • Efficient Sieve + divisor functions
🚀 Level Up Your Engineering Mindset

Never Study in Isolation. Join Binary Dose.

Connect with fellow engineers, clear conceptual doubts, and get 2 visual breakdowns & engineering mental models daily across our community channels.

Discord
Official Hub

Discord Community

Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.

Join Discord Server→
⚡
Daily Knowledge Drops

Daily Visual Deep-Dives

Bite-sized system design schematics, core OS internals, and DSA intuition served right into your feeds every single day.

💡Have handwritten notes, cheat-sheets, or want to publish a technical article?
Learn How to Contribute→