Find the largest good number in the divisors of given number N | C # program to 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
using System ; using System . Collections . Generic ; public class GFG { static List < int > PrimeFactors ( int n ) { List < int > v = new List < int > ( ) ; int x = n ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( x % i == 0 ) { v . Add ( i ) ; while ( x % i == 0 ) x /= i ; } } if ( x > 1 ) v . Add ( x ) ; return v ; } static int GoodNumber ( int n ) { List < int > v = new List < int > ( PrimeFactors ( n ) ) ; int ans = 1 ; for ( int i = 0 ; i < v . Count ; i ++ ) ans *= v [ i ] ; return ans ; } public static void Main ( String [ ] args ) { int n = 12 ; Console . WriteLine ( GoodNumber ( n ) ) ; } }