Count of numbers satisfying m + sum(m) + sum(sum(m)) = N

Given an integer N, find out the count of numbers(m) that satisfy the condition m + sum(m) + sum (sum(m)) = N, where sum(m) denotes the sum of digits in m. Given N <= 10e9.

Examples:

Input: 9
Output: 1
Explanation: Only 1 positive integer satisfies 
             the condition that is 3, 
            3 + sum(3) + sum(sum(3)) = 3 + 3 + 3 
                                     = 9 

Input: 9939
Output: 4
Explanation: m can be 9898, 9907, 9910 and 9913. 
             9898 + sum(9898) + sum(sum(9898))  
              = 9898 + 34 + 7 = 9939.
             9907 + sum(9907) + sum(sum(9907)) 
              = 9907 + 25 + 7 = 9939.
             9910 + sum(9910) + sum(sum(9910)) 
              = 9910 + 19 + 10 = 9939.
             9913 + sum(9913) + sum(sum(9913))  
              = 9913 + 22 + 4 = 9939.  



Approach: The first thing to note is that we are given the constraint N<=10e9. This means sum(x) can at maximum be 81 for any number, This is because the largest number below 10e9 is 999999999 whose digits add up to 81. The maximum case for sum(sum(x)) will be 16 (number being 79<=81), so at max it 81 + 16 which is 97 needs to be checked. We just need to iterate from N – 97 to N and check which integers satisfy the equation because no integer smaller than N-97 can satisfy the equation and neither can any integer greater than N can.

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to count numbers
// satisfying equation.
#include <bits/stdc++.h>
using namespace std;
  
// function that returns sum of 
// digits in a number
int sum(int n)
{
    int rem = 0;
  
    // initially sum of digits is 0
    int sum_of_digits = 0;
  
    // loop runs till all digits 
    // have been extracted
    while (n > 0) {
      
        // last digit from backside
        rem = n % 10;
  
        // sums up the digits
        sum_of_digits += rem;
  
        // the number is reduced to the
        // number removing the last digit
        n = n / 10;
    }
      
    // returns the sum of digits in a number
    return sum_of_digits;
}
  
// function to calculate the count of
// such occurences
int count(int n)
{
    // counter to calculate the occurences
    int c = 0;
  
    // loop to traverse from n-97 to n
    for (int i = n - 97; i <= n; i++) {
  
        // calls the function to calculate 
        // the sum of digits of i
        int a = sum(i);
  
        // calls the function to calculate
        // the sum of digits of a
        int b = sum(a);
  
        // if the summation is equal to n
        // then increase counter by 1
        if ((i + a + b) == n) {
            c += 1;
        }
    }
      
    // returns the count
    return c;
}
  
// driver program to test the above function
int main()
{
    int n = 9939;
      
    // calls the function to get the answer
    cout << count(n) << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to count numbers
// satisfying equation.
import java.io.*;
  
class GFG {
      
    // function that returns sum of 
    // digits in a number
    static int sum(int n)
    {
        int rem = 0;
      
        // initially sum of digits is 0
        int sum_of_digits = 0;
      
        // loop runs till all digits 
        // have been extracted
        while (n > 0)
        {
          
            // last digit from backside
            rem = n % 10;
      
            // sums up the digits
            sum_of_digits += rem;
      
            // the number is reduced to the
            // number removing the last digit
            n = n / 10;
        }
          
        // returns the sum of digits in a number
        return sum_of_digits;
    }
      
    // function to calculate the count of
    // such occurences
    static int count(int n)
    {
        // counter to calculate the occurences
        int c = 0;
      
        // loop to traverse from n-97 to n
        for (int i = n - 97; i <= n; i++)
        {
      
            // calls the function to calculate 
            // the sum of digits of i
            int a = sum(i);
      
            // calls the function to calculate
            // the sum of digits of a
            int b = sum(a);
      
            // if the summation is equal to n
            // then increase counter by 1
            if ((i + a + b) == n) 
            {
                c += 1;
            }
        }
          
        // returns the count
        return c;
    }
  
    // driver program to test the above function
    public static void main (String[] args) 
    {
        int n = 9939;
        // calls the function to get the answer
        System.out.println ( count(n) );
      
    }
}
  
// This article is contributed by vt_m

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program
# to count numbers
# satisfying equation.
  
# function that returns sum of 
# digits in a number
def sum(n):
  
    rem = 0
   
    #initially sum of digits is 0
    sum_of_digits = 0
   
    # loop runs till all digits 
    # have been extracted
    while (n > 0):
       
        # last digit from backside
        rem = n % 10
   
        # sums up the digits
        sum_of_digits += rem
   
        # the number is reduced to the
        # number removing the last digit
        n = n // 10
  
    # returns the sum
    # of digits in a number
    return sum_of_digits
   
# function to calculate
# the count of
# such occurences
def count(n):
  
    # counter to calculate the occurences
    c = 0
   
    # loop to traverse from n - 97 to n
    for i in range(n - 97,n+1):
   
        # calls the function to calculate 
        # the sum of digits of i
        a = sum(i)
   
        # calls the function to calculate
        # the sum of digits of a
        b = sum(a)
   
        # if the summation is equal to n
        # then increase counter by 1
        if ((i + a + b) == n):
            c += 1
  
    # returns the count
    return c
   
# driver program to test
# the above function
  
n = 9939
       
# calls the function
# to get the answer
print(count(n))
  
# This code is contributed
# by Anant Agarwal.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count numbers
// satisfying equation.
using System;
  
class GFG {
      
    // function that returns sum  
    // of digits in a number
    static int sum(int n)
    {
        int rem = 0;
      
        // initially sum of 
        // digits is 0
        int sum_of_digits = 0;
      
        // loop runs till all digits 
        // have been extracted
        while (n > 0)
        {
            // last digit from
            // backside
            rem = n % 10;
      
            // sums up the digits
            sum_of_digits += rem;
      
            // the number is reduced 
            // to the number removing
            // the last digit
            n = n / 10;
        }
          
        // returns the sum of 
        // digits in a number
        return sum_of_digits;
    }
      
    // function to calculate the 
    // count of such occurences
    static int count(int n)
    {
          
        // counter to calculate 
        // the occurences
        int c = 0;
      
        // loop to traverse from n-97 to n
        for (int i = n - 97; i <= n; i++)
        {
      
            // calls the function to calculate 
            // the sum of digits of i
            int a = sum(i);
      
            // calls the function to calculate
            // the sum of digits of a
            int b = sum(a);
      
            // if the summation is equal to n
            // then increase counter by 1
            if ((i + a + b) == n) 
            {
                c += 1;
            }
        }
          
        // returns the count
        return c;
    }
  
    // Driver Code
    public static void Main () 
    {
        int n = 9939;
          
        // calling the function
        Console.Write ( count(n) );
      
    }
}
  
// This code is contributed by Nitin Mittal.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right



Output:

4

This article is contributed by Raja Vikramaditya. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : nitin mittal