Maximum GCD of N integers with given product | Function to find maximum GCD of N integers with product P ; map to store prime factors of P ; prime factorization of P ; traverse all prime factors and multiply its 1 / N power to the result ; Driver code
function maxGCD ( N , P ) { let ans = 1 ; let prime_factors = new Map ( ) ; for ( let i = 2 ; i * i <= P ; i ++ ) { while ( P % i == 0 ) { if ( prime_factors . get ( i ) == null ) prime_factors . set ( i , 1 ) ; else prime_factors . set ( i , ( prime_factors . get ( i ) + 1 ) ) ; P = Math . floor ( P / i ) ; } } if ( P != 1 ) prime_factors [ P ] ++ ; console . log ( prime_factors ) for ( let v of prime_factors ) { console . log ( v ) ans *= Math . pow ( v [ 0 ] , Math . floor ( v [ 1 ] / N ) ) ; } return ans ; } let N = 3 , P = 24 ; document . write ( maxGCD ( N , P ) ) ;