Find the largest good number in the divisors of given number N | function to return distinct prime factors ; to store distinct prime factors ; run a loop upto sqrt ( n ) ; place this prime factor in vector ; This condition is to handle the case when n is a prime number greater than 1 ; function that returns good number ; distinct prime factors ; to store answer ; product of all distinct prime factors is required answer ; Driver code ; function call
function PrimeFactors ( n ) { let v = [ ] ; let x = n ; for ( let i = 2 ; i * i <= n ; i ++ ) { if ( x % i == 0 ) { v . push ( i ) ; while ( x % i == 0 ) x = Math . floor ( x / i ) ; } } if ( x > 1 ) v . push ( x ) ; return v ; } function GoodNumber ( n ) { let v = PrimeFactors ( n ) ; let ans = 1 ; for ( let i = 0 ; i < v . length ; i ++ ) ans *= v [ i ] ; return ans ; } let n = 12 ; document . write ( GoodNumber ( n ) ) ;