Numbers in a Range with given Digital Root | Function to return the count of required numbers ; Count of numbers present in given range ; Number of groups of 9 elements starting from L ; Left over elements not covered in factor 9 ; One Number in each group of 9 ; To check if any number in rem satisfy the property ; Driver code
< ? php function countNumbers ( $ L , $ R , $ K ) { if ( $ K == 9 ) $ K = 0 ; $ totalnumbers = $ R - $ L + 1 ; $ factor9 = intval ( $ totalnumbers / 9 ) ; $ rem = $ totalnumbers % 9 ; $ ans = $ factor9 ; for ( $ i = $ R ; $ i > $ R - $ rem ; $ i -- ) { $ rem1 = $ i % 9 ; if ( $ rem1 == $ K ) $ ans ++ ; } return $ ans ; } $ L = 10 ; $ R = 22 ; $ K = 3 ; echo countNumbers ( $ L , $ R , $ K ) ; ? >