5
\$\begingroup\$

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 ;
}
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Edward Jan 5 '15 at 11:40
  • \$\begingroup\$ A few hints: sqsum is not correct. How many times does the loop execute? What is the initial value of sum? \$\endgroup\$ – Edward Jan 5 '15 at 11:44
  • \$\begingroup\$ I changed my program . Please review it again ? \$\endgroup\$ – Himagra Chawla Jan 5 '15 at 11:50
  • \$\begingroup\$ @Himagra Chawla does it give you the correct answer now? Until it does this is off-topic \$\endgroup\$ – morbidCode Jan 5 '15 at 13:09
  • \$\begingroup\$ I edited the code and now it gives me correct :) \$\endgroup\$ – Himagra Chawla Jan 5 '15 at 13:21
5
\$\begingroup\$

Here are a few comments that may help you improve your code.

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;

Avoid abusing using namespace std

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';
}
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – abuzittin gillifirca Jan 8 '15 at 14:32
  • \$\begingroup\$ @abuzittingillifirca: Good point, thanks! I've updated my answer to include your suggestion. \$\endgroup\$ – Edward Jan 8 '15 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.