text: 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
code: < ? php function largestPower ( $ n , $ p ) { $ x = 0 ; while ( $ n ) { $ n = ( int ) $ n / $ p ; $ x += $ n ; } return floor ( $ x ) ; } $ n = 10 ; $ p = 3 ; echo " The ▁ largest ▁ power ▁ of ▁ " , $ p ; echo " ▁ that ▁ divides ▁ " , $ n , " ! ▁ is ▁ " ; echo largestPower ( $ n , $ p ) ; ? >