# Count all perfect divisors of a number

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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive approach

A brute force is find all the divisors of a number. Count all divisors that are perfect squares.

## C++

 `// Below is C++ code to count total perfect Divisors ` `#include ` `using` `namespace` `std; ` ` `  `// Utility function to check perfect square number ` `bool` `isPerfectSquare(``int` `n) ` `{ ` `    ``int` `sq = (``int``) ``sqrt``(n); ` `    ``return` `(n == sq * sq); ` `} ` ` `  `// Returns count all perfect divisors of n ` `int` `countPerfectDivisors(``int` `n) ` `{ ` `    ``// Initialize result ` `    ``int` `count = 0; ` ` `  `    ``// Consider every number that can be a divisor ` `    ``// of n ` `    ``for` `(``int` `i=1; i*i <= n; ++i) ` `    ``{ ` `        ``// If i is a divisor ` `        ``if` `(n%i == 0) ` `        ``{ ` `            ``if` `(isPerfectSquare(i)) ` `                ``++count; ` `            ``if` `(n/i != i && isPerfectSquare(n/i)) ` `                ``++count; ` `        ``} ` `    ``} ` `    ``return` `count; ` `} ` ` `  `// Driver code ` `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

 `// Java code to count ` `// total perfect Divisors ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `     `  `// Utility function to check  ` `// perfect square number ` `static` `boolean` `isPerfectSquare(``int` `n) ` `{ ` `    ``int` `sq = (``int``) Math.sqrt(n); ` `    ``return` `(n == sq * sq); ` `} ` ` `  `// Returns count all  ` `// perfect divisors of n ` `static` `int` `countPerfectDivisors(``int` `n) ` `{ ` `    ``// Initialize result ` `    ``int` `count = ``0``; ` ` `  `    ``// Consider every number  ` `    ``// that can be a divisor of n ` `    ``for` `(``int` `i = ``1``; i * i <= n; ++i) ` `    ``{ ` `        ``// If i is a divisor ` `        ``if` `(n % i == ``0``) ` `        ``{ ` `            ``if` `(isPerfectSquare(i)) ` `                ``++count; ` `            ``if` `(n / i != i &&  ` `                ``isPerfectSquare(n / i)) ` `                ``++count; ` `        ``} ` `    ``} ` `    ``return` `count; ` `} ` ` `  `// Driver code ` `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)); ` `} ` `} ` ` `  `// This code is contributed by ajit `

## Python3

 `# Python3 implementation of Naive method  ` `# to count all perfect divisors ` ` `  `import` `math ` ` `  `def` `isPerfectSquare(x) : ` `    ``sq ``=` `(``int``)(math.sqrt(x)) ` `    ``return` `(x ``=``=` `sq ``*` `sq) ` ` `  `# function to count all prefect divisors ` `def` `countPerfectDivisors(n) : ` `     `  `    ``# Initialize result ` `    ``cnt ``=` `0` ` `  `        ``# Consider every number that  ` `        ``# can be a divisor of n ` `    ``for` `i ``in` `range``(``1``, (``int``)(math.sqrt(n)) ``+` `1``) : ` ` `  `            ``# If i is a divisor ` `            ``if` `( n ``%` `i ``=``=` `0` `) : ` ` `  `                ``if` `isPerfectSquare(i): ` `                        ``cnt ``=` `cnt ``+` `1` `                ``if` `n``/``i !``=` `i ``and` `isPerfectSquare(n``/``i): ` `                        ``cnt ``=` `cnt ``+` `1` `    ``return` `cnt ` `     `  `         `  `# Driver program to test above function  ` `print``(``"Total perfect divisor of 16 = "``, ` `    ``countPerfectDivisors(``16``)) ` `     `  `print``(``"Total perfect divisor of 12 = "``, ` `    ``countPerfectDivisors(``12``))     ` ` `  `# This code is contributed by Saloni Gupta `

## C#

 `// C# code to count ` `// total perfect Divisors ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Utility function to check  ` `// perfect square number ` `static` `bool` `isPerfectSquare(``int` `n) ` `{ ` `    ``int` `sq = (``int``) Math.Sqrt(n); ` `    ``return` `(n == sq * sq); ` `} ` ` `  `// Returns count all  ` `// perfect divisors of n ` `static` `int` `countPerfectDivisors(``int` `n) ` `{ ` `    ``// Initialize result ` `    ``int` `count = 0; ` ` `  `    ``// Consider every number  ` `    ``// that can be a divisor of n ` `    ``for` `(``int` `i = 1;  ` `             ``i * i <= n; ++i) ` `    ``{ ` `        ``// If i is a divisor ` `        ``if` `(n % i == 0) ` `        ``{ ` `            ``if` `(isPerfectSquare(i)) ` `                ``++count; ` `            ``if` `(n / i != i &&  ` `                ``isPerfectSquare(n / i)) ` `                ``++count; ` `        ``} ` `    ``} ` `    ``return` `count; ` `} ` ` `  `// Driver code ` `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)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by akt_mit `

## PHP

 ` `

```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.

1. Create a list of n consecutive integers from 1 to n:(1, 2, 3, …, n)
2. Initially, let d be 2, the first divisor
3. 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
4. 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++

 `// Below is C++ code to count total perfect ` `// divisors ` `#include ` `using` `namespace` `std; ` `#define MAX 100001 ` ` `  `int` `perfectDiv[MAX]; ` ` `  `// Pre-compute counts of all perfect divisors ` `// of all numbers upto MAX. ` `void` `precomputeCounts() ` `{ ` `    ``for` `(``int` `i=1; i*i < MAX; ++i) ` `    ``{ ` `        ``// Iterate through all the multiples of i*i ` `        ``for` `(``int` `j=i*i; j < MAX; j += i*i) ` ` `  `            ``// Increment all such multiples by 1 ` `            ``++perfectDiv[j]; ` `    ``} ` `} ` ` `  `// Returns count of perfect divisors of n. ` `int` `countPerfectDivisors(``int` `n) ` `{ ` `    ``return` `perfectDiv[n]; ` `} ` ` `  `// Driver code ` `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

 `// Java code to count total perfect ` `// divisors ` `class` `GFG ` `{ ` `     `  `static` `int` `MAX = ``100001``; ` ` `  `static` `int``[] perfectDiv = ``new` `int``[MAX]; ` ` `  `// Pre-compute counts of all perfect divisors ` `// of all numbers upto MAX. ` `static` `void` `precomputeCounts() ` `{ ` `    ``for` `(``int` `i = ``1``; i * i < MAX; ++i) ` `    ``{ ` `        ``// Iterate through all the multiples of i*i ` `        ``for` `(``int` `j = i * i; j < MAX; j += i * i) ` ` `  `            ``// Increment all such multiples by 1 ` `            ``++perfectDiv[j]; ` `    ``} ` `} ` ` `  `// Returns count of perfect divisors of n. ` `static` `int` `countPerfectDivisors(``int` `n) ` `{ ` `    ``return` `perfectDiv[n]; ` `} ` ` `  `// Driver code ` `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)); ` `} ` `} ` ` `  `// This code is contributed by mits `

## Python3

 `# Below is Python3 code to count total perfect ` `# divisors ` `MAX` `=` `100001` `  `  `perfectDiv``=` `[``0``]``*``MAX` `  `  `# Pre-compute counts of all perfect divisors ` `# of all numbers upto MAX. ` `def` `precomputeCounts(): ` ` `  `    ``i``=``1` `    ``while` `i``*``i < ``MAX``: ` `     `  `        ``# Iterate through all the multiples of i*i ` `        ``for` `j ``in` `range``(i``*``i,``MAX``,i``*``i): ` `  `  `            ``# Increment all such multiples by 1 ` `            ``perfectDiv[j] ``+``=` `1` `        ``i ``+``=` `1` `  `  `# Returns count of perfect divisors of n. ` `def` `countPerfectDivisors( n): ` ` `  `    ``return` `perfectDiv[n] ` `  `  `# Driver code ` `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#

 `// C# code to count total perfect ` `// divisors ` `using` `System; ` `class` `GFG ` `{ ` `     `  `static` `int` `MAX = 100001; ` ` `  `static` `int``[] perfectDiv = ``new` `int``[MAX]; ` ` `  `// Pre-compute counts of all perfect  ` `// divisors of all numbers upto MAX. ` `static` `void` `precomputeCounts() ` `{ ` `    ``for` `(``int` `i = 1; i * i < MAX; ++i) ` `    ``{ ` `        ``// Iterate through all the multiples of i*i ` `        ``for` `(``int` `j = i * i; j < MAX; j += i * i) ` ` `  `            ``// Increment all such multiples by 1 ` `            ``++perfectDiv[j]; ` `    ``} ` `} ` ` `  `// Returns count of perfect divisors of n. ` `static` `int` `countPerfectDivisors(``int` `n) ` `{ ` `    ``return` `perfectDiv[n]; ` `} ` ` `  `// Driver code ` `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)); ` `} ` `} ` ` `  `// This code is contributed by mits `

## PHP

 ` `

Output:

```Total Perfect divisors of 16 = 3
Total Perfect divisors of 12 = 2
```

Time complexity: O(MAX * log(log (MAX)))
Auxiliary space: O(MAX)

This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.