🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of damiankorz

damiankorz's solution

to Difference Of Squares in the CFML Track

Published at Dec 13 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/

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

}
component {
	public numeric function squareOfSum( required numeric number ) {
		var sum = 0;
		for( var i=1; i<=number; i++ ){
			sum += i;
		}
		return sum^2;
	}
	public numeric function sumOfSquares( required numeric number ) {
		 var sum = 0;
		 for( var i=1; i<=number; i++ ){
			sum += (i^2);
		 }
		 return sum;
	}
	public numeric function differenceOfSquares( required numeric number ) {
		var difference = squareOfSum( number ) - sumOfSquares( number );
		return difference;
	}
}

What can you learn from this solution?

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?