Largest factor of a given number which is a perfect square | Function to find the largest factor of a given number which is a perfect square ; Initialise the answer to 1 ; Finding the prime factors till sqrt ( num ) ; Frequency of the prime factor in the factorisation initialised to 0 ; If the frequency is odd then multiply i frequency - 1 times to the answer ; Else if it is even , multiply it frequency times ; Driver Code
function largestSquareFactor ( num ) { let answer = 1 ; for ( let i = 2 ; i < Math . sqrt ( num ) ; ++ i ) { let cnt = 0 ; let j = i ; while ( num % j == 0 ) { cnt ++ ; j *= i ; } if ( cnt & 1 ) { cnt -- ; answer *= Math . pow ( i , cnt ) ; } else { answer *= Math . pow ( i , cnt ) ; } } return answer ; } let N = 420 ; document . write ( largestSquareFactor ( N ) ) ;