Minimum number of Square Free Divisors
Last Updated :
04 Aug, 2022
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 : 6
Output : 1
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++
#include <bits/stdc++.h>
using namespace std;
#define MAX 1005
void SieveOfEratosthenes(vector< int >& primes)
{
bool prime[MAX];
memset (prime, true , sizeof (prime));
for ( int p = 2; p * p < MAX; p++) {
if (prime[p] == true ) {
for ( int i = p * 2; i < MAX; i += p)
prime[i] = false ;
}
}
for ( int p = 2; p < MAX; p++)
if (prime[p])
primes.push_back(p);
}
int minimumSquareFreeDivisors( int N)
{
vector< int > primes;
SieveOfEratosthenes(primes);
int max_count = 0;
for ( int i = 0; i < primes.size() && primes[i] * primes[i] <= N; i++) {
if (N % primes[i] == 0) {
int tmp = 0;
while (N % primes[i] == 0) {
tmp++;
N /= primes[i];
}
max_count = max(max_count, tmp);
}
}
if (max_count == 0)
max_count = 1;
return max_count;
}
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
import java.util.Vector;
public class GFG {
static final int MAX = 1005 ;
static void SieveOfEratosthenes(Vector<Integer> primes) {
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] == true ) {
for ( int i = p * 2 ; i < MAX; i += p) {
prime[i] = false ;
}
}
}
for ( int p = 2 ; p < MAX; p++) {
if (prime[p]) {
primes.add(primes.size(), p);
}
}
}
static int minimumSquareFreeDivisors( int N) {
Vector<Integer> primes = new Vector<>();
SieveOfEratosthenes(primes);
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 ) {
int tmp = 0 ;
while (N % primes.get(i) == 0 ) {
tmp++;
N /= primes.get(i);
}
max_count = Math.max(max_count, tmp);
}
}
if (max_count == 0 ) {
max_count = 1 ;
}
return max_count;
}
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
from math import sqrt
MAX = 1005
def SieveOfEratosthenes(primes):
prime = [ True for i in range ( MAX )]
for p in range ( 2 , int (sqrt( MAX )) + 1 , 1 ):
if (prime[p] = = True ):
for i in range (p * 2 , MAX , p):
prime[i] = False
for p in range ( 2 , MAX , 1 ):
if (prime[p]):
primes.append(p)
return primes
def minimumSquareFreeDivisors(N):
prime = []
primes = []
primes = SieveOfEratosthenes(prime)
max_count = 0
i = 0
while ( len (primes) and primes[i] *
primes[i] < = N):
if (N % primes[i] = = 0 ):
tmp = 0
while (N % primes[i] = = 0 ):
tmp + = 1
N / = primes[i]
max_count = max (max_count, tmp)
i + = 1
if (max_count = = 0 ):
max_count = 1
return max_count
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))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static int MAX = 1005;
static void SieveOfEratosthenes(List< int > primes) {
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] == true ) {
for ( int i = p * 2; i < MAX; i += p) {
prime[i] = false ;
}
}
}
for ( int p = 2; p < MAX; p++) {
if (prime[p]) {
primes.Add(p);
}
}
}
static int minimumSquareFreeDivisors( int N) {
List< int > primes = new List< int >();
SieveOfEratosthenes(primes);
int max_count = 0;
for ( int i = 0; i < primes.Count && primes[i] *
primes[i] <= N; i++) {
if (N % primes[i] == 0) {
int tmp = 0;
while (N % primes[i] == 0) {
tmp++;
N /= primes[i];
}
max_count = Math.Max(max_count, tmp);
}
}
if (max_count == 0) {
max_count = 1;
}
return max_count;
}
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));
}
}
|
Javascript
<script>
let MAX = 1005;
function SieveOfEratosthenes(primes) {
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] == true ) {
for (let i = p * 2; i < MAX; i += p) {
prime[i] = false ;
}
}
}
for (let p = 2; p < MAX; p++) {
if (prime[p]) {
primes.push(p);
}
}
}
function minimumSquareFreeDivisors(N) {
let primes = [];
SieveOfEratosthenes(primes);
let max_count = 0;
for (let i = 0; i < primes.length && primes[i] *
primes[i] <= N; i++) {
if (N % primes[i] == 0) {
let tmp = 0;
while (N % primes[i] == 0) {
tmp++;
N = parseInt(N / primes[i], 10);
}
max_count = Math.max(max_count, tmp);
}
}
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));
</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
Share your thoughts in the comments
Please Login to comment...