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
function findX ( n , k ) { let r = n ; let v = 0 ; let u = 0 ; let m = Math . floor ( Math . sqrt ( k ) + 1 ) ; for ( let i = 2 ; i <= m && k > 1 ; i ++ ) { if ( i == m ) { i = k ; } for ( let u = v = 0 ; k % i == 0 ; v ++ ) { k = Math . floor ( k / i ) ; } if ( v > 0 ) { let t = n ; while ( t > 0 ) { t = Math . floor ( t / i ) ; u += t ; } r = Math . min ( r , Math . floor ( u / v ) ) ; } } return r ; } let n = 5 ; let k = 2 ; document . write ( findX ( n , k ) ) ;