Given a number N, the task is to print all distinct divisors of N^{2}.
Examples:
Input: N = 4
Output: 1 2 4 8 16
Explanation:
N = 4, N^{2} = 16
Divisors of 16 are: 1 2 4 8 16Input: N = 8
Output: 1 2 4 8 16 32 64
Naive Approach:
Find all divisors of a natural number using sqrt(N) approach. But this solution is not efficient as the time complexity would be O(N).
Efficient Approach:
- We try to generate divisors of N^{2} from divisors of N using the sqrt(N) approach. As,
Divisors of N^{2} = All distinct numbers obtained from pairwise multiplication of divisors of N.
For example: If N = 4, to generate divisors of 4^{2} = 16, we would first calculate the divisors of 4 = 1, 2, 4. Now we will iterate over this generated divisors to calculate divisors of 4^{2} that are 1, 2, 4, 8, and 16.
Below is the implementation of the above approach.
C++
// C++ code to print all // divisors of N*N using N #include <bits/stdc++.h> using namespace std; // Function to find Divisor of N void DivisorOfN(vector< int >& v, map< int , bool >& marked, int n) { // sqrt(N) approach // to find divisors of N for ( int i = 1; i <= sqrt (n); i++) { if (n % i == 0) { if (n / i == i) { v.push_back(i); marked[i] = true ; } else { v.push_back(i); v.push_back(n / i); marked[i] = true ; marked[n / i] = true ; } } } } // Function to print all divisor of N*N void PrintDivisors( int n) { // Vector v to store divisors of n vector< int > v; // Map to avoid repeated divisors map< int , bool > marked; // Store all divisor of n DivisorOfN(v, marked, n); int size = v.size(); // Iterating over vector v // to generate divisors of N*N for ( int i = 0; i < size; i++) { for ( int j = i; j < size; j++) { int check = v[i] * v[j]; // Checking if element is // already present if (marked[check] != true ) { v.push_back(v[i] * v[j]); // marking element true // after adding in vector marked[v[i] * v[j]] = true ; } } } sort(v.begin(), v.end()); printf ( "Divisors of %d are: " , n * n); for ( int i = 0; i < v.size(); i++) { printf ( "%d " , v[i]); } printf ( "\n" ); } // Driver Code int main() { PrintDivisors(4); PrintDivisors(8); PrintDivisors(10); return 0; } |
Python3
# Python3 code to print all # divisors of N*N using from math import sqrt # Function to find Divisor of N def DivisorOfN(v, marked, n): # sqrt(N) approach # to find divisors of N for i in range ( 1 , int (sqrt(n)) + 1 , 1 ): if (n % i = = 0 ): if (n / / i = = i): v.append(i) marked[i] = True else : v.append(i) v.append(n / / i) marked[i] = True marked[n / / i] = True # Function to print all divisor of N*N def PrintDivisors(n): # Vector v to store divisors of n v = [] # Map to avoid repeated divisors marked = {i: False for i in range ( 1000 )} # Store all divisor of n DivisorOfN(v, marked, n) size = len (v) # Iterating over vector v # to generate divisors of N*N for i in range (size): for j in range (i,size, 1 ): check = v[i] * v[j] # Checking if element is # already present if (marked[check] ! = True ): v.append(v[i] * v[j]) # marking element true # after adding in vector marked[v[i] * v[j]] = True v.sort(reverse = False ) print ( "Divisors of" ,n * n, "are: " ,end = "") for i in range ( len (v)): print (v[i],end = " " ) print ( "\n" ,end = "") # Driver Code if __name__ = = '__main__' : PrintDivisors( 4 ) PrintDivisors( 8 ) PrintDivisors( 10 ) # This code is contributed by Bhupendra_Singh |
Divisors of 16 are: 1 2 4 8 16 Divisors of 64 are: 1 2 4 8 16 32 64 Divisors of 100 are: 1 2 4 5 10 20 25 50 100
Time Complexity: O(sqrt(N) + a^{2}) where, a is number of divisor of N.
Note: How this approach is different from Find all divisors of a natural number?
Let N = 5, therefore we need to find all divisors of 25.
- Using approach used in Find all divisors of a natural number: we will iterate using i from 1 to sqrt(25) = 5 and check for i and n/i.
Time Complexity: O(sqrt(25)) - Using approach used in this article: we will find divisor of 5 by using the above-mentioned articles approach which will be done in sqrt(5) time complexity. Now for all divisor of 5 i.e. 1, 5 we will store this in an array and multiply it pairwise with the help of 2 loops { (1*1, 1*5, 5*1, 5*5) } and choose the unique ones i.e. 1, 5, 25. This will take a^2 time (where a is the number of the divisor of 5, which is 2 here)
Time Complexity: O(sqrt(5) + 2^2)
This article only works better than the above-mentioned article when the number of divisors of the number is less.
Recommended Posts:
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Sum of all prime divisors of all the numbers in range L-R
- Find the number of divisors of all numbers in the range [1, n]
- Find all divisors of first N natural numbers
- Sum of product of all subsets formed by only divisors of N
- Check if the given array contains all the divisors of some integer
- Sum of product of proper divisors of all Numbers lying in range [L, R]
- Sum of all divisors from 1 to N | Set 2
- Sum of all perfect square divisors of numbers from 1 to N
- Sum of all divisors from 1 to N | Set 3
- Find two numbers whose divisors are given in a random order
- Find A and B from list of divisors
- Create a Graph by connecting divisors from N to M and find shortest path
- Find number from its divisors
- Count divisors of array multiplication
- Number of divisors of product of N numbers
- Count elements in the given range which have maximum number of divisors
- First triangular number whose number of divisors exceeds N
- Count number of integers less than or equal to N which has exactly 9 divisors
- Numbers in range [L, R] such that the count of their divisors is both even and prime
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.