# Count of square free divisors of a given number

Given an integer N, the task is to count the number of square-free divisors of the given number.

A number is said to be square-free, if no prime factor divides it more than once, i.e., the largest power of a prime factor that divides N is one.

Examples:

Input: N = 72
Output: 3
Explanation: 2, 3, 6 are the three possible square free numbers that divide 72.

Input: N = 62290800
Output: 31

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

Naive Approach:
For every integer N, find its factors and check if it is a square-free number or not. If it is a square-free number then increase the count or proceed to the next number otherwise. Finally, print the count which gives us the required number of square-free divisors of N.
Time complexity: O(N3/2)

Efficient Approach:
Follow the steps below to solve the problem:

• From the definition of square-free numbers, it can be understood that by finding out all the prime factors of the given number N, all the possible square-free numbers that can divide N can be found out.
• Let the number of prime factors of N be X. Therefore, 2X – 1 square-free numbers can be formed using these X prime factors.
• Since each of these X prime factors is a factor of N, therefore any product combination of these X prime factors is also a factor of N and thus there will be 2X – 1 square free divisors of N.

Illustration:

• N = 72
• Prime factors of N are 2, 3.
• Hence, the three possible square free numbers generated from these two primes are 2, 3 and 6.
• Hence, the total square-free divisors of 72 are 3( = 22 – 1).

Below is the implementation of the above approach:

## C++

 `// C++ Program to find the square ` `// free divisors of a given number ` `#include ` `using` `namespace` `std; ` ` `  `// The function to check ` `// if a number is prime or not ` `bool` `IsPrime(``int` `i) ` `{ ` `    ``// If the number is even ` `    ``// then its not prime ` `    ``if` `(i % 2 == 0 && i != 2) ` `        ``return` `false``; ` ` `  `    ``else` `{ ` `        ``for` `(``int` `j = 3; ` `             ``j <= ``sqrt``(i); j += 2) { ` `            ``if` `(i % j == 0) ` `                ``return` `false``; ` `        ``} ` `        ``return` `true``; ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// Stores the count of ` `    ``// distinct prime factors ` `    ``int` `c = 0; ` `    ``int` `N = 72; ` ` `  `    ``for` `(``int` `i = 2; ` `         ``i <= ``sqrt``(N); i++) { ` ` `  `        ``if` `(IsPrime(i)) { ` `            ``if` `(N % i == 0) { ` `                ``c++; ` `                ``if` `(IsPrime(N / i) ` `                    ``&& i != (N / i)) { ` `                    ``c++; ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Print the number of ` `    ``// square-free divisors ` `    ``cout << ``pow``(2, c) - 1 ` `         ``<< endl; ` `    ``return` `0; ` `} `

## Java

 `// Java program to find the square  ` `// free divisors of a given number ` `import` `java.util.*; ` ` `  `class` `GFG{ ` `     `  `// The function to check  ` `// if a number is prime or not  ` `static` `boolean` `IsPrime(``int` `i) ` `{ ` `     `  `    ``// If the number is even  ` `    ``// then its not prime  ` `    ``if` `(i % ``2` `== ``0` `&& i != ``2``)  ` `        ``return` `false``;  ` `    ``else` `    ``{ ` `        ``for``(``int` `j = ``3``;  ` `                ``j <= Math.sqrt(i);  ` `                ``j += ``2``)  ` `        ``{ ` `           ``if` `(i % j == ``0``) ` `               ``return` `false``; ` `        ``} ` `        ``return` `true``;  ` `    ``} ` `} ` ` `  `// Driver code  ` `public` `static` `void` `main(String[] args)  ` `{ ` `     `  `    ``// Stores the count of  ` `    ``// distinct prime factors  ` `    ``int` `c = ``0``;  ` `    ``int` `N = ``72``; ` `     `  `    ``for``(``int` `i = ``2``;  ` `            ``i <= Math.sqrt(N); i++) ` `    ``{ ` `       ``if` `(IsPrime(i)) ` `       ``{ ` `           ``if` `(N % i == ``0``) ` `           ``{ ` `               ``c++;  ` `               ``if` `(IsPrime(N / i) &&  ` `                     ``i != (N / i)) ` `                   ``c++; ` `           ``} ` `       ``} ` `    ``} ` `     `  `    ``// Print the number of  ` `    ``// square-free divisors  ` `    ``System.out.print(Math.pow(``2``, c) - ``1``); ` `} ` `} ` ` `  `// This code is contributed by sanjoy_62 `

## Python3

 `# Python3 program to find the square  ` `# free divisors of a given number  ` `import` `math ` ` `  `# The function to check  ` `# if a number is prime or not ` `def` `IsPrime(i): ` `     `  `    ``# If the number is even  ` `    ``# then its not prime  ` `    ``if` `(i ``%` `2` `=``=` `0` `and` `i !``=` `2``): ` `        ``return` `0``;  ` `         `  `    ``else``: ` `        ``for` `j ``in` `range``(``3``, ``int``(math.sqrt(i) ``+` `1``), ``2``): ` `            ``if` `(i ``%` `j ``=``=` `0``): ` `                ``return` `0``; ` `                 `  `        ``return` `1``;  ` ` `  `# Driver code  ` ` `  `# Stores the count of  ` `# distinct prime factors ` `c ``=` `0``;  ` `N ``=` `72``; ` ` `  `for` `i ``in` `range``(``2``, ``int``(math.sqrt(N)) ``+` `1``): ` `    ``if` `(IsPrime(i)): ` `        ``if` `(N ``%` `i ``=``=` `0``): ` `            ``c ``=` `c ``+` `1` ` `  `            ``if` `(IsPrime(N ``/` `i) ``and`  `                 ``i !``=` `(N ``/` `i)): ` `                ``c ``=` `c ``+` `1` `                 `  `# Print the number of  ` `# square-free divisors      ` `print` `(``pow``(``2``, c) ``-` `1``) ` ` `  `# This code is contributed by sanjoy_62 `

## C#

 `// C# program to find the square  ` `// free divisors of a given number ` `using` `System; ` `class` `GFG{ ` `     `  `// The function to check  ` `// if a number is prime or not  ` `static` `Boolean IsPrime(``int` `i) ` `{ ` `     `  `    ``// If the number is even  ` `    ``// then its not prime  ` `    ``if` `(i % 2 == 0 && i != 2)  ` `        ``return` `false``;  ` `    ``else` `    ``{ ` `        ``for``(``int` `j = 3;  ` `                ``j <= Math.Sqrt(i);  ` `                ``j += 2)  ` `        ``{ ` `        ``if` `(i % j == 0) ` `            ``return` `false``; ` `        ``} ` `        ``return` `true``;  ` `    ``} ` `} ` ` `  `// Driver code  ` `public` `static` `void` `Main(String[] args)  ` `{ ` `     `  `    ``// Stores the count of  ` `    ``// distinct prime factors  ` `    ``int` `c = 0;  ` `    ``int` `N = 72; ` `     `  `    ``for``(``int` `i = 2;  ` `            ``i <= Math.Sqrt(N); i++) ` `    ``{ ` `        ``if` `(IsPrime(i)) ` `        ``{ ` `            ``if` `(N % i == 0) ` `            ``{ ` `                ``c++;  ` `                ``if` `(IsPrime(N / i) &&  ` `                        ``i != (N / i)) ` `                    ``c++; ` `            ``} ` `        ``} ` `    ``} ` `     `  `    ``// Print the number of  ` `    ``// square-free divisors  ` `    ``Console.Write(Math.Pow(2, c) - 1); ` `} ` `} ` ` `  `// This code is contributed by shivanisinghss2110 `

Output:

```3
```

Time Complexity: O(N)
Auxiliary Space: O(1) My Personal Notes arrow_drop_up

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.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.