# confidentidiot's solution

## to Difference Of Squares in the D Track

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

## Getting Started

Make sure you have read D page on exercism.io. This covers the basic information on setting up the development environment expected by the exercises.

## Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development. Create just enough structure by declaring namespaces, functions, classes, etc., to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, uncomment the next test by moving the following line past the next test.

``````static if (all_tests_enabled)
``````

This may result in compile errors as new constructs may be invoked that you haven't yet declared or defined. Again, fix the compile errors minimally to get a failing test, then change the code minimally to pass the test, refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard D facilities in preference to writing your own low-level algorithms or facilities by hand. DRefLanguage and DReference are references to the D language and D standard library.

## 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.

### difference_of_squares.d

``````module difference_of_squares;

unittest {
const int allTestsEnabled = 0;

assert(squares(5).squareOfSum == 225);
static if (allTestsEnabled) {
assert(squares(10).squareOfSum == 3_025);
assert(squares(100).squareOfSum == 25_502_500);

assert(squares(5).sumOfSquares == 55);
assert(squares(10).sumOfSquares == 385);
assert(squares(100).sumOfSquares == 338_350);

assert(squares(0).difference == 0);
assert(squares(5).difference == 170);
assert(squares(10).difference == 2_640);
assert(squares(100).difference == 25_164_150);
}

}``````
``````module difference_of_squares;
import std.conv;

unittest {
const int allTestsEnabled = 1;

static if (allTestsEnabled) {

assert(squares(5).squareOfSum == 225);
assert(squares(10).squareOfSum == 3_025);
assert(squares(100).squareOfSum == 25_502_500);

assert(squares(5).sumOfSquares == 55);
assert(squares(10).sumOfSquares == 385);
assert(squares(100).sumOfSquares == 338_350);

assert(squares(0).difference == 0);
assert(squares(5).difference == 170);
assert(squares(10).difference == 2_640);
assert(squares(100).difference == 25_164_150);
}

}

struct Dos {
int squareOfSum;
int sumOfSquares;
int difference;
}

Dos squares(int x) {
Dos result;
int s1 = 0;
int s2 = 0;

for (int i = 1; i < x + 1; ++i) {
s1 = s1 + i;
s2 = s2 + i^^2;
}

s1 = s1^^2;

result.squareOfSum = s1;
result.sumOfSquares = s2;
result.difference = s1 - s2;

return result;
}

void main() {}``````