Count all perfect divisors of a number
Last Updated :
13 Sep, 2023
Given a number n, count total perfect divisors of n. Perfect divisors are those divisors which are square of some integer. For example a perfect divisor of 8 is 4.
Examples:
Input : n = 16
Output : 3
Explanation : There are only 5 divisor of 16:
1, 2, 4, 8, 16. Only three of them are perfect
squares: 1, 4, 16. Therefore the answer is 3
Input : n = 7
Output : 1
Naive approach
A brute force is find all the divisors of a number. Count all divisors that are perfect squares.
C++
#include<bits/stdc++.h>
using namespace std;
bool isPerfectSquare( int n)
{
int sq = ( int ) sqrt (n);
return (n == sq * sq);
}
int countPerfectDivisors( int n)
{
int count = 0;
for ( int i=1; i*i <= n; ++i)
{
if (n%i == 0)
{
if (isPerfectSquare(i))
++count;
if (n/i != i && isPerfectSquare(n/i))
++count;
}
}
return count;
}
int main()
{
int n = 16;
cout << "Total perfect divisors of "
<< n << " = " << countPerfectDivisors(n) << "\n" ;
n = 12;
cout << "Total perfect divisors of "
<< n << " = " << countPerfectDivisors(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean isPerfectSquare( int n)
{
int sq = ( int ) Math.sqrt(n);
return (n == sq * sq);
}
static int countPerfectDivisors( int n)
{
int count = 0 ;
for ( int i = 1 ; i * i <= n; ++i)
{
if (n % i == 0 )
{
if (isPerfectSquare(i))
++count;
if (n / i != i &&
isPerfectSquare(n / i))
++count;
}
}
return count;
}
public static void main (String[] args)
{
int n = 16 ;
System.out.print( "Total perfect " +
"divisors of " + n);
System.out.println( " = " +
countPerfectDivisors(n));
n = 12 ;
System.out.print( "Total perfect " +
"divisors of " + n);
System.out.println( " = " +
countPerfectDivisors(n));
}
}
|
Python3
import math
def isPerfectSquare(x) :
sq = ( int )(math.sqrt(x))
return (x = = sq * sq)
def countPerfectDivisors(n) :
cnt = 0
for i in range ( 1 , ( int )(math.sqrt(n)) + 1 ) :
if ( n % i = = 0 ) :
if isPerfectSquare(i):
cnt = cnt + 1
if n / i ! = i and isPerfectSquare(n / i):
cnt = cnt + 1
return cnt
print ( "Total perfect divisor of 16 = " ,
countPerfectDivisors( 16 ))
print ( "Total perfect divisor of 12 = " ,
countPerfectDivisors( 12 ))
|
C#
using System;
class GFG
{
static bool isPerfectSquare( int n)
{
int sq = ( int ) Math.Sqrt(n);
return (n == sq * sq);
}
static int countPerfectDivisors( int n)
{
int count = 0;
for ( int i = 1;
i * i <= n; ++i)
{
if (n % i == 0)
{
if (isPerfectSquare(i))
++count;
if (n / i != i &&
isPerfectSquare(n / i))
++count;
}
}
return count;
}
static public void Main ()
{
int n = 16;
Console.Write( "Total perfect " +
"divisors of " + n);
Console.WriteLine( " = " +
countPerfectDivisors(n));
n = 12;
Console.Write( "Total perfect " +
"divisors of " + n);
Console.WriteLine( " = " +
countPerfectDivisors(n));
}
}
|
PHP
<?php
function isPerfectSquare( $n )
{
$sq = sqrt( $n );
return ( $n == $sq * $sq );
}
function countPerfectDivisors( $n )
{
$count = 0;
for ( $i = 1; $i * $i <= $n ; ++ $i )
{
if ( $n % $i == 0)
{
if (isPerfectSquare( $i ))
++ $count ;
if ( $n / $i != $i &&
isPerfectSquare( $n / $i ))
++ $count ;
}
}
return $count ;
}
$n = 16;
echo "Total perfect divisors of " ,
$n , " = " , countPerfectDivisors( $n ), "\n" ;
$n = 12;
echo "Total perfect divisors of " ,
$n , " = " , countPerfectDivisors( $n );
?>
|
Javascript
<script>
function isPerfectSquare(n)
{
let sq = Math.sqrt(n);
return (n == sq * sq);
}
function countPerfectDivisors(n)
{
let count = 0;
for (let i = 1; i * i <= n; ++i)
{
if (n % i == 0)
{
if (isPerfectSquare(i))
++count;
if (n / i != i &&
isPerfectSquare(n / i))
++count;
}
}
return count;
}
let n = 16;
document.write( "Total perfect " +
"divisors of " + n);
document.write( " = " +
countPerfectDivisors(n) + "<br/>" );
n = 12;
document.write( "Total perfect " +
"divisors of " + n);
document.write( " = " +
countPerfectDivisors(n));
</script>
|
Output:
Total Perfect divisors of 16 = 3
Total Perfect divisors of 12 = 2
Time complexity: O(sqrt(n))
Auxiliary space: O(1)
Efficient approach (For large number of queries)
The idea is based on Sieve of Eratosthenes. This approach is beneficial if there are large number of queries. Following is the algorithm to find all perfect divisors up to n numbers.
- Create a list of n consecutive integers from 1 to n:(1, 2, 3, …, n)
- Initially, let d be 2, the first divisor
- Starting from 4(square of 2) increment all the multiples of 4 by 1 in perfectDiv[] array, as all these multiples contain 4 as perfect divisor. These numbers will be 4d, 8d, 12d, … etc
- Repeat the 3rd step for all other numbers. The final array of perfectDiv[] will contain all the count of perfect divisors from 1 to n
Below is implementation of above steps.
C++
#include<bits/stdc++.h>
using namespace std;
#define MAX 100001
int perfectDiv[MAX];
void precomputeCounts()
{
for ( int i=1; i*i < MAX; ++i)
{
for ( int j=i*i; j < MAX; j += i*i)
++perfectDiv[j];
}
}
int countPerfectDivisors( int n)
{
return perfectDiv[n];
}
int main()
{
precomputeCounts();
int n = 16;
cout << "Total perfect divisors of "
<< n << " = " << countPerfectDivisors(n) << "\n" ;
n = 12;
cout << "Total perfect divisors of "
<< n << " = " << countPerfectDivisors(n);
return 0;
}
|
Java
class GFG
{
static int MAX = 100001 ;
static int [] perfectDiv = new int [MAX];
static void precomputeCounts()
{
for ( int i = 1 ; i * i < MAX; ++i)
{
for ( int j = i * i; j < MAX; j += i * i)
++perfectDiv[j];
}
}
static int countPerfectDivisors( int n)
{
return perfectDiv[n];
}
public static void main (String[] args)
{
precomputeCounts();
int n = 16 ;
System.out.println( "Total perfect divisors of " +
n + " = " + countPerfectDivisors(n));
n = 12 ;
System.out.println( "Total perfect divisors of " +
n + " = " + countPerfectDivisors(n));
}
}
|
Python3
MAX = 100001
perfectDiv = [ 0 ] * MAX
def precomputeCounts():
i = 1
while i * i < MAX :
for j in range (i * i, MAX ,i * i):
perfectDiv[j] + = 1
i + = 1
def countPerfectDivisors( n):
return perfectDiv[n]
if __name__ = = "__main__" :
precomputeCounts()
n = 16
print ( "Total perfect divisors of "
, n , " = " ,countPerfectDivisors(n))
n = 12
print ( "Total perfect divisors of "
,n , " = " ,countPerfectDivisors(n))
|
C#
using System;
class GFG
{
static int MAX = 100001;
static int [] perfectDiv = new int [MAX];
static void precomputeCounts()
{
for ( int i = 1; i * i < MAX; ++i)
{
for ( int j = i * i; j < MAX; j += i * i)
++perfectDiv[j];
}
}
static int countPerfectDivisors( int n)
{
return perfectDiv[n];
}
public static void Main()
{
precomputeCounts();
int n = 16;
Console.WriteLine( "Total perfect divisors of " + n +
" = " + countPerfectDivisors(n));
n = 12;
Console.WriteLine( "Total perfect divisors of " + n +
" = " + countPerfectDivisors(n));
}
}
|
PHP
<?php
$MAX = 10001;
$perfectDiv = array_fill (0, $MAX , 0);
function precomputeCounts()
{
global $MAX , $perfectDiv ;
for ( $i = 1; $i * $i < $MAX ; ++ $i )
{
for ( $j = $i * $i ;
$j < $MAX ; $j += $i * $i )
++ $perfectDiv [ $j ];
}
}
function countPerfectDivisors( $n )
{
global $perfectDiv ;
return $perfectDiv [ $n ];
}
precomputeCounts();
$n = 16;
echo "Total perfect divisors of " . $n .
" = " . countPerfectDivisors( $n ) . "\n" ;
$n = 12;
echo "Total perfect divisors of " . $n .
" = " . countPerfectDivisors( $n );
?>
|
Javascript
<script>
let MAX = 100001;;
let perfectDiv = new Array(MAX);
for (let i = 0; i < MAX; i++)
{
perfectDiv[i] = 0;
}
function precomputeCounts()
{
for (let i = 1; i * i < MAX; ++i)
{
for (let j = i * i; j < MAX; j += i * i)
++perfectDiv[j];
}
}
function countPerfectDivisors(n)
{
return perfectDiv[n];
}
precomputeCounts();
let n = 16;
document.write( "Total perfect divisors of " +
n + " = " +
countPerfectDivisors(n) + "<br>" );
n = 12;
document.write( "Total perfect divisors of " +
n + " = " +
countPerfectDivisors(n));
</script>
|
Output:
Total Perfect divisors of 16 = 3
Total Perfect divisors of 12 = 2
Time complexity: O(MAX * log(log (MAX)))
Auxiliary space: O(MAX)
Share your thoughts in the comments
Please Login to comment...