Find all factors of a Natural Number
Last Updated :
13 Dec, 2023
Given a natural number n, print all distinct divisors of it.
Examples:
Input : n = 10
Output: 1 2 5 10
Input: n = 100
Output: 1 2 4 5 10 20 25 50 100
Input: n = 125
Output: 1 5 25 125
Note that this problem is different from finding all prime factors.
A Naive Solution would be to iterate all the numbers from 1 to n, checking if that number divides n and printing it. Below is a program for the same:
C++
#include <iostream>
using namespace std;
void printDivisors( int n)
{
for ( int i = 1; i <= n; i++)
if (n % i == 0)
cout << " " << i;
}
int main()
{
cout << "The divisors of 100 are: \n" ;
printDivisors(100);
return 0;
}
|
C
#include<stdio.h>
void printDivisors( int n)
{
for ( int i=1;i<=n;i++)
if (n%i==0)
printf ( "%d " ,i);
}
int main()
{
printf ( "The divisors of 100 are: \n" );
printDivisors(100);
return 0;
}
|
Java
class Test
{
static void printDivisors( int n)
{
for ( int i= 1 ;i<=n;i++)
if (n%i== 0 )
System.out.print(i+ " " );
}
public static void main(String args[])
{
System.out.println( "The divisors of 100 are: " );
printDivisors( 100 );;
}
}
|
Python3
def printDivisors(n) :
i = 1
while i < = n :
if (n % i = = 0 ) :
print (i,end = " " )
i = i + 1
print ( "The divisors of 100 are: " )
printDivisors( 100 )
|
C#
using System;
class GFG {
static void printDivisors( int n)
{
for ( int i = 1; i <= n; i++)
if (n % i == 0)
Console.Write( i + " " );
}
public static void Main()
{
Console.Write( "The divisors of" ,
" 100 are: " );
printDivisors(100);;
}
}
|
PHP
<?php
function printDivisors( $n )
{
for ( $i = 1; $i <= $n ; $i ++)
if ( $n % $i == 0)
echo $i , " " ;
}
echo "The divisors of 100 are:\n" ;
printDivisors(100);
?>
|
Javascript
<script>
function printDivisors(n)
{
for (i=1;i<=n;i++)
if (n%i==0)
document.write(i+ " " );
}
document.write( "The divisors of 100 are:" + "<br>" );
printDivisors(100);
</script>
|
Output:
The divisors of 100 are:
1 2 4 5 10 20 25 50 100
Time Complexity : O(n)
Auxiliary Space : O(1)
Can we improve the above solution?
If we look carefully, all the divisors are present in pairs. For example if n = 100, then the various pairs of divisors are: (1,100), (2,50), (4,25), (5,20), (10,10)
Using this fact we could speed up our program significantly.
We, however, have to be careful if there are two equal divisors as in the case of (10, 10). In such case, we’d print only one of them.
Below is an implementation for the same:
C++
#include <iostream>
#include <math.h>
using namespace std;
void printDivisors( int n)
{
for ( int i=1; i<= sqrt (n); i++)
{
if (n%i == 0)
{
if (n/i == i)
cout << " " << i;
else
cout << " " << i << " " << n/i;
}
}
}
int main()
{
cout << "The divisors of 100 are: \n" ;
printDivisors(100);
return 0;
}
|
C
#include <stdio.h>
#include <math.h>
void printDivisors( int n)
{
for ( int i=1; i<= sqrt (n); i++)
{
if (n%i == 0)
{
if (n/i == i)
printf ( "%d " , i);
else
printf ( "%d %d " , i, n/i);
}
}
}
int main()
{
printf ( "The divisors of 100 are: \n" );
printDivisors(100);
return 0;
}
|
Java
class Test
{
static void printDivisors( int n)
{
for ( int i= 1 ; i<=Math.sqrt(n); i++)
{
if (n%i== 0 )
{
if (n/i == i)
System.out.print( " " + i);
else
System.out.print(i+ " " + n/i + " " );
}
}
}
public static void main(String args[])
{
System.out.println( "The divisors of 100 are: " );
printDivisors( 100 );;
}
}
|
Python3
import math
def printDivisors(n) :
i = 1
while i < = math.sqrt(n):
if (n % i = = 0 ) :
if (n / i = = i) :
print (i,end = " " )
else :
print (i , int (n / i), end = " " )
i = i + 1
print ( "The divisors of 100 are: " )
printDivisors( 100 )
|
C#
using System;
class GFG {
static void printDivisors( int n)
{
for ( int i = 1; i <= Math.Sqrt(n);
i++)
{
if (n % i == 0)
{
if (n / i == i)
Console.Write(i + " " );
else
Console.Write(i + " "
+ n / i + " " );
}
}
}
public static void Main()
{
Console.Write( "The divisors of "
+ "100 are: \n" );
printDivisors(100);
}
}
|
PHP
<?php
function printDivisors( $n )
{
for ( $i = 1; $i <= sqrt( $n ); $i ++)
{
if ( $n % $i == 0)
{
if ( $n / $i == $i )
echo $i , " " ;
else
echo $i , " " , $n / $i , " " ;
}
}
}
echo "The divisors of 100 are: \n" ;
printDivisors(100);
?>
|
Javascript
<script>
function printDivisors(n)
{
for (let i = 1; i <= Math.sqrt(n); i++)
{
if (n % i == 0)
{
if (parseInt(n / i, 10) == i)
document.write(i);
else
document.write(i + " " +
parseInt(n / i, 10) + " " );
}
}
}
document.write( "The divisors of 100 are: </br>" );
printDivisors(100);
</script>
|
Output:
The divisors of 100 are:
1 100 2 50 4 25 5 20 10
Time Complexity: O(sqrt(n))
Auxiliary Space : O(1)
However there is still a minor problem in the solution, can you guess?
Yes! the output is not in a sorted fashion which we had got using the brute-force technique. Please refer below for an O(sqrt(n)) time solution that prints divisors in sorted order.
Find all divisors of a natural number | Set 2
Share your thoughts in the comments
Please Login to comment...