Largest Divisor of a Number not divisible by a perfect square

Given a positive integer N. Find the largest divisor of the given number that is not divisible by a perfect square greater than 1.

Examples:

Input : 12
Output : 6
Explanation : Divisors of 12 are 1, 2, 3, 4, 6 and 12. 
Since 12 is divisible by 4 (a perfect square), 
it can't be required divisor. 6 is not divisible 
by any perfect square.
 
Input :97
Output :97

A simple approach is to find all the divisors of the given number N by iterating upto square root of N and keep them in sorted order(Descending) in a list. Here we are inserting them in a set in descending order to keep them sorted. Also, make a list of all perfect squares upto 1010 by iterating from 1 to 105.

Now for each divisor starting from the greatest one check whether it is divisible by any perfect square in the list or not. If a divisor is not divisible by any perfect, simply return it as the answer.

Below is the implementation of the above approach.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to find the largest
// divisor not divisible by any
// perfect square greater than 1
#include <bits/stdc++.h>
using namespace std;
  
const int MAX = 1e5;
  
// Function to find the largest
// divisor not divisible by any
// perfect square greater than 1
int findLargestDivisor(int n)
{
    // set to store divisors in
    // descending order
    int m = n;
    set<int, greater<int> > s;
    s.insert(1);
    s.insert(n);
  
    for (int i = 2; i < sqrt(n) + 1; i++) {
        // If the number is divisible
        // by i, then insert it
        if (n % i == 0) {
            s.insert(n / i);
            s.insert(i);
            while (m % i == 0)
                m /= i;
        }
    }
  
    if (m > 1)
        s.insert(m);
  
    // Vector to store perfect squares
    vector<int> vec;
    for (int i = 2; i <= MAX; i++)
        vec.push_back(i * i);
  
    // Check for each divisor, if it is not
    // divisible by any perfect square,
    // simply return it as the answer.
    for (auto d : s) {
        int divi = 0;
        for (int j = 0; j < vec.size()
                        && vec[j] <= d;
             j++) {
            if (d % vec[j] == 0) {
                divi = 1;
                break;
            }
        }
        if (!divi)
            return d;
    }
}
  
// Driver Code
int main()
{
    int n = 12;
    cout << findLargestDivisor(n) << endl;
  
    n = 97;
    cout << findLargestDivisor(n) << endl;
    return 0;
}

chevron_right


Output:

6
97

An efficient approach is to divide n by i for every i such that (i * i) divides n.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Efficient CPP Program to find the 
// largest divisor not divisible by any
// perfect square greater than 1
#include <bits/stdc++.h>
using namespace std;
   
// Function to find the largest
// divisor not divisible by any
// perfect square greater than 1
int findLargestDivisor(int n)
{
    for (int i = 2; i < sqrt(n) + 1; i++) {
          
        // If the number is divisible
        // by i*i, then remove one i
        while (n % (i * i) == 0) {
            n = n / i;
        }
    }
    
    // Now all squares are removed from n
    return n;    
}
   
// Driver Code
int main()
{
    int n = 12;
    cout << findLargestDivisor(n) << endl;
   
    n = 97;
    cout << findLargestDivisor(n) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Efficient Java Program to find the 
// largest divisor not divisible by any 
// perfect square greater than 1 
  
public class GFG
{
  
    // Function to find the largest 
    // divisor not divisible by any 
    // perfect square greater than 1 
    static int findLargestDivisor(int n) 
    
        for (int i = 2; i < Math.sqrt(n) + 1; i++) { 
              
            // If the number is divisible 
            // by i*i, then remove one i 
            while (n % (i * i) == 0) { 
                n = n / i; 
            
        
          
        // Now all squares are removed from n 
        return n;     
    
      
    // Driver Code 
    public static void main(String args[]) 
    
        int n = 12
        System.out.println(findLargestDivisor(n)) ;
      
        n = 97
        System.out.println(findLargestDivisor(n)) ;
      
    
    // This code is contributed
    // by Ryuga
}

chevron_right


Python3

# Efficient CPP Program to find the
# largest divisor not divisible by any
# perfect square greater than 1
import math

# Function to find the largest
# divisor not divisible by any
# perfect square greater than 1
def findLargestDivisor( n):

for i in range (2, int(math.sqrt(n)) + 1) :

# If the number is divisible
# by i*i, then remove one i
while (n % (i * i) == 0) :
n = n // i

# Now all squares are removed from n
return n

# Driver Code
if __name__ == “__main__”:

n = 12
print (findLargestDivisor(n))

n = 97
print (findLargestDivisor(n))

# This code is contributed by ita_c

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Efficient C# Program to find the 
// largest divisor not divisible by any 
// perfect square greater than 1 
using System;
public class GFG
{
  
    // Function to find the largest 
    // divisor not divisible by any 
    // perfect square greater than 1 
    static int findLargestDivisor(int n) 
    
        for (int i = 2; i < Math.Sqrt(n) + 1; i++) { 
              
            // If the number is divisible 
            // by i*i, then remove one i 
            while (n % (i * i) == 0) { 
                n = n / i; 
            
        
          
        // Now all squares are removed from n 
        return n;     
    
      
    // Driver Code 
    public static void Main() 
    
        int n = 12; 
        Console.WriteLine(findLargestDivisor(n)) ;
      
        n = 97; 
        Console.WriteLine(findLargestDivisor(n)) ;
      
    
}
    // This code is contributed
    // by Mukul Singh

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Efficient PHP Program to find the 
// largest divisor not divisible by 
// any perfect square greater than 1
  
// Function to find the largest
// divisor not divisible by any
// perfect square greater than 1
function findLargestDivisor($n)
{
    for ($i = 2; $i < sqrt($n) + 1; $i++)
    {
          
        // If the number is divisible
        // by i*i, then remove one i
        while ($n % ($i * $i) == 0)
        {
            $n = $n / $i;
        }
    }
  
    // Now all squares are removed from n
    return $n
}
  
// Driver Code
$n = 12;
echo(findLargestDivisor($n));
echo("\n");
  
$n = 97;
echo(findLargestDivisor($n)) ;
  
// This code is contributed 
// by Shivi_Aggarwal
?>

chevron_right


Output:

6
97


My Personal Notes arrow_drop_up