Legendre 's formula (Given p and n, find the largest x such that p^x divides n!) | Returns largest power of p that divides n ! ; Initialize result ; Calculate x = n / p + n / ( p ^ 2 ) + n / ( p ^ 3 ) + ... . ; Driver Code
function largestPower ( n , p ) { let x = 0 ; while ( n ) { n = parseInt ( n / p ) ; x += n ; } return Math . floor ( x ) ; } let n = 10 ; let p = 3 ; document . write ( " " + p ) ; document . write ( " " + n + " " ) ; document . write ( largestPower ( n , p ) ) ;