Project Euler Problem #6 - sum square difference

The sum of the squares of the first ten natural numbers is:

$1^2 + 2^2 + ... + 10^2 = 385$

The square of the sum of the first ten natural numbers is:

$(1 + 2 + ... + 10)^2 = 55^2 = 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.

//Project euler problem 6

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

unsigned int sum(int);
unsigned int sqsum(int);

int main()
{
cout << sum(100)*sum(100) - sqsum(100);
}

unsigned int sum(int n) // function for finding sum of n numbers
{
return (n*(n+1))/2;
}

unsigned int sqsum(int n) // function for finding sum of squares
{
return ((n)*(n+1)*(2*n +1 ))/6 ;
}

• Welcome to Code Review! Once your code is working, we'll be happy to help you make it better but broken code is off topic here. See the help center for more details. – Edward Jan 5 '15 at 11:40
• A few hints: sqsum is not correct. How many times does the loop execute? What is the initial value of sum? – Edward Jan 5 '15 at 11:44
• I changed my program . Please review it again ? – Himagra Chawla Jan 5 '15 at 11:50
• @Himagra Chawla does it give you the correct answer now? Until it does this is off-topic – lightning_missile Jan 5 '15 at 13:09
• I edited the code and now it gives me correct :) – Himagra Chawla Jan 5 '15 at 13:21

Use only necessary #includes

The #include <cmath> line is not necessary and can be safely removed.

Avoid "magic numbers"

Since the constant 100 is used several times within the code, it makes sense to create a constant instead. This also helps with troubleshooting since you can quickly change it to small values (e.g. 10) for which you already know the answer:

const int maxnum = 100;


Don't abuse using namespace std

Especially in a very simple program like this, there's little reason to use that line. Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid.

Eliminate function prototypes by ordering

If you put the sum and sqsum implementations above main in the source code, you don't need the function prototypes.

End the last console output with a newline

The output to the program is a little neater on most machines if you output a newline character after the last of the program's console output.

Avoid unnecessary function calls

It doesn't make a huge difference in this code, but the code dosn't need to make two calls to sum(). Instead, the main routine might look like this:

int main()
{
const int maxnum = 100;
unsigned square_of_sum = sum(maxnum);
square_of_sum *= square_of_sum;
std::cout << square_of_sum - sqsum(maxnum)-a << '\n';
}

• sumofsquares stands first for sum and then square of the sum; despite its name, never for sum of the squares. Valid identifiers sum, square_of_the_sum are available. If you are feeling terse s (for sum) and s2 (for square of sum) are also acceptable. – abuzittin gillifirca Jan 8 '15 at 14:32
• @abuzittingillifirca: Good point, thanks! I've updated my answer to include your suggestion. – Edward Jan 8 '15 at 14:37