Maximum GCD of N integers with given product | C # implementation of above approach ; 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
using System ; using System . Collections . Generic ; class GFG { static int maxGCD ( int N , int P ) { int ans = 1 ; Dictionary < int , int > prime_factors = new Dictionary < int , int > ( ) ; for ( int i = 2 ; i * i <= P ; i ++ ) { while ( P % i == 0 ) { if ( ! prime_factors . ContainsKey ( i ) ) prime_factors . Add ( i , 1 ) ; else prime_factors [ i ] = prime_factors [ i ] + 1 ; P /= i ; } } if ( P != 1 ) if ( ! prime_factors . ContainsKey ( P ) ) prime_factors . Add ( P , 1 ) ; else prime_factors [ P ] = prime_factors [ P ] + 1 ; foreach ( KeyValuePair < int , int > me in prime_factors ) { ans *= ( int ) Math . Pow ( me . Key , me . Value / N ) ; } return ans ; } public static void Main ( String [ ] args ) { int N = 3 , P = 24 ; Console . WriteLine ( maxGCD ( N , P ) ) ; } }