Find maximum value of x such that n ! % ( k ^ x ) = 0 | Function to maximize the value of x such that n ! % ( k ^ x ) = 0 ; Find square root of k and add 1 to it ; Run the loop from 2 to m and k should be greater than 1 ; optimize the value of k ; Minimum store ; Driver Code
< ? php function findX ( $ n , $ k ) { $ r = $ n ; $ m = ( int ) sqrt ( $ k ) + 1 ; for ( $ i = 2 ; $ i <= $ m && $ k > 1 ; $ i ++ ) { if ( $ i == $ m ) { $ i = $ k ; } for ( $ u = $ v = 0 ; $ k % $ i == 0 ; $ v ++ ) { $ k = ( int ) ( $ k / $ i ) ; } if ( $ v > 0 ) { $ t = $ n ; while ( $ t > 0 ) { $ t = ( int ) ( $ t / $ i ) ; $ u = $ u + $ t ; } $ r = min ( $ r , ( int ) ( $ u / $ v ) ) ; } } return $ r ; } $ n = 5 ; $ k = 2 ; echo findX ( $ n , $ k ) ; ? >