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++
#include <bits/stdc++.h>
using namespace std;
int sum( int n)
{
int rem = 0;
int sum_of_digits = 0;
while (n > 0) {
rem = n % 10;
sum_of_digits += rem;
n = n / 10;
}
return sum_of_digits;
}
int count( int n)
{
int c = 0;
for ( int i = n - 97; i <= n; i++) {
int a = sum(i);
int b = sum(a);
if ((i + a + b) == n) {
c += 1;
}
}
return c;
}
int main()
{
int n = 9939;
cout << count(n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int sum( int n)
{
int rem = 0 ;
int sum_of_digits = 0 ;
while (n > 0 )
{
rem = n % 10 ;
sum_of_digits += rem;
n = n / 10 ;
}
return sum_of_digits;
}
static int count( int n)
{
int c = 0 ;
for ( int i = n - 97 ; i <= n; i++)
{
int a = sum(i);
int b = sum(a);
if ((i + a + b) == n)
{
c += 1 ;
}
}
return c;
}
public static void main (String[] args)
{
int n = 9939 ;
System.out.println ( count(n) );
}
}
|
Python3
def 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
def count(n):
c = 0
for i in range (n - 97 ,n + 1 ):
a = sum (i)
b = sum (a)
if ((i + a + b) = = n):
c + = 1
return c
n = 9939
print (count(n))
|
C#
using System;
class GFG {
static int sum( int n)
{
int rem = 0;
int sum_of_digits = 0;
while (n > 0)
{
rem = n % 10;
sum_of_digits += rem;
n = n / 10;
}
return sum_of_digits;
}
static int count( int n)
{
int c = 0;
for ( int i = n - 97; i <= n; i++)
{
int a = sum(i);
int b = sum(a);
if ((i + a + b) == n)
{
c += 1;
}
}
return c;
}
public static void Main ()
{
int n = 9939;
Console.Write ( count(n) );
}
}
|
PHP
<?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 ) ;
?>
|
Javascript
<script>
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));
</script>
|
Output:
4
Time Complexity: O(log10N+97), as we are using two loops to traverse log10N and 97 times.
Auxiliary Space: O(1), as we are not using any extra space.
Last Updated :
18 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...