integer sum problem

I am trying to solve the problem. Is my logic correct?
https://www.chegg.com/homework-help/questions-and-answers/write-function-solution-given-integer-n-returns-smallest-non-negative-integer-whose-indivi-q65857134
https://www.chegg.com/homework-help/questions-and-answers/write-stdout-debugging-purposes-eg-print-debug-message-def-solution-n-write-code-python-36-q101295036

Below is my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

#include <iostream>


class Program { 
    
public:
 static int solution(int N)
{
        if (N == 0)
	{
	    return 0;
	}
	if (N <= 9)
	{
	    return N;
	}
	int sum1 = 0;
	while (N != 0)
	{
	   sum1 += N % 10;
	   N /= 10;
	}

	int sum2 = 0;
	for (int i = 10; i <= 1500; i++)
	{
		sum2 += i;
		if (sum2 == sum1)
		{
			sum1 = sum2;
		}
	}

	return sum1;

} 
};

int main()
{
    std::cout << Program::solution(16) << " --> 79" << std::endl;
	std::cout << Program::solution(19) << " --> 199" << std::endl;
	std::cout << Program::solution(7) << " --> 7" << std::endl;

}



OUTPUT:
7 --> 79
10 --> 199
7 --> 7
 

I am trying to solve the problem. Is my logic correct?
No, you shouldn't sum the input value.

Program::solution(16) is supposed to return 79. So you should sum the digits in the loop on line 26 and return the value who's digit sum (for 16 -> 79 = 7 + 9) equals the input value (16)


std::cout << "16 --> " << Program::solution(16) << std::endl;
Is my logic correct?

Obviously not - you are producing the wrong answers!

Try a few more and see if you can spot a pattern:
N       solution
================
0       0
1       1
2       2
3       3
4       4
5       5
6       6
7       7
8       8
9       9
10      19
11      29
12      39
13      49
14      59
15      69
16      79
17      89
18      99
19      199
20      299
21      399
22      499
23      599
24      699
25      799
26      899
27      999
28      1999
29      2999
30      3999
31      4999
32      5999
33      6999
34      7999
35      8999
36      9999
37      19999
38      29999
39      39999
40      49999
41      59999
42      69999
43      79999
44      89999
45      99999
46      199999
47      299999
48      399999
49      499999
50      599999


Last edited on
Do you mean to sum digits inside below loop this way?

1
2
3
4
5
6
7
8
for (int i = 10; i <= 1500; i++)
	{
		sum2 += i;
		if (sum2 == sum1)
		{
			sum1 += sum2;
		}
	}


for this logic, I got wrong output

OUTPUT:
7 --> 79
20 --> 199
7 --> 7
Last edited on
for this logic, I got wrong output

So why did you ask if the logic was correct? If it produces the wrong answer then it clearly isn't.

Look at the actual results. I'll give you a clue: you NEVER have to sum any digits to answer the question.
I am trying to figure out , how to get sequence without sum of digits?
there is probably some math answer that takes staring at it for a while to figure out, but at the very least, you can do this:
16 -1 is 15 and 1
16 - 2 is 14 and 2
...
16-9 is 9 and 7
16-10 is 10 and 6

...
there are only 16 sums including zero. But the ones with 2 digits are invalid, right?

since you only need to handle 0-50, the best answer is a lookup table of the answers :)
but pick any number, 47 for example.
now, you know that 9+9+9 is 27. 9+9+9+9, still too small. 5x9 is 45, too small. so its a 6 digit number. 45+2, actually. 299999, perhaps? See any algorithm you can use there? You can find the number of digits needed... and from there, figure out what those digits must be.
trying 16 that way, its < 9+9 and > 9 so 2 digits expected.
16-9 is 7, so you have your 7 and 9 digits, but is that best?
decrement the 9, increment the 7, 8 and 8 is the only other candidate.
so 97, 79, 88 ... which one?
Last edited on
Below is modified code logic, but still output is wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>


class Program { 
    
public:
 static int solution(int N)
{       int sum1 = 0;
	while (N != 0)
	{
	   sum1 += N % 10;
	   N /= 10;
	}
        return sum1;
}

static void smallnum(int N)
{
	int i = 1;
	while(1 != 0)
	{
	
		if (solution(i) == N)
		{
		     break;
		}
                i++;
	}

} 
};

int main()
{
    std::cout << Program::solution(16) << + " --> 79" << std::endl;
	std::cout << Program::solution(19) << + " --> 199" << std::endl;
	std::cout << Program::solution(7) << + " --> 7" << std::endl;

}




OUTPUT:

7 --> 79
10 --> 199
7 --> 7
 
Modified code again, but output is not expected.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>


class Program { 
    
public:
 static int solution(int N)
{       int sum1 = 0;
	while (N != 0)
	{
	   sum1 += N % 10;
	   N /= 10;
	}
        return sum1;
}

static void smallnum(int N)
{
	std::cout << (N % 9 + 1) 
                    * pow(10, (N / 9)) 
                - 1; 

} 
};

int main()
{
    std::cout << Program::solution(16) << + " --> 79" << std::endl;
	std::cout << Program::solution(19) << + " --> 199" << std::endl;
	std::cout << Program::solution(7) << + " --> 7" << std::endl;

}

OUTPUT:
7 --> 79
10 --> 199
7 --> 7
Got the answer now. I hope this is ok?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>


class Program { 
    
public:
 static int solution(int N)
{       int sum1 = 0;
	while (N != 0)
	{
	   sum1 += N % 9;
	   N /= 9;
	}
        return sum1;
}

static void smallnum(int N)
{
	std::cout << (N % 9 + 1) 
                    * pow(10, (N / 9)) 
                - 1; 

} 
};

int main()
{
    //std::cout << Program::solution(16) << + " --> 79" << std::endl;
	//std::cout << Program::solution(19) << + " --> 199" << std::endl;
	//std::cout << Program::solution(7) << + " --> 7" << std::endl;
    int N = 16; 
    Program::smallnum(N);
    return 0;
}


OUTPUT:
79 
Why a class? Why static members?

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int smallnum(int N)
{
	return (N % 9 + 1) * pow(10, (N / 9)) - 1; 
}

int main()
{
    for ( int n=0; n < 51; ++n ) {
        std::cout << n << " --> " << smallnum(n) << '\n';
    }
}
yes, it looks correct now.
Yes Keskiverto, we can avoid those static members, class not needed here as well.
@leo2008: try optimize code as much as you can.
Last edited on
Thanks everyone for quick support and guidance. Closing this thread now.
Here's a couple of alternatives.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace std;

int solution1( int N )
{
   return N >= 9 ? 9 + 10 * solution1( N - 9 ) : N;
}


int solution2( int N )
{
   return stoi( to_string( N % 9 ) + string( N / 9, '9' ) );
}


int main()
{
   for ( int i = 0; i <= 50; i++ ) cout << i << '\t' << solution1( i ) << '\t' << solution2( i ) << '\n';
}



Why a class? Why static members?

I think that it's because @leo2008 and @denver2020 are trying to copy java solutions that they have found elsewhere.
Last edited on
Registered users can post here. Sign in or register to post.