Sum square difference

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

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.

 

Formula review:

     The square and continuous natural numbers: 1²+2²+3²+. . . . . . +n² = n*(n+1)*(2*n+1)/6

     The cubic and continuous natural numbers: 13+23+33+…+n3=[n(n+1)/2]2

     And then found that continuous natural numbers of cubic and even and continuous natural numbers and the square is the same....

     Continuous natural numbers and the square: [1+2+3+...+n]2  = [n(n+1)/2]2

Formula to be

#include <iostream>
#include <cmath>
using namespace std;

int main(int argc, char const *argv[])
{
    int n;
    while( cin >> n )
    {
        int sum1 = 0, sum2 = 0;
        sum1 = n*(n+1)*(2*n+1)/6;
        sum2 = (int)pow((double)((1+n)*n/2),2);
        cout <<abs(sum1 - sum2) << endl;
    }
    return 0;
}

View Code

 

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Boris at November 19, 2013 - 10:39 AM