Count digits in a factorial | Set 1 | This function receives an integer n , and returns the number of digits present in n ! ; factorial exists only for n >= 0 ; base case ; else iterate through n and calculate the value ; Driver code
< ? php function findDigits ( $ n ) { if ( $ n < 0 ) return 0 ; if ( $ n <= 1 ) return 1 ; $ digits = 0 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ digits += log10 ( $ i ) ; return floor ( $ digits ) + 1 ; } echo findDigits ( 1 ) , " STRNEWLINE " ; echo findDigits ( 5 ) , " STRNEWLINE " ; echo findDigits ( 10 ) , " STRNEWLINE " ; echo findDigits ( 120 ) , " STRNEWLINE " ; ? >