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
< ? php function PrimeFactors ( $ n ) { $ v = array ( ) ; $ x = $ n ; for ( $ i = 2 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ x % $ i == 0 ) { array_push ( $ v , $ i ) ; while ( $ x % $ i == 0 ) $ x /= $ i ; } } if ( $ x > 1 ) array_push ( $ v , $ x ) ; return $ v ; } function GoodNumber ( $ n ) { $ v = PrimeFactors ( $ n ) ; $ ans = 1 ; for ( $ i = 0 ; $ i < count ( $ v ) ; $ i ++ ) $ ans *= $ v [ $ i ] ; return $ ans ; } $ n = 12 ; echo GoodNumber ( $ n ) ; ? >