Find all divisors of N2 using N
Given a number N, the task is to print all distinct divisors of N2.
Examples:
Input: N = 4
Output: 1 2 4 8 16
Explanation:
N = 4, N2 = 16
Divisors of 16 are: 1 2 4 8 16
Input: N = 8
Output: 1 2 4 8 16 32 64
Naive Approach:
Find all divisors of a natural number using the 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 N2 from divisors of N using the sqrt(N) approach. As
For example: If N = 4, to generate divisors of 42 = 16, we would first calculate the divisors of 4 = 1, 2, 4. Now we will iterate over these generated divisors to calculate divisors of 42 that are 1, 2, 4, 8, and 16.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void DivisorOfN(vector< int >& v,
map< int , bool >& marked,
int 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 ;
}
}
}
}
void PrintDivisors( int n)
{
vector< int > v;
map< int , bool > marked;
DivisorOfN(v, marked, n);
int size = v.size();
for ( int i = 0; i < size; i++) {
for ( int j = i; j < size; j++) {
int check = v[i] * v[j];
if (marked[check] != true ) {
v.push_back(v[i] * v[j]);
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" );
}
int main()
{
PrintDivisors(4);
PrintDivisors(8);
PrintDivisors(10);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static List<Integer> v = new ArrayList<>();
static HashMap<Integer, Boolean> marked = new HashMap<>();
static void DivisorOfN( int n)
{
for ( int i = 1 ; i <= Math.sqrt(n); i++)
{
if (n % i == 0 )
{
if (n / i == i)
{
v.add(i);
marked.put(i, true );
}
else
{
v.add(i);
v.add(n / i);
marked.put(i, true );
marked.put(n / i, true );
}
}
}
}
static void PrintDivisors( int n)
{
DivisorOfN(n);
int size = v.size();
for ( int i = 0 ; i < size; i++)
{
for ( int j = i; j < size; j++)
{
int check = v.get(i) * v.get(j);
if (!marked.containsKey(check))
{
v.add(v.get(i) * v.get(j));
marked.put(v.get(i) * v.get(j), true );
}
}
}
Collections.sort(v);
System.out.print( "Divisors of " + n * n + " are: " );
for ( int i = 0 ; i < v.size(); i++)
{
System.out.print(v.get(i) + " " );
}
System.out.println();
v.clear();
marked.clear();
}
public static void main(String[] args)
{
PrintDivisors( 4 );
PrintDivisors( 8 );
PrintDivisors( 10 );
}
}
|
Python3
from math import sqrt
def DivisorOfN(v, marked, 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
def PrintDivisors(n):
v = []
marked = {i: False for i in range ( 1000 )}
DivisorOfN(v, marked, n)
size = len (v)
for i in range (size):
for j in range (i,size, 1 ):
check = v[i] * v[j]
if (marked[check] ! = True ):
v.append(v[i] * v[j])
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 = "")
if __name__ = = '__main__' :
PrintDivisors( 4 )
PrintDivisors( 8 )
PrintDivisors( 10 )
|
C#
using System;
using System.Collections.Generic;
class GFG {
static List< int > v = new List< int >();
static Dictionary< int , bool > marked = new Dictionary< int , bool >();
static void DivisorOfN( int n)
{
for ( int i = 1; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
if (n / i == i)
{
v.Add(i);
marked[i] = true ;
}
else
{
v.Add(i);
v.Add(n / i);
marked[i] = true ;
marked[n / i] = true ;
}
}
}
}
static void PrintDivisors( int n)
{
DivisorOfN(n);
int size = v.Count;
for ( int i = 0; i < size; i++)
{
for ( int j = i; j < size; j++)
{
int check = v[i] * v[j];
if (!marked.ContainsKey(check))
{
v.Add(v[i] * v[j]);
marked[v[i] * v[j]] = true ;
}
}
}
v.Sort();
Console.Write( "Divisors of " + n * n + " are: " );
for ( int i = 0; i < v.Count; i++)
{
Console.Write(v[i] + " " );
}
Console.WriteLine();
v.Clear();
marked.Clear();
}
static void Main()
{
PrintDivisors(4);
PrintDivisors(8);
PrintDivisors(10);
}
}
|
Javascript
<script>
function DivisorOfN(v, marked, n)
{
for ( var i = 1; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i) {
v.push(i);
marked[i] = true ;
}
else {
v.push(i);
v.push(n / i);
marked[i] = true ;
marked[n / i] = true ;
}
}
}
}
function PrintDivisors(n)
{
var v = [];
var marked = new Map();
DivisorOfN(v, marked, n);
var size = v.length;
for ( var i = 0; i < size; i++) {
for ( var j = i; j < size; j++) {
var check = v[i] * v[j];
if (marked[check] != true ) {
v.push(v[i] * v[j]);
marked[v[i] * v[j]] = true ;
}
}
}
v.sort((a,b)=>a-b)
document.write( "Divisors of " +n*n+ " are: " );
for ( var i = 0; i < v.length; i++) {
document.write(v[i]+ " " );
}
document.write( "<br>" );
}
PrintDivisors(4);
PrintDivisors(8);
PrintDivisors(10);
</script>
|
Output:
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) + a2) where a is a number of divisors of N.
Note: How is this approach different from Find all divisors of a natural number?
Let N = 5, therefore we need to find all divisors of 25.
- Using the 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 the approach used in this article: we will find a 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.
Last Updated :
01 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...