Count digits in a factorial | Set 2 | Returns the number of digits present in n ! Since the result can be large long long is used as return type ; factorial of - ve number doesn 't exists ; base case ; Use Kamenetsky formula to calculate the number of digits ; Driver Code
< ? php function findDigits ( $ n ) { if ( $ n < 0 ) return 0 ; if ( $ n <= 1 ) return 1 ; $ x = ( ( $ n * log10 ( $ n / M_E ) + log10 ( 2 * M_PI * $ n ) / 2.0 ) ) ; return floor ( $ x ) + 1 ; } echo findDigits ( 1 ) . " STRNEWLINE " ; echo findDigits ( 50000000 ) . " STRNEWLINE " ; echo findDigits ( 1000000000 ) . " STRNEWLINE " ; echo findDigits ( 120 ) ; ? >