ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Dec 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.

To run the code in this exercise, you will only need to have CommandBox CLI installed. This binary runs CFML code from the command line.

To run the tests, `cd`

into the exercise folder and run the following:

```
box task run TestRunner
# Or start up a test watcher that will rerun when files change
box task run TestRunner --:watcher
```

The tests leverage a library called TestBox which supports xUnit and BDD style of testing. All test suites will be written in the BDD style which uses closures to define test specs. You won't need to worry about installing TestBox. The CLI test runner will take care of that for you. You just need to be connected to the internet the first time you run it. You can read more about it here:

https://testbox.ortusbooks.com/content/

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.

```
component extends="testbox.system.BaseSpec" {
function beforeAll(){
SUT = createObject( 'DifferenceOfSquares' );
}
function run(){
describe( "My DifferenceOfSquares class", function(){
describe( 'Square the sum of the numbers up to the given number', function(){
it( 'square of sum 1', function(){
expect( SUT.squareOfSum( number='1' ) ).toBe( '1' );
});
it( 'square of sum 5', function(){
expect( SUT.squareOfSum( number='5' ) ).toBe( '225' );
});
it( 'square of sum 100', function(){
expect( SUT.squareOfSum( number='100' ) ).toBe( '25502500' );
});
});
describe( 'Sum the squares of the numbers up to the given number', function(){
it( 'sum of squares 1', function(){
expect( SUT.sumOfSquares( number='1' ) ).toBe( '1' );
});
it( 'sum of squares 5', function(){
expect( SUT.sumOfSquares( number='5' ) ).toBe( '55' );
});
it( 'sum of squares 100', function(){
expect( SUT.sumOfSquares( number='100' ) ).toBe( '338350' );
});
});
describe( 'Subtract sum of squares from square of sums', function(){
it( 'difference of squares 1', function(){
expect( SUT.differenceOfSquares( number='1' ) ).toBe( '0' );
});
it( 'difference of squares 5', function(){
expect( SUT.differenceOfSquares( number='5' ) ).toBe( '170' );
});
it( 'difference of squares 100', function(){
expect( SUT.differenceOfSquares( number='100' ) ).toBe( '25164150' );
});
});
});
}
}
```

```
component extends="DifferenceOfSquaresTest" {
function beforeAll(){
SUT = createObject( 'Solution' );
}
}
```

```
/**
* Your implmentation of the DifferenceOfSquares exercise
*/
component {
function squareOfSum( number ) {
var sum = 0;
for (var i = 1; i <= number; i++) {
sum += i;
}
return sum * sum;
}
function sumOfSquares( number ) {
var numArr = [];
var sqNumArr = [];
var sum = 0
for (var i = 1; i <= number; i++){
arrayAppend(numArr, i);
}
for (var j = 1; j <= arrayLen(numArr); j++) {
arrayAppend(sqNumArr, (j * j ));
}
for (num in sqNumArr) {
sum += num
}
return sum;
}
function differenceOfSquares( number ) {
return squareOfSum(number) - sumOfSquares(number);
}
}
```

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

## Community comments