Published at Jul 20 2018
·
0 comments

Instructions

Test suite

Solution

Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.

The square of the sum of the first ten natural numbers is (1 + 2 + ... + 10)² = 55² = 3025.

The sum of the squares of the first ten natural numbers is 1² + 2² + ... + 10² = 385.

Hence the difference between the square of the sum of the first ten natural numbers and the sum of the squares of the first ten natural numbers is 3025 - 385 = 2640.

Make sure you have read the C++ page on exercism.io. This covers the basic information on setting up the development environment expected by the exercises.

Get the first test compiling, linking and passing by following the three rules of test-driven development. Create just enough structure by declaring namespaces, functions, classes, etc., to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, uncomment the next test by moving the following line past the next test.

```
#if defined(EXERCISM_RUN_ALL_TESTS)
```

This may result in compile errors as new constructs may be invoked that you haven't yet declared or defined. Again, fix the compile errors minimally to get a failing test, then change the code minimally to pass the test, refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C++11 facilities in preference to writing your own low-level algorithms or facilities by hand. CppReference is a wiki reference to the C++ language and standard library. If you are new to C++, but have programmed in C, beware of C traps and pitfalls.

Problem 6 at Project Euler http://projecteuler.net/problem=6

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
#include "difference_of_squares.h"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(up_to_5)
{
BOOST_REQUIRE_EQUAL(225, squares::square_of_sums(5));
BOOST_REQUIRE_EQUAL(55, squares::sum_of_squares(5));
BOOST_REQUIRE_EQUAL(170, squares::difference(5));
}
#if defined(EXERCISM_RUN_ALL_TESTS)
BOOST_AUTO_TEST_CASE(up_to_10)
{
BOOST_REQUIRE_EQUAL(3025, squares::square_of_sums(10));
BOOST_REQUIRE_EQUAL(385, squares::sum_of_squares(10));
BOOST_REQUIRE_EQUAL(2640, squares::difference(10));
}
BOOST_AUTO_TEST_CASE(up_to_100)
{
BOOST_REQUIRE_EQUAL(25502500, squares::square_of_sums(100));
BOOST_REQUIRE_EQUAL(338350, squares::sum_of_squares(100));
BOOST_REQUIRE_EQUAL(25164150, squares::difference(100));
}
#endif
```

```
#include "difference_of_squares.h"
#include <cmath>
int squares::square_of_sums(int num) {
return std::pow((num*(num+1)/2), 2);
}
int squares::sum_of_squares(int num) {
return num*(num+1)*(2*num+1)/6;
}
int squares::difference(int num) {
return squares::square_of_sums(num) - squares::sum_of_squares(num);
}
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,373 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments