### integer sum problem I am trying to solve the problem. Is my logic correct?

Below is my code.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354`` `````` #include 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?

 ``12345678`` ``````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.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849`` ``````#include 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.

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include 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?

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include 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?

 ``12345678910111213`` ``````#include 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.

 ``1234567891011121314151617181920`` ``````#include #include 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.