Queries to print all divisors of number
Last Updated :
01 Aug, 2022
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++
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 1e5;
vector< int > divisor[MAX + 1];
void sieve()
{
for ( int i = 1; i <= MAX; ++i) {
for ( int j = i; j <= MAX; j += i)
divisor[j].push_back(i);
}
}
inline void printDivisor( int & n)
{
for ( auto & div : divisor[n])
cout << div << " " ;
}
int main()
{
sieve();
int n = 10;
cout << "Divisors of " << n << " = " ;
printDivisor(n);
n = 30;
cout << "\nDivisors of " << n << " = " ;
printDivisor(n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int MAX = 100000 ;
static ArrayList<ArrayList<Integer> > divisor
= new ArrayList<ArrayList<Integer> >();
static void sieve()
{
for ( int i = 0 ; i <= MAX; i++)
divisor.add( new ArrayList<Integer>());
for ( int i = 1 ; i <= MAX; ++i) {
for ( int j = i; j <= MAX; j += i)
divisor.get(j).add(i);
}
}
static void printDivisor( int n)
{
for ( int i = 0 ; i < divisor.get(n).size(); i++)
System.out.print((divisor.get(n)).get(i) + " " );
System.out.println();
}
public static void main(String[] args)
{
sieve();
int n = 10 ;
System.out.println( "Divisors of " + n + " = " );
printDivisor(n);
n = 30 ;
System.out.println( "Divisors of " + n + " = " );
printDivisor(n);
}
}
|
Python3
MAX = 100000
divisor = [ list () for _ in range ( MAX + 1 )]
def sieve():
for i in range ( 1 , MAX + 1 ):
for j in range (i, MAX + 1 , i):
divisor[j] = divisor[j] + [i]
def printDivisor(n):
print ( * (divisor[n]))
sieve()
n = 10
print ( "Divisors of" , n, "=" )
printDivisor(n)
n = 30
print ( "Divisors of" , n, "=" )
printDivisor(n)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int MAX = 100000;
static List<List< int > > divisor
= new List<List< int > >();
static void sieve()
{
for ( int i = 0; i <= MAX; i++)
divisor.Add( new List< int >());
for ( int i = 1; i <= MAX; ++i) {
for ( int j = i; j <= MAX; j += i)
divisor[j].Add(i);
}
}
static void printDivisor( int n)
{
foreach ( var div in divisor[n])
Console.Write(div + " " );
}
public static void Main( string [] args)
{
sieve();
int n = 10;
Console.WriteLine( "Divisors of " + n + " = " );
printDivisor(n);
n = 30;
Console.WriteLine( "Divisors of " + n + " = " );
printDivisor(n);
}
}
|
Javascript
let MAX = 1e5;
let divisor = new Array(MAX + 1);
for ( var i = 1; i <= MAX; i++)
divisor[i] = [];
function sieve()
{
for ( var i = 1; i <= MAX; ++i) {
for ( var j = i; j <= MAX; j += i)
divisor[j].push(i);
}
}
function printDivisor(n)
{
for ( var div of divisor[n])
process.stdout.write(div + " " );
}
sieve();
let n = 10;
console.log( "Divisors of " + n + " = " );
printDivisor(n);
n = 30;
console.log( "\nDivisors of " + n + " = " );
printDivisor(n);
|
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...