Exercism v3 launches on Sept 1st 2021. Learn more! ðŸš€ðŸš€ðŸš€

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

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 {
numeric function squareOfSum( required numeric number ) {
var result = 0;
for (var currentNumber = 1; currentNumber <= arguments.number ; currentNumber++) {
result += currentNumber;
}
return result*result;
}
numeric function sumOfSquares( required numeric number ) {
var result = 0;
for (var currentNumber = 1; currentNumber <= arguments.number ; currentNumber++) {
result += currentNumber*currentNumber;
}
return result;
}
numeric function differenceOfSquares( required numeric number ) {
return squareOfSum(arguments.number) - sumOfSquares(arguments.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