Total no of 1 's in numbers | function to count the frequency of 1. ; Driver Code
< ? php function countDigitOne ( $ n ) { $ countr = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i *= 10 ) { $ divider = $ i * 10 ; $ countr += ( int ) ( $ n / $ divider ) * $ i + min ( max ( $ n % $ divider - $ i + 1 , 0 ) , $ i ) ; } return $ countr ; } $ n = 13 ; echo countDigitOne ( $ n ) , " STRNEWLINE " ; $ n = 113 ; echo countDigitOne ( $ n ) , " STRNEWLINE " ; $ n = 205 ; echo countDigitOne ( $ n ) , " STRNEWLINE " ; ? >