Largest Divisor of a Number not divisible by a perfect square | Javascript 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 ; set to store divisors in descending order ; If the number is divisible by i , then insert it ; Vector to store perfect squares ; Check for each divisor , if it is not divisible by any perfect square , simply return it as the answer . ; Driver code
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 ) + " " ) ; n = 97 ; document . write ( findLargestDivisor ( n ) + " " ) ;