Open In App

Count of square free divisors of a given number

Last Updated : 23 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:
Explanation: 2, 3, 6 are the three possible square free numbers that divide 72.

Input: N = 62290800 
Output: 31 
 

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 <bits/stdc++.h>
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


Javascript




<script>
 
// Javascript program to find the square
// free divisors of a given number
 
// The function to check
// if a number is prime or not
function IsPrime(i)
{
     
    // If the number is even
    // then its not prime
    if (i % 2 == 0 && i != 2)
        return false;
    else
    {
        for(j = 3; j <= Math.sqrt(i); j += 2)
        {
            if (i % j == 0)
                return false;
        }
        return true;
    }
}
 
// Driver code
 
// Stores the count of
// distinct prime factors
var c = 0;
var N = 72;
 
for(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
document.write(Math.pow(2, c) - 1);
 
// This code is contributed by aashish1995
 
</script>


Output: 

3

 

Time Complexity: O(N) 
Auxiliary Space: O(1)
 



Similar Reads

Check if a number has an odd count of odd divisors and even count of even divisors
Given an integer N, the task is to check if N has an odd number of odd divisors and even number of even divisors. Examples: Input: N = 36Output: YesExplanation:Divisors of 36 = 1, 2, 3, 4, 6, 9, 12, 18, 36Count of Odd Divisors(1, 3, 9) = 3 [Odd]Count of Even Divisors(2, 4, 6, 12, 18, 36) = 6 [Even] Input: N = 28Output: No Naive Approach: The idea i
9 min read
Divisors of n-square that are not divisors of n
Given an positive large integer n. Count the number of positive divisors of n2 which are not divisible by any divisor of n(1 &lt;= n &lt;= 1012). Input: 6 Output: 5 Explanation Total divisors of 62 are 9 i.e., 1, 2, 3, 4, 6, 9, 12, 18, 36 Total divisors of '6' are 4, 1, 2, 3, 6 Total divisor of '36' which are not divisible by divisors of '6' are '5
7 min read
Check if count of even divisors of N is equal to count of odd divisors
Given a positive integer N, the task is to check whether the count of even divisors and odd divisors of N are equal or not. If they are same then print "YES" else print "NO".Examples : Input: N = 6 Output: YES Explanation: Number 6 has four factors: 1, 2, 3, 6, count of even divisors = 2 (2 and 6) count of odd divisors = 2 (1 and 3)Input: N = 9 Out
9 min read
Minimum value exceeding X whose count of divisors has different parity with count of divisors of X
Given an integer X, the task is to determine the minimum value of Y greater than X, such that count of divisors of X and Y have different parities. Examples: Input: X = 5Output: 9Explanation: The count of divisors of 5 and 9 are 2 and 3 respectively, which are of different parities. Input: X = 9Output: 10Explanation: The counts of divisors of 9 and
8 min read
Minimum number of Square Free Divisors
Given an integer N. Find the minimum number of square free divisors. In other words, the factorization of N should comprise of only those divisors that are square-free. A square free number is a number that is not divisible by any perfect square (Of course, 1 will not be considered a perfect square in this case). Constraints: 2 ? N ? 106 Examples:
12 min read
Find sum of inverse of the divisors when sum of divisors and the number is given
Given an integer N and the sum of its divisors. The task is to find the sum of the inverse of the divisors of N.Examples: Input: N = 6, Sum = 12 Output: 2.00 Divisors of N are {1, 2, 3, 6} Sum of inverse of divisors is equal to (1/1 + 1/2 + 1/3 + 1/6) = 2.0Input: N = 9, Sum = 13 Output: 1.44 Naive Approach: Calculate all the divisors of the given i
3 min read
Find sum of divisors of all the divisors of a natural number
Given a natural number n, the task is to find sum of divisors of all the divisors of n. Examples: Input : n = 54 Output : 232 Divisors of 54 = 1, 2, 3, 6, 9, 18, 27, 54. Sum of divisors of 1, 2, 3, 6, 9, 18, 27, 54 are 1, 3, 4, 12, 13, 39, 40, 120 respectively. Sum of divisors of all the divisors of 54 = 1 + 3 + 4 + 12 + 13 + 39 + 40 + 120 = 232. I
7 min read
Maximum possible prime divisors that can exist in numbers having exactly N divisors
Given an integer N which denotes the number of divisors of any number, the task is to find the maximum prime divisors that are possible in number having N divisors. Examples: Input: N = 4 Output: 2 Input: N = 8 Output: 3 Naive Approach: In this approach, the idea is to generate all the numbers having exactly N divisors and check for the maximum num
11 min read
Count integers whose square lie in given range and digits are perfect square
Given two integers L and R. Then the task is to output the count of integers X, such that L ≤ X2 ≤ R and X2 only consist of digits, which are perfect squares. Examples: Input: L = 167, R = 456Output: 2Explanation: Two numbers are 20 and 21, Their squares are 400 and 441 respectively. It can be verified that squares of both 20 and 21 are in the rang
13 min read
Count array elements whose count of divisors is a prime number
Given an array arr[] consisting of N positive integers, the task is to find the number of array elements whose count of divisors is a prime number. Examples: Input: arr[] = {3, 6, 4}Output: 2Explanation:The count of divisors for each element are: arr[0]( = 3): 3 has 2 divisors i.e., 1 and 3.arr[1]( = 6): 6 has 4 divisors i.e., 1, 2, 3, and 6.arr[2]
9 min read