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

# mk2s's solution

## to Difference Of Squares in the CFML Track

Published at Mar 04 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
``````

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/

## Source

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

## Submitting Incomplete Solutions

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

### DifferenceOfSquaresTest.cfc

``````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' );
});

});

});

}

}``````

### SolutionTest.cfc

``````component extends="DifferenceOfSquaresTest" {

function beforeAll(){
SUT = createObject( 'Solution' );
}

}``````
``````/**
* Your implmentation of the DifferenceOfSquares exercise
*/
component {

function squareOfSum( number ) {
var result = 0;

for(i=1; i<=number; i=i+1){
result += i;
}
return result * result;
}

function sumOfSquares( number ) {
var result = 0;
for(i=1; i<=number; i=i+1){
result += i*i;
}
return result;
}

function differenceOfSquares( number ) {
return squareOfSum(number) - sumOfSquares(number);
}

}``````