Count of numbers satisfying m + sum ( m ) + sum ( sum ( m ) ) = N | function that returns sum of digits in a number ; initially sum of digits is 0 ; loop runs till all digits have been extracted ; last digit from backside ; sums up the digits ; the number is reduced to the number removing the last digit ; returns the sum of digits in a number ; function to calculate the count of such occurrences ; counter to calculate the occurrences ; loop to traverse from n - 97 to n ; calls the function to calculate the sum of digits of i ; calls the function to calculate the sum of digits of a ; if the summation is equal to n then increase counter by 1 ; returns the count ; Driver Code ; calls the function to get the answer
< ? php function sum ( $ n ) { $ rem = 0 ; $ sum_of_digits = 0 ; while ( $ n > 0 ) { $ rem = $ n % 10 ; $ sum_of_digits += $ rem ; $ n = $ n / 10 ; } return $ sum_of_digits ; } function countt ( $ n ) { $ c = 0 ; for ( $ i = $ n - 97 ; $ i <= $ n ; $ i ++ ) { $ a = sum ( $ i ) ; $ b = sum ( $ a ) ; if ( ( $ i + $ a + $ b ) == $ n ) { $ c += 1 ; } } return $ c ; } $ n = 9939 ; echo countt ( $ n ) ; ? >