Given a positive integer ‘n’ and query ‘q’. Print all the divisors of number ‘n’.
Input: 6 Output: 1 2 3 6 Explanation Divisors of 6 are: 1, 2, 3, 6 Input: 10 Output: 1 2 5 10
Naive approach is to iterate through 1 to sqrt(n) for every query ‘q’ and print the divisors accordingly. See this to understand more. Time complexity of this approach is q*sqrt(n) which is not sufficient for large number of queries.
Efficient approach is to use factorization by using sieve base approach.
- Create a list of consecutive integers from 1 to ‘n’.
- For any number ‘d’, iterate through all the multiples of ‘d’ i.e., d, 2d, 3d, … etc. Meanwhile push the divisor ‘d’ for every multiples.
// C++ program to print divisors of // number for multiple query #include <iostream> #include <vector> using namespace std; const int MAX = 1e5; // Initialize global divisor vector // array of sequence container vector<int> divisor[MAX + 1]; // Sieve based approach to pre- // calculate all divisors of number void sieve() { for (int i = 1; i <= MAX; ++i) { for (int j = i; j <= MAX; j += i) divisor[j].push_back(i); } } // Utility function to print divisors // of given number inline void printDivisor(int& n) { for (auto& div : divisor[n]) cout << div << " "; } // Driver code int main() { sieve(); int n = 10; cout << "Divisors of " << n << " = "; printDivisor(n); n = 30; cout << "\nDivisors of " << n << " = "; printDivisor(n); return 0; } |
Output Divisors of 10 = 1 2 5 10 Divisors of 30 = 1 2 3 5 6 10 15 303
Time complexity: O(len) for each query, where len is equal to total divisors of number ‘n’.
Auxiliary space: O(MAX)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find sum of divisors of all the divisors of a natural number
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Check if a number has an odd count of odd divisors and even count of even divisors
- Divisors of n-square that are not divisors of n
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Check if count of even divisors of N is equal to count of odd divisors
- Count of elements having odd number of divisors in index range [L, R] for Q queries
- Product of proper divisors of a number for Q queries
- Count of divisors of product of an Array in range L to R for Q queries
- Count of nodes having odd divisors in the given subtree for Q queries
- Check if a number is divisible by all prime divisors of another number
- Find all divisors of a natural number | Set 1
- Find all divisors of a natural number | Set 2
- Sum of all proper divisors of a natural number
- Count all perfect divisors of a number
- Sum of all the prime divisors of a number
- Break a number such that sum of maximum divisors of all parts is minimum
- Find the number of divisors of all numbers in the range [1, n]
- Sum of all second largest divisors after splitting a number into one or more parts
- Generating all divisors of a number using its prime factorization
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

