Find all divisors of first N natural numbers
Last Updated :
03 Feb, 2023
Given an integer N, the task is to find all the divisors of numbers from 1 to N.
Note: 1 ? N ? 100000
Examples:
Input: N = 2
Output:
1 –>1
2 –>1, 2
Input: N = 5
Output:
1 –>1
2 –>1, 2
3 –>1, 3
4 –>1, 2, 4
5 –>1, 5
Naive Approach:
- Iterate over first N natural numbers using a loop variable (say i)
- Iterate over the natural numbers from 1 to i with a loop variable (say j) and check that i % j == 0. Then j is a divisor of the natural number i.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void factors( int n)
{
int i, j;
cout << "1 -->1\n" ;
for (i = 2; i <= n; i++) {
cout << i << " -->" ;
for (j = 1; j <= i / 2; j++) {
if (i % j == 0)
cout << j << ", " ;
}
cout << i << "\n" ;
}
}
int main()
{
int n = 5;
factors(n);
}
|
Java
import java.io.*;
public class GFG{
static void factors( int n)
{
int i, j;
System.out.print( "1 -->1\n" );
for (i = 2 ; i <= n; i++)
{
System.out.print(i + " -->" );
for (j = 1 ; j <= i / 2 ; j++)
{
if (i % j == 0 )
System.out.print(j + ", " );
}
System.out.print(i + "\n" );
}
}
public static void main(String[] args)
{
int n = 5 ;
factors(n);
}
}
|
Python3
def factors(n):
i = 0 ; j = 0 ;
print ( "1 -->1" );
for i in range ( 2 , n + 1 ):
print (i, "-->" , end = "");
for j in range ( 1 , (i / / 2 ) + 1 ):
if (i % j = = 0 ):
print (j, "," , end = "");
print (i, end = "\n" );
n = 5 ;
factors(n);
|
C#
using System;
class GFG{
static void factors( int n)
{
int i, j;
Console.Write( "1 -->1\n" );
for (i = 2; i <= n; i++)
{
Console.Write(i + " -->" );
for (j = 1; j <= i / 2; j++)
{
if (i % j == 0)
Console.Write(j + ", " );
}
Console.Write(i + "\n" );
}
}
public static void Main()
{
int n = 5;
factors(n);
}
}
|
Javascript
<script>
function factors(n)
{
let i, j;
document.write( "1 -->1<br>" );
for (i = 2; i <= n; i++) {
document.write(i + " -->" );
for (j = 1; j <= parseInt(i / 2); j++) {
if (i % j == 0)
document.write(j + ", " );
}
document.write(i + "<br>" );
}
}
let n = 5;
factors(n);
</script>
|
Output:
1 -->1
2 -->1, 2
3 -->1, 3
4 -->1, 2, 4
5 -->1, 5
Time Complexity: O(N2)
Auxiliary Space: O(1)
Better Approach:
- Iterate over the first N natural numbers using a loop variable.
- For the number to find its divisors iterate from 2 to that number and check any one of them is a divisor of the given number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void factors( int n)
{
int i, j;
cout << "1 -->1\n" ;
for (i = 2; i <= n; i++) {
cout << i << " -->" ;
for (j = 1; j * j <= i; j++) {
if (i % j == 0){
cout << j << ", " ;
if (i / j != j)
cout << i/j << ", " ;
}
}
cout << "\n" ;
}
}
int main()
{
int n = 5;
factors(n);
}
|
Java
import java.util.*;
class GFG{
static void factors( int n)
{
int i, j;
System.out.print( "1 -->1\n" );
for (i = 2 ; i <= n; i++)
{
System.out.print(i + " -->" );
for (j = 1 ; j * j <= i; j++)
{
if (i % j == 0 )
{
System.out.print(j + ", " );
if (i / j != j)
System.out.print(i / j + ", " );
}
}
System.out.print( "\n" );
}
}
public static void main(String args[])
{
int n = 5 ;
factors(n);
}
}
|
Python3
def factors(n):
print ( "1 -->1" );
for i in range ( 2 , n + 1 ):
print (i, " -->" , end = "");
for j in range ( 1 , int ( pow (i, 1 ))):
if (i % j = = 0 ):
print (j, ", " , end = "");
if (i / / j ! = j):
print (i / / j, ", " , end = "");
print (end = "\n" );
if __name__ = = '__main__' :
n = 5 ;
factors(n);
|
C#
using System;
class GFG{
static void factors( int n)
{
int i, j;
Console.Write( "1 -->1\n" );
for (i = 2; i <= n; i++)
{
Console.Write(i + " -->" );
for (j = 1; j * j <= i; j++)
{
if (i % j == 0)
{
Console.Write(j + ", " );
if (i / j != j)
Console.Write(i / j + ", " );
}
}
Console.Write( "\n" );
}
}
public static void Main()
{
int n = 5;
factors(n);
}
}
|
Javascript
<script>
function factors(n)
{
let i, j;
document.write( "1 -->1<br>" );
for (i = 2; i <= n; i++)
{
document.write(i + " -->" );
for (j = 1; j * j <= i; j++)
{
if (i % j == 0)
{
document.write(j + ", " );
if (parseInt(i / j) != j)
document.write(parseInt(i/j) + ", " );
}
}
document.write( "<br>" );
}
}
let n = 5;
factors(n);
</script>
|
Output:
1 -->1
2 -->1, 2,
3 -->1, 3,
4 -->1, 4, 2,
5 -->1, 5,
Time Complexity: O(N*sqrt(N))
Auxiliary Space: O(1)
As constant extra space is used.
Efficient Approach: The idea is to precompute the factors of the numbers with the help of the Sieve of Eratosthenes. Then finally iterate over the first N natural numbers to find the factors.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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);
}
}
void findNFactors( int n){
for ( int i = 1; i <= n; i++){
cout << i << "-->" ;
for ( auto &divi: divisor[i]){
cout << divi << ", " ;
}
cout << "\n" ;
}
}
int main()
{
int n = 5;
sieve();
findNFactors(n);
}
|
Java
import java.util.*;
class GFG{
static int MAX = ( int ) 1e5;
static Vector<Integer> []divisor = new Vector[MAX + 1 ];
static void sieve()
{
for ( int i = 1 ; i <= MAX; ++i)
{
for ( int j = i; j <= MAX; j += i)
divisor[j].add(i);
}
}
static void findNFactors( int n)
{
for ( int i = 1 ; i <= n; i++)
{
System.out.print(i+ "-->" );
for ( int divi: divisor[i])
{
System.out.print(divi+ ", " );
}
System.out.print( "\n" );
}
}
public static void main(String[] args)
{
int n = 5 ;
for ( int i = 0 ; i < divisor.length; i++)
divisor[i] = new Vector<Integer>();
sieve();
findNFactors(n);
}
}
|
Python3
MAX = 100001
divisor = [[] for x in range ( MAX )]
def sieve():
for i in range ( 1 , MAX ):
for j in range (i, MAX , i):
divisor[j].append(i)
def findNFactors (n):
for i in range ( 1 , n + 1 ):
print (i, " --> " , end = '')
for divi in divisor[i]:
print (divi, ", " , end = '')
print ()
if __name__ = = '__main__' :
n = 5
sieve()
findNFactors(n)
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static int MAX = ( int ) 1e5;
static List< int > []divisor = new List< int >[MAX + 1];
static void sieve()
{
for ( int i = 1; i <= MAX; ++i)
{
for ( int j = i; j <= MAX; j += i)
divisor[j].Add(i);
}
}
static void findNFactors( int n)
{
for ( int i = 1; i <= n; i++)
{
Console.Write(i+ "-->" );
foreach ( int divi in divisor[i])
{
Console.Write(divi+ ", " );
}
Console.Write( "\n" );
}
}
public static void Main(String[] args)
{
int n = 5;
for ( int i = 0; i < divisor.Length; i++)
divisor[i] = new List< int >();
sieve();
findNFactors(n);
}
}
|
Javascript
<script>
var MAX = 100000;
var divisor = Array.from(Array(MAX+1),()=> Array(0));
function sieve()
{
for ( var i = 1; i <= MAX; ++i) {
for ( var j = i; j <= MAX; j += i)
divisor[j].push(i);
}
}
function findNFactors(n){
for ( var i = 1; i <= n; i++){
document.write( i + "-->" );
for ( var j =0; j< divisor[i].length;j++){
document.write( divisor[i][j] + ", " );
}
document.write( "<br>" );
}
}
var n = 5;
sieve();
findNFactors(n);
</script>
|
Output:
1-->1,
2-->1, 2,
3-->1, 3,
4-->1, 2, 4,
5-->1, 5,
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...