The sum of the squares of the first ten natural numbers is,
12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
Solution:
Maybe I am overlooking a snazzy shortcut or something, but this seems to be the most straightforward problem I have encountered on PE. Sum the squares, and square the sum . Take the difference, and be done with it.
int main (void)
{
unsigned int sqr_o_sum = 0;
unsigned int sum_o_sqr = 0;
unsigned int difference;
int i;
for (i = 1; i <= 100; i++)
{
sum_o_sqr += i*i;
sqr_o_sum += i;
}
sqr_o_sum *= sqr_o_sum;
difference = sqr_o_sum - sum_o_sqr;
printf("Difference is %d\n", difference);
return 1;
}
Compile and watch the solution appear before your eyes!
No comments:
Post a Comment