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++
// 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
Similar Reads
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 a
6 min read
Total number of divisors for a given number
Given a positive integer n, we have to find the total number of divisors for n. Examples:Input : n = 25Output : 3Divisors are 1, 5 and 25.Input : n = 24Output : 8Divisors are 1, 2, 3, 4, 6, 812 and 24. We have discussed different approaches for printing all divisors (here and here). Here the task is
7 min read
Find minimum sum of factors of number
Given a number N, the task is to find minimum sum of its factors. Examples: Input: 12Output: 7Explanation: Following are different ways to factorize 12 andsum of factors in different ways.12 = 12 * 1 = 12 + 1 = 1312 = 2 * 6 = 2 + 6 = 812 = 3 * 4 = 3 + 4 = 712 = 2 * 2 * 3 = 2 + 2 + 3 = 7Therefore min
4 min read
Find the sum of the number of divisors
Given three integers A, B, C, the task is to find ?Ai=1 ?Bj=1?Ck=1 d(i.j.k), where d(x) is the number of divisors of x. Answer can be very large, So, print answer modulo 109+7. Examples: Input: A = 2, B = 2, c = 2 Output: 20 Explanation: d(1.1.1) = d(1) = 1; d(1·1·2) = d(2) = 2; d(1·2·1) = d(2) = 2;
6 min read
Nth Square free number
Given a number n, find the n-th square-free number. A number is square-free if it is not divisible by a perfect square other than 1.Examples : Input : n = 2 Output : 2 Input : 5 Output : 6 There is one number (in range from 1 to 6) that is divisible by a square. The number is 4. Method 1 (Brute Forc
15+ min read
First triangular number whose number of divisors exceeds N
Given a number N, find the first triangular number whose number of divisors exceeds N. Triangular numbers are sums of natural numbers, i. e., of the form x*(x+1)/2. First few triangular numbers are 1, 3, 6, 10, 15, 21, 28, ...Examples: Input: N = 2 Output: 6 6 is the first triangular number with mor
13 min read
Square Free Number
Given a number, check if it is square-free or not. 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. First few square-free numbers are 1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, 30,
6 min read
Minimum digits to remove to make a number Perfect Square
Given an integer n, we need to find how many digits remove from the number to make it a perfect square. Examples : Input : 8314 Output: 81 2 Explanation: If we remove 3 and 4 number becomes 81 which is a perfect square. Input : 57 Output : -1 The idea is to generate all possible subsequences and ret
9 min read
Sum of all perfect square divisors of numbers from 1 to N
Given a number N, the task is to find the sum of all the perfect square divisors of numbers from 1 to N. Examples: Input: N = 5 Output: 9 Explanation: N = 5 Perfect square divisors of 1 = 1. Similarly, perfect square divisors of 2, 3 = 1. Perfect square divisors of 4 = 1, 4. Perfect square divisors
12 min read
Number of squares of maximum area in a rectangle
Given a rectangle of sides m and n. Cut the rectangle into smaller identical pieces such that each piece is a square having maximum possible side length with no leftover part of the rectangle. Print number of such squares formed.Examples: Input: 9 6 Output: 6 Rectangle can be cut into squares of siz
6 min read