-4

I am not that skilled or advanced in C++ and I have trouble solving a problem. I know how to do it mathematically but I can't write the source code, my algorithm is wrong and messy.

So, the problem is that I have to write a code that reads a number ( n ) from the keyboard and then it has to find a sum that is equal to n squared ( n ^ 2 ) and the number of sum's elements has to be equal to n. For example 3^2 = 9, 3^2 = 2 + 3 + 4, 3 elements and 3^2 is 9 = 2 + 3 + 4. I had several attempts but none of them were successful. I know I'm borderline stupid but at least I tried.

If anyone has the time to look over this problem and is willing to help me I'd be very thankful.

1

#include <iostream>
#include <list>
#include <algorithm>

using namespace std;

int main()
{

  //1,3,5,7,9,11,13,15,17,19,21,23,25,27..

  int n;
  list<int> l;

  cin >> n;

  if ( n % 2 == 0 ){
      cout << "Wrong." << endl;
  }

  for ( int i = 1; i <= 99;i+=2){
      l.push_back(i);
  }

  //List is full with 1,3,5,7,9,11,13,15,17,19,21,23,25,27..

  list<int>::iterator it = find(begin(l),end(l), n);

}

2

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
// 3^2 = 2 + 3 + 4
// 7^2 = 4 + 5 + 6 + 7 + 8 + 9 + 10

int n;

int numbers[100];

for (int i = 0; i <= 100; i++){
    numbers[i] = i;
}

cin >> n;

int requiredSum;

requiredSum = n * n;

//while(sum < requiredSum){
//    for(int i = 1; i < requiredSum; i++){
//        sum += i;
//        sumnums.push_back(sum);
//    }
//}

int sum = 0;

std::vector<int> sumnums;

while(sum < requiredSum){
    for(int i = 1; i < requiredSum; i++){
        sum += i;
        sumnums.push_back(sum);
    }
}

for(int i=0; i<sumnums.size(); ++i)
    std::cout << sumnums[i] << ' ';

}

Update:

The numbers of the sum have to be consecutive numbers.Like 3 * 3 has to be equal to 2 + 3 + 4 not 3 + 3 + 3.

So, my first try was that I found a rule for each sum. Like 3 * 3 = 2 + 3 + 4, 5 * 5 = 3 + 4 + 5 + 6 + 7, 7 * 7 = 4 + 5 + 6 + 7 + 8 + 9 + 10.

Every sum starts with the second element of the previous sum and continues for a number of elements equal to n - 1, like 3 * 3 = 2 + 3 + 4, 5 * 5 , the sum for 5 * 5 starts with 3 + another 4 elements.

And another algorithm would be @molbdnilo 's, like 3 * 3 = 3 + 3 + 3 = 3 + 3 + 3 - 1 + 1, 3 * 3 = ( 3 - 1 ) + 3 + ( 3 + 1 ), but then 5 * 5 = (5 - 2) + ( 5 - 1 ) + 5 + 5 + 1 + 5 + 2

Fxuram
  • 1
  • 3
  • 1
    Is there any constraint on what should be the numbers that are forming the sum? Should they be consecutive numbers? – wdc Jun 29 '18 at 14:22
  • It appears from your first attempt that `n` must be odd. In that case, here’s a hint: `3^2 = 3*3 = 3+3+3 = 3+3+3-1+1 = (3-1) + 3 + (3+1)`. And `5*5 = 5+5+5+5+5 - 3 + 3 = 3+4+5+6+7`. – molbdnilo Jun 29 '18 at 14:36
  • @molbdnilo yes I discovered more ways to do it mathematically but I don't know why but I just can't write the pseudocode or the source code for my math, one of the ways is just like yours but could you give me a code example? and yes after some simple algebra I got that n has to be odd in order to get a sum out of it's square. – Fxuram Jun 29 '18 at 15:23

3 Answers3

1

If there is not constraints on what are the elements forming the sum, the simplest solution is just to sum up the number n, n times, which is always n^2.

int main()
{
    int n;
    cout<<"Enter n: ";
    cin >> n;

    for(int i=0; i<n-1; i++){
        cout<<n<<"+";
    }

    cout<<n<<"="<<(n*n);

    return 0;
}
wdc
  • 2,623
  • 1
  • 28
  • 41
  • Yes sorry I forgot to write it, they have to be especially consecutive like 3 * 3 has to be equal to = 2 + 3 + 4 not 3 + 3 + 3, I'll update it – Fxuram Jun 29 '18 at 15:20
1

Let's do a few special cases by hand.
(The division here is integer division.)

3^2: 9  
 2 + 3 + 4 = 9
x-1  x  x+1
1 is 3/2

5: 25
 3 + 4 + 5 + 6 + 7 = 25
x-2 x-1  x  x+1 x+2
2 is 5/2

7: 49
 4 + 5 + 6 + 7 + 8 + 9 + 10
x-3 x-2 x-1  x  x+1 x+2 x+3
3 is 7/2

It appears that we're looking for the sequence from n - n / 2 to n + n / 2.
(Or, equivalently, n / 2 + 1 to n / 2 + n, but I like symmetry.)

Assuming that this is correct (the proof left as an exercise ;-):

int main()
{
    int n = 0;
    std::cin >> n;
    if (n % 2 == 0)
    {
        std::cout << "Must be odd\n";
        return -1;
    }
    int delta = n / 2;
    for (int i = n - delta; i <= n + delta; i++)
    {
        std::cout << i << " ";
    }
    std::cout << std::endl;
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • It appears to be correct and I hope it'll be in all cases. Thank you so much for the work you've done for me, very much appreciated. I was just testing a case like yours with a for loop and with a remainder, in your case delta, I almost figured it out myself but again thank you so much for taking your time to solve this for me. – Fxuram Jun 29 '18 at 19:24
0

Firstly, better use std::vector<> than std::list<>, at least while you have less than ~million elements (it will be faster, because of inside structure of the containers).

Secondly, prefer ++i usage, instead of, i++. Specially in situation like that

...for(int i = 1; i < requiredSum; i++)...

Take a look over here

Finally,

the only error you had that you were simply pushing new numbers inside container (std::list, std::vector, etc.) instead of summing them, so

while(sum < requiredSum){
    for(int i = 1; i < requiredSum; i++){
        sum += i;
        sumnums.push_back(sum);
    }

change to

// will count our numbers
amountOfNumbers = 1;
while(sum < requiredSum && amountOfNumber < n)
{
   sum += amountOfNumbers;
   ++amountOfNumbers;

}
// we should make -1 to our amount
--amountOfNumber;
// now let's check our requirements...
if(sum == requiredSum && amountOfNumbers == n)
{
   cout << "Got it!";
   // you can easily cout them, if you wish, because you have amountOfNumbers.
   // implementation of that I am leaving for you, because it is not hard ;) 
}
else
{
   cout << "Damn it!;
}

I assumed that you need sequential sum of numbers that starts from 1 and equals to n*n and their amount equils to n.

If something wrong or need explanation, please, do not hesitate to contact me.

Upd. amountOfNumber < n intead <= Also, regarding "not starting from 1". You said that you know how do it on paper, than could you provide your algorithm, then we can better understand your problem.

Upd.#2: Correct and simple answer. Sorry for such a long answer. I came up with a great and simple solution.

Your condition requires this equation x+(x+1)+(x+2)+... = n*n to be true then we can easily find a solution.

nx+ArPrg = nn, where is ArPrg - Arithmetic progression (ArPrg = ((n-1)*(1+n-1))/2)

After some manipulation with only unknown variable x, our final equation will be

#include <iostream>

int main() 
{

    int n;
    std::cout << "Enter x: ";
    std::cin >> n;

    auto squareOfN = n * n;

    if (n % 2 == 0)
    {
        std::cout << "Can't count this.\n";
    }

    auto x = n - (n - 1) / 2;

    std::cout << "Our numbers: ";
    for (int i = 0; i < n; ++i)
        std::cout << x + i << " ";

    return 0;
}

Math is cool :)

Sviatoslav V.
  • 671
  • 6
  • 18
  • It really doesn't seem to work. I tried 3 as input and I get Damn It. I'm sure it is a easy way around it. And the numbers have to be consecutive but they don't necessarily have to start from 1. Again, 3 * 3 = 2 + 3 + 4, the sequence of numbers does not start from 1, thanks for the try though, very much appreciated. or 7 * 7 = 4 + 5 + 6 + 7 + 8 + 9 + 10 – Fxuram Jun 29 '18 at 15:56
  • And if you get a correct answer the program has to output the sum that meets the requirements, like if I input 3, the output has to be 2, 3, 4 – Fxuram Jun 29 '18 at 15:59
  • @Fxuram I updated algorithm with no loops, no tough calculations. Just clear and cool math. I see that solution already here, but it might also fit, or, at least, you will know one more solution. – Sviatoslav V. Jun 29 '18 at 20:35
  • I tried the program and it works very well, thank you very much for taking the time to solve this for me. The only thing I have to say is that I don't know why but codeblocks doesn't allow me to compile it with auto so I just edited the code with integer data type. – Fxuram Jun 30 '18 at 09:30