Largest Divisor of a Number not divisible by a perfect square
Last Updated :
07 Mar, 2023
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 up to the 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 up to 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.
Steps to solve the problem:
- Initialize a variable m to n and a set s.
- Insert 1 and n into the set s.
- Iterate over all integers from 2 to the square root of n.
- If n is divisible by i, insert n/i and i into the set s, and divide m by i repeatedly until it is no longer divisible by i.
- if m is greater than 1, insert m into the set s.
- Initialize a vector vec to store all perfect squares up to a certain limit.
- Iterate over each element d in the set s using a range-based for loop.
- Initialize a variable divi to 0.
- Iterate over each perfect square in vec that is less than or equal to d .
- Check if d is divisible by the current perfect square without a remainder using an if statement.
- If d is divisible by the current perfect square, set divi to 1 and break out of the for loop.
- If d is not divisible by any perfect square, return d as the answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5;
int findLargestDivisor( int n)
{
int m = n;
set< int , greater< int > > s;
s.insert(1);
s.insert(n);
for ( int i = 2; i < sqrt (n) + 1; i++) {
if (n % i == 0) {
s.insert(n / i);
s.insert(i);
while (m % i == 0)
m /= i;
}
}
if (m > 1)
s.insert(m);
vector< int > vec;
for ( int i = 2; i <= MAX; i++)
vec.push_back(i * i);
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;
}
}
int main()
{
int n = 12;
cout << findLargestDivisor(n) << endl;
n = 97;
cout << findLargestDivisor(n) << endl;
return 0;
}
|
Java
import java.util.*;
class Main{
static int MAX = ( int )1e5;
public static int findLargestDivisor( int n)
{
int m = n;
Set<Integer> s =
new HashSet<Integer>();
s.add( 1 );
s.add(n);
for ( int i = 2 ;
i < ( int )Math.sqrt(n) + 1 ; i++)
{
if (n % i == 0 )
{
s.add(n / i);
s.add(i);
while (m % i == 0 )
m /= i;
}
}
if (m > 1 )
s.add(m);
List<Integer> l =
new ArrayList<Integer>(s);
Collections.sort(l);
Collections.reverse(l);
Vector<Integer> vec =
new Vector<Integer>();
for ( int i = 2 ; i <= MAX; i++)
vec.add(i * i);
for ( int d : l)
{
int divi = 0 ;
for ( int j = 0 ;
j < vec.size() &&
vec.get(j) <= d; j++)
{
if (d % vec.get(j) == 0 )
{
divi = 1 ;
break ;
}
}
if (divi == 0 )
return d;
}
return 0 ;
}
public static void main(String[] args)
{
int n = 12 ;
System.out.println(findLargestDivisor(n));
n = 97 ;
System.out.println(findLargestDivisor(n));
}
}
|
Python3
MAX = 10 * * 5
def findLargestDivisor(n):
m = n
s = set ()
s.add( 1 )
s.add(n)
for i in range ( 2 , int (n * * ( 0.5 )) + 1 ):
if n % i = = 0 :
s.add(n / / i)
s.add(i)
while m % i = = 0 :
m / / = i
if m > 1 :
s.add(m)
vec = [i * * 2 for i in range ( 2 , MAX + 1 )]
for d in sorted (s, reverse = True ):
divi, j = 0 , 0
while j < len (vec) and vec[j] < = d:
if d % vec[j] = = 0 :
divi = 1
break
j + = 1
if not divi:
return d
if __name__ = = "__main__" :
n = 12
print (findLargestDivisor(n))
n = 97
print (findLargestDivisor(n))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int MAX = ( int )1e5;
static int findLargestDivisor( int n)
{
int m = n;
HashSet< int > s = new HashSet< int >();
s.Add(1);
s.Add(n);
for ( int i = 2;
i < ( int )Math.Sqrt(n) + 1;
i++)
{
if (n % i == 0)
{
s.Add(n / i);
s.Add(i);
while (m % i == 0)
m /= i;
}
}
if (m > 1)
s.Add(m);
List< int > l = new List< int >(s);
l.Sort();
l.Reverse();
List< int > vec = new List< int >();
for ( int i = 2; i <= MAX; i++)
vec.Add(i * i);
foreach ( int d in l)
{
int divi = 0;
for ( int j = 0;
j < vec.Count && vec[j] <= d;
j++)
{
if (d % vec[j] == 0)
{
divi = 1;
break ;
}
}
if (divi == 0)
return d;
}
return 0;
}
static void Main()
{
int n = 12;
Console.WriteLine(findLargestDivisor(n));
n = 97;
Console.WriteLine(findLargestDivisor(n));
}
}
|
Javascript
<script>
let MAX = 100000;
function findLargestDivisor(n)
{
let m = n;
let s = new Set();
s.add(1);
s.add(n);
for (let i = 2;
i < Math.floor(Math.sqrt(n)) + 1; i++)
{
if (n % i == 0)
{
s.add(Math.floor(n / i));
s.add(i);
while (m % i == 0)
m = Math.floor(m/i);
}
}
if (m > 1)
s.add(m);
let l =Array.from(s);
l.sort( function (a,b){ return a-b;});
l.reverse();
let vec = [];
for (let i = 2; i <= MAX; i++)
vec.push(i * i);
for (let d=0;d<l.length;d++)
{
let divi = 0;
for (let j = 0;
j < vec.length &&
vec[j] <= l[d]; j++)
{
if (l[d] % vec[j] == 0)
{
divi = 1;
break ;
}
}
if (divi == 0)
return l[d];
}
return 0;
}
let n = 12;
document.write(findLargestDivisor(n)+ "<br>" );
n = 97;
document.write(findLargestDivisor(n)+ "<br>" );
</script>
|
Time Complexity : O(sqrt(n)*logn)
Auxiliary Space: O(n)
An efficient approach is to divide n by i for every i such that (i * i) divides n.
C++
#include <bits/stdc++.h>
using namespace std;
int findLargestDivisor( int n)
{
for ( int i = 2; i < sqrt (n) + 1; i++) {
while (n % (i * i) == 0) {
n = n / i;
}
}
return n;
}
int main()
{
int n = 12;
cout << findLargestDivisor(n) << endl;
n = 97;
cout << findLargestDivisor(n) << endl;
return 0;
}
|
Java
public class GFG
{
static int findLargestDivisor( int n)
{
for ( int i = 2 ; i < Math.sqrt(n) + 1 ; i++) {
while (n % (i * i) == 0 ) {
n = n / i;
}
}
return n;
}
public static void main(String args[])
{
int n = 12 ;
System.out.println(findLargestDivisor(n)) ;
n = 97 ;
System.out.println(findLargestDivisor(n)) ;
}
}
|
Python3
import math
def findLargestDivisor( n):
for i in range ( 2 , int (math.sqrt(n)) + 1 ) :
while (n % (i * i) = = 0 ) :
n = n / / i
return n
if __name__ = = "__main__" :
n = 12
print (findLargestDivisor(n))
n = 97
print (findLargestDivisor(n))
|
C#
using System;
public class GFG
{
static int findLargestDivisor( int n)
{
for ( int i = 2; i < Math.Sqrt(n) + 1; i++) {
while (n % (i * i) == 0) {
n = n / i;
}
}
return n;
}
public static void Main()
{
int n = 12;
Console.WriteLine(findLargestDivisor(n)) ;
n = 97;
Console.WriteLine(findLargestDivisor(n)) ;
}
}
|
PHP
<?php
function findLargestDivisor( $n )
{
for ( $i = 2; $i < sqrt( $n ) + 1; $i ++)
{
while ( $n % ( $i * $i ) == 0)
{
$n = $n / $i ;
}
}
return $n ;
}
$n = 12;
echo (findLargestDivisor( $n ));
echo ( "\n" );
$n = 97;
echo (findLargestDivisor( $n )) ;
?>
|
Javascript
<script>
function findLargestDivisor(n)
{
for (let i = 2; i < Math.sqrt(n) + 1; i++)
{
while (n % (i * i) == 0) {
n = n / i;
}
}
return n;
}
let n = 12;
document.write(findLargestDivisor(n) + "</br>" );
n = 97;
document.write(findLargestDivisor(n) + "</br>" );
</script>
|
Time Complexity : O(sqrt(n))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...