
Joseph H. answered 10/09/15
Tutor
4.8
(6)
Tutor with Electrical Engineering and Computer Science Degrees
One important thing to notice is that the sequence of numbers you gave, each have integer square roots.
12=1
22=4
32=9
42=16
52=25
...
So you can see if we can just generate the numbers 1,2,3,4,5... and sum their squares, then we can get the solution you are seeking.
This is probably a good job for a loop since we want to generate a bunch of numbers in a sequence. A for loop might be good.
int num;
/* This will generate values of num 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14*/
for (num = 1; num*num < 200; num = num + 1){
}
Now we have to put something inside the for loop to keep a running sum of num2. This will look like
int num;
int totalSum = 0;
/* This will generate values of num 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14*/
for (num = 1; num*num < 200; num = num + 1){
/*This will keep a running sum of the square of num*/
totalSum = totalSum + (num*num);
}
totalSum = totalSum + (num*num);
}
However, this will not quite give the right answer, because your equation had negative signs. It looks like every other number gets subtracted.
So we can add another variable to keep track of the sign
int num;
int totalSum = 0;
int numSign = 1;
int totalSum = 0;
int numSign = 1;
/* This will generate values of num 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14*/
for (num = 1; num*num < 200; num = num + 1){
/*This will keep a running sum of the square of num*/
totalSum = totalSum + numSign*(num*num);
/*This will change the sign each time through the loop */
numSign=numSign*-1;
}