+ 1
Help with C++
So, I have to write program which for loaded natural (integer) number (5<N<100) has to show how many ways that number can be in form N = x + y + z, where is x < y < z. And it also has to show how that number 'N' can be separated into the sum of three different natural (integer) numbers, x, y and z, where x < y < z is true. For example, if we type number 10, it shows number 4 (ways that this number can be separated into sum of three numbers) and in new line it shows 10=1+2+7; 10=1+3+6; 10=1+4+5; 10=2+3+5;
2 Answers
+ 3
Well there are certainly some mathematical constraints that would help with this problem.
1. None of the numbers can be N - 2 or greater. E.g. N = 10. The lowest x and y can be is 1 and 2 which means z can never be greater 7.
2. Try using z as the first check, because the largest number will be the deciding factor for what the other two numbers can be. N = 50, using z = 34, the sum of x and y has to be 16 which means y can't be greater than 15.
3. You will need a loop of course. Personally, I would wrap everything in a while loop. Inside of that loop I would start with checking if the number is 6, because x =1, y = 2, z = 3 is the only solution for 6. Then I would start with z = N - 4, since every number greater than 6 has at least z = N - 3, y = 2, x = 1 as a solution.
4. Write out the solutions for some of the smaller numbers, like 7, 8 and 9. Try to find a pattern and think about how to code the pattern.
If you need help, feel free to comment on my codes for help.
+ 1
Thanks for the answer, I will do something about this what you wrote :)