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 program to test the above function ; calls the function to get the answer
function sum ( n ) { var rem = 0 ; var sum_of_digits = 0 ; while ( n > 0 ) { rem = n % 10 ; sum_of_digits += rem ; n = parseInt ( n / 10 ) ; } return sum_of_digits ; } function count ( n ) { var c = 0 ; for ( i = n - 97 ; i <= n ; i ++ ) { var a = sum ( i ) ; var b = sum ( a ) ; if ( ( i + a + b ) == n ) { c += 1 ; } } return c ; } var n = 9939 ; document . write ( count ( n ) ) ;