Open In App

Minimum number of Square Free Divisors

Improve
Improve
Like Article
Like
Save
Share
Report

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: 
 

Input : 24 
Output : 3 
Explanation: 24 will be represented as 24 = 6 x 2 x 2, here every factor 
(6 and 2) both are square free. 

Note: 24 cannot be represented as (24 = 12 x 2) or (24 = 24) or (24 = 8 x 3) because in all these factorizations, every factorization has atleast one number that is not square free i.e. 12, 24 and 8 are divisible by 4 (4 is a perfect square). Hence minimum number of square free divisors is 3.

Input :
Output :
Explanation: 6 will be represented as 6 = 6, since 6 is not divisible by any perfect square. Hence minimum number of square free divisors is 1.

 

Prerequisites: Sieve of Eratosthenes

Naive Approach: 

Here consider all possible factorizations of the number N and then check if there exists a number that is not square free (divisible by some perfect square) for each factorization. If it exists, then discard that factorization, otherwise, consider that factorization in answer. After considering all correct factorizations, find the minimum number of square free divisors.

Efficient Approach: 

Build Prime Sieve (using Sieve of Eratosthenes) by finding all the prime factors upto square root of N (upto 103 considering the constraints). Now, consider all prime factors less than or equal to square root of N and for each prime factor find its maximum power in number N (like max power of 2 in 24 is 3). Now, we know that if a prime factor has a power greater than 1 in N, it can’t be grouped with itself (for e.g. 2 has power of 3 in 24, hence 2 x 2 = 4 or 2 x 2 x 2 = 8 can’t occur in the factorization of 24 as both of them are not square free) since it will be divisible by some perfect square. But a prime factor grouped with another prime factor (only once) will never be divisible by any perfect square. This gives us an intuition that answer will be the maximum of maximum powers of all prime factors in number N. In other words, 
 

If prime factorization of N = f1p1 * f2p2 * ... * fnpn
where f1, f2 ... fn are the prime factors and p1, p2 ... pn are their 
maximum respective powers in N. Then answer will be,
ans = max(p1, p2, ..., pn)
For e.g. 24 = 23 * 31
ans = max(3, 1) = 3

Below is an illustration to explain the above algorithm 

Let N = 24. Factorization of N can be represented in many ways and out of those we have to find that in which there are minimum divisors with each divisor being square free. Some factorizations are: 
24 = 24 (24 is not square Free) 
24 = 2 * 12 (12 is not square free) 
24 = 3 * 8 (8 is not square free) 
24 = 4 * 6 (4 is not square free) 
24 = 6 * 2 * 2 (Every divisor is square free with divisor count = 3) 
24 = 3 * 2 * 2 * 2 (Every divisor is square free with divisor count = 4) 
Hence, appropriate answer would be 3 (24 = 6 * 2 * 2). Now if we observe carefully, we can’t group a prime factor with itself but with another prime factor. Like in this case, 2 is grouped with 3 to make 6 (square free) to reduce divisors count. Thus, answer would be maximum of maximum powers of all prime factors because we need atleast that much count to retain the condition of square free and other prime factors (whose power is not maximum) can be grouped with the prime factor with maximum power to minimize the count of divisors. 
 

Below is the implementation of above approach. Here, we will do preprocessing of finding prime factors (using Sieve) so that we can answer many queries simultaneously.
 

C++




// CPP Program to find the minimum
// number of square free divisors
#include <bits/stdc++.h>
using namespace std;
 
// Initializing MAX with SQRT(10 ^ 6)
#define MAX 1005
 
void SieveOfEratosthenes(vector<int>& primes)
{
    // Create a boolean array "prime[0..n]" and initialize
    // all entries it as true. A value in prime[i] will
    // finally be false if i is Not a prime, else true.
    bool prime[MAX];
    memset(prime, true, sizeof(prime));
 
    for (int p = 2; p * p < MAX; p++) {
 
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == true) {
 
            // Update all multiples of p
            for (int i = p * 2; i < MAX; i += p)
                prime[i] = false;
        }
    }
 
    // Print all prime numbers
    for (int p = 2; p < MAX; p++)
        if (prime[p])
            primes.push_back(p);
}
 
// This function returns the minimum number of
// Square Free divisors
int minimumSquareFreeDivisors(int N)
{
    vector<int> primes;
 
    // Precomputing Prime Factors
    SieveOfEratosthenes(primes);
 
    // holds max of max power of all prime factors
    int max_count = 0;
    for (int i = 0; i < primes.size() && primes[i] * primes[i] <= N; i++) {
        if (N % primes[i] == 0) {
 
            // holds the max power of current prime factor
            int tmp = 0;
            while (N % primes[i] == 0) {
                tmp++;
                N /= primes[i];
            }
            max_count = max(max_count, tmp);
        }
    }
 
    // If number itself is prime, it will be included
    // as answer and thus minimum required answer is 1
    if (max_count == 0)
        max_count = 1;
 
    return max_count;
}
 
// Driver Code to test above functions
int main()
{
    int N = 24;
    cout << "Minimum Number of Square Free Divisors is "
         << minimumSquareFreeDivisors(N) << endl;
 
    N = 6;
    cout << "Minimum Number of Square Free Divisors is "
         << minimumSquareFreeDivisors(N) << endl;
    return 0;
}


Java




// Java Program to find the minimum
// number of square free divisors
 
import java.util.Vector;
 
public class GFG {
 
// Initializing MAX with SQRT(10 ^ 6)
    static final int MAX = 1005;
 
    static void SieveOfEratosthenes(Vector<Integer> primes) {
        // Create a boolean array "prime[0..n]" and initialize
        // all entries it as true. A value in prime[i] will
        // finally be false if i is Not a prime, else true.
        boolean prime[] = new boolean[MAX];
        for (int i = 0; i < prime.length; i++) {
            prime[i] = true;
        }
        for (int p = 2; p * p < MAX; p++) {
 
            // If prime[p] is not changed, then it is a prime
            if (prime[p] == true) {
 
                // Update all multiples of p
                for (int i = p * 2; i < MAX; i += p) {
                    prime[i] = false;
                }
            }
        }
 
        // Print all prime numbers
        for (int p = 2; p < MAX; p++) {
            if (prime[p]) {
                primes.add(primes.size(), p);
            }
        }
    }
 
// This function returns the minimum number of
// Square Free divisors
    static int minimumSquareFreeDivisors(int N) {
        Vector<Integer> primes = new Vector<>();
 
        // Precomputing Prime Factors
        SieveOfEratosthenes(primes);
 
        // holds max of max power of all prime factors
        int max_count = 0;
        for (int i = 0; i < primes.size() && primes.get(i) *
                         primes.get(i) <= N; i++) {
            if (N % primes.get(i) == 0) {
 
                // holds the max power of current prime factor
                int tmp = 0;
                while (N % primes.get(i) == 0) {
                    tmp++;
                    N /= primes.get(i);
                }
                max_count = Math.max(max_count, tmp);
            }
        }
 
        // If number itself is prime, it will be included
        // as answer and thus minimum required answer is 1
        if (max_count == 0) {
            max_count = 1;
        }
 
        return max_count;
    }
 
// Driver Code to test above functions
    public static void main(String[] args) {
        int N = 24;
        System.out.println("Minimum Number of Square Free Divisors is "
                + minimumSquareFreeDivisors(N));
 
        N = 6;
        System.out.println("Minimum Number of Square Free Divisors is "
                + minimumSquareFreeDivisors(N));
    }
}


Python3




# Python 3 Program to find the minimum
# number of square free divisors
from math import sqrt
 
# Initializing MAX with SQRT(10 ^ 6)
MAX = 1005
 
def SieveOfEratosthenes(primes):
     
    # Create a boolean array "prime[0..n]" and
    # initialize all entries it as true. A value
    # in prime[i] will finally be false if i is
    # Not a prime, else true.
    prime = [True for i in range(MAX)]
 
    for p in range(2,int(sqrt(MAX)) + 1, 1):
         
        # If prime[p] is not changed, then
        # it is a prime
        if (prime[p] == True):
             
            # Update all multiples of p
            for i in range(p * 2, MAX, p):
                prime[i] = False
 
    # Print all prime numbers
    for p in range(2, MAX, 1):
        if (prime[p]):
            primes.append(p)
 
    return primes
 
# This function returns the minimum number
# of Square Free divisors
def minimumSquareFreeDivisors(N):
    prime = []
    primes = []
     
    # Precomputing Prime Factors
    primes = SieveOfEratosthenes(prime)
 
    # holds max of max power of all
    # prime factors
    max_count = 0
    i = 0
    while(len(primes) and primes[i] *
                          primes[i] <= N):
        if (N % primes[i] == 0):
 
            # holds the max power of current
            # prime factor
            tmp = 0
            while (N % primes[i] == 0):
                tmp += 1
                N /= primes[i]
 
            max_count = max(max_count, tmp)
 
        i += 1
 
    # If number itself is prime, it will be included
    # as answer and thus minimum required answer is 1
    if (max_count == 0):
        max_count = 1
 
    return max_count
 
# Driver Code
if __name__ == '__main__':
    N = 24
    print("Minimum Number of Square Free Divisors is",
                         minimumSquareFreeDivisors(N))
 
    N = 6
    print("Minimum Number of Square Free Divisors is",
                         minimumSquareFreeDivisors(N))
     
# This code is contributed by
# Surendra_Gangwar


C#




// C# Program to find the minimum
// number of square free divisors
 
using System;
using System.Collections.Generic;
 
public class GFG {
 
// Initializing MAX with SQRT(10 ^ 6)
    static int MAX = 1005;
 
    static void SieveOfEratosthenes(List<int> primes) {
        // Create a boolean array "prime[0..n]" and initialize
        // all entries it as true. A value in prime[i] will
        // finally be false if i is Not a prime, else true.
        bool []prime = new bool[MAX];
        for (int i = 0; i < prime.Length; i++) {
            prime[i] = true;
        }
        for (int p = 2; p * p < MAX; p++) {
 
            // If prime[p] is not changed, then it is a prime
            if (prime[p] == true) {
 
                // Update all multiples of p
                for (int i = p * 2; i < MAX; i += p) {
                    prime[i] = false;
                }
            }
        }
 
        // Print all prime numbers
        for (int p = 2; p < MAX; p++) {
            if (prime[p]) {
                primes.Add(p);
            }
        }
    }
 
// This function returns the minimum number of
// Square Free divisors
    static int minimumSquareFreeDivisors(int N) {
        List<int> primes = new List<int>();
 
        // Precomputing Prime Factors
        SieveOfEratosthenes(primes);
 
        // holds max of max power of all prime factors
        int max_count = 0;
        for (int i = 0; i < primes.Count && primes[i] *
                        primes[i] <= N; i++) {
            if (N % primes[i] == 0) {
 
                // holds the max power of current prime factor
                int tmp = 0;
                while (N % primes[i] == 0) {
                    tmp++;
                    N /= primes[i];
                }
                max_count = Math.Max(max_count, tmp);
            }
        }
 
        // If number itself is prime, it will be included
        // as answer and thus minimum required answer is 1
        if (max_count == 0) {
            max_count = 1;
        }
 
        return max_count;
    }
 
// Driver Code to test above functions
    public static void Main(){
        int N = 24;
        Console.WriteLine("Minimum Number of Square Free Divisors is "
                + minimumSquareFreeDivisors(N));
 
        N = 6;
        Console.WriteLine("Minimum Number of Square Free Divisors is "
                + minimumSquareFreeDivisors(N));
    }
    // This code is contributed by Ryuga
}


Javascript




<script>
    // Javascript Program to find the minimum
    // number of square free divisors
     
    // Initializing MAX with SQRT(10 ^ 6)
    let MAX = 1005;
   
    function SieveOfEratosthenes(primes) {
        // Create a boolean array "prime[0..n]" and initialize
        // all entries it as true. A value in prime[i] will
        // finally be false if i is Not a prime, else true.
        let prime = new Array(MAX);
        for (let i = 0; i < prime.length; i++) {
            prime[i] = true;
        }
        for (let p = 2; p * p < MAX; p++) {
   
            // If prime[p] is not changed, then it is a prime
            if (prime[p] == true) {
   
                // Update all multiples of p
                for (let i = p * 2; i < MAX; i += p) {
                    prime[i] = false;
                }
            }
        }
   
        // Print all prime numbers
        for (let p = 2; p < MAX; p++) {
            if (prime[p]) {
                primes.push(p);
            }
        }
    }
   
    // This function returns the minimum number of
    // Square Free divisors
    function minimumSquareFreeDivisors(N) {
        let primes = [];
   
        // Precomputing Prime Factors
        SieveOfEratosthenes(primes);
   
        // holds max of max power of all prime factors
        let max_count = 0;
        for (let i = 0; i < primes.length && primes[i] *
                        primes[i] <= N; i++) {
            if (N % primes[i] == 0) {
   
                // holds the max power of current prime factor
                let tmp = 0;
                while (N % primes[i] == 0) {
                    tmp++;
                    N = parseInt(N / primes[i], 10);
                }
                max_count = Math.max(max_count, tmp);
            }
        }
   
        // If number itself is prime, it will be included
        // as answer and thus minimum required answer is 1
        if (max_count == 0) {
            max_count = 1;
        }
   
        return max_count;
    }
     
    let N = 24;
    document.write("Minimum Number of Square Free Divisors is "
                      + minimumSquareFreeDivisors(N) + "</br>");
 
    N = 6;
    document.write("Minimum Number of Square Free Divisors is "
                      + minimumSquareFreeDivisors(N));
     
    // This code is contributed by suresh07.
</script>


Output: 
 

Minimum Number of Square Free Divisors is 3
Minimum Number of Square Free Divisors is 1

Time Complexity: O(n)

Auxiliary Space: O(n) // an extra array is used for creating prime numbers 



Last Updated : 04 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads