Published at Aug 23 2019
·
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.

You are not expected to discover an efficient solution to this yourself from first principles; research is allowed, indeed, encouraged. Finding the best algorithm for the problem is a key skill in software engineering.

Go through the setup instructions for Javascript to install the necessary dependencies:

https://exercism.io/tracks/javascript/installation

Install assignment dependencies:

```
$ npm install
```

Execute the tests with:

```
$ npm test
```

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing `xtest`

to
`test`

.

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.

```
import { Squares } from './difference-of-squares';
describe('difference-of-squares', () => {
const squares1 = new Squares(1);
const squares5 = new Squares(5);
const squares100 = new Squares(100);
describe('Square the sum of the numbers up to the given number', () => {
xtest('square of sum 1', () => {
expect(squares1.squareOfSum).toBe(1);
});
xtest('square of sum 5', () => {
expect(squares5.squareOfSum).toBe(225);
});
xtest('square of sum 100', () => {
expect(squares100.squareOfSum).toBe(25502500);
});
});
describe('Sum the squares of the numbers up to the given number', () => {
xtest('sum of squares 1', () => {
expect(squares1.sumOfSquares).toBe(1);
});
xtest('sum of squares 5', () => {
expect(squares5.sumOfSquares).toBe(55);
});
xtest('sum of squares 100', () => {
expect(squares100.sumOfSquares).toBe(338350);
});
});
describe('Subtract sum of squares from square of sums', () => {
xtest('difference of squares 1', () => {
expect(squares1.difference).toBe(0);
});
xtest('difference of squares 5', () => {
expect(squares5.difference).toBe(170);
});
xtest('difference of squares 100', () => {
expect(squares100.difference).toBe(25164150);
});
});
});
```

```
const add = (accumulator, currentValue) => accumulator + currentValue;
export class Squares {
constructor(number) {
this.range = Array.from(new Array(number), (x, i) => i + 1);
}
get sumOfSquares() {
return this.range.reduce((sum, n) => sum + n ** 2);
}
get squareOfSum() {
return this.range.reduce(add) ** 2;
}
get difference() {
return this.squareOfSum - this.sumOfSquares;
}
}
```

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,276 exercises across 51 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments