Published at Feb 15 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.

You can run all the tests for an exercise by entering

```
$ gradle test
```

in your terminal.

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.

```
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class DifferenceOfSquaresCalculatorTest {
private DifferenceOfSquaresCalculator calculator;
@Before
public void setUp() {
calculator = new DifferenceOfSquaresCalculator();
}
@Test
public void testSquareOfSumUpToOne() {
int expected = 1;
int actual = calculator.computeSquareOfSumTo(1);
assertEquals(expected, actual);
}
@Ignore("Remove to run test")
@Test
public void testSquareOfSumUpToFive() {
int expected = 225;
int actual = calculator.computeSquareOfSumTo(5);
assertEquals(expected, actual);
}
@Ignore("Remove to run test")
@Test
public void testSquareOfSumUpToHundred() {
int expected = 25502500;
int actual = calculator.computeSquareOfSumTo(100);
assertEquals(expected, actual);
}
@Ignore("Remove to run test")
@Test
public void testSumOfSquaresUpToOne() {
int expected = 1;
int actual = calculator.computeSumOfSquaresTo(1);
assertEquals(expected, actual);
}
@Ignore("Remove to run test")
@Test
public void testSumOfSquaresUpToFive() {
int expected = 55;
int actual = calculator.computeSumOfSquaresTo(5);
assertEquals(expected, actual);
}
@Ignore("Remove to run test")
@Test
public void testSumOfSquaresUpToHundred() {
int expected = 338350;
int actual = calculator.computeSumOfSquaresTo(100);
assertEquals(expected, actual);
}
@Ignore("Remove to run test")
@Test
public void testDifferenceOfSquaresUpToOne() {
int expected = 0;
int actual = calculator.computeDifferenceOfSquares(1);
assertEquals(expected, actual);
}
@Ignore("Remove to run test")
@Test
public void testDifferenceOfSquaresUpToFive() {
int expected = 170;
int actual = calculator.computeDifferenceOfSquares(5);
assertEquals(expected, actual);
}
@Ignore("Remove to run test")
@Test
public void testDifferenceOfSquaresUpToHundred() {
int expected = 25164150;
int actual = calculator.computeDifferenceOfSquares(100);
assertEquals(expected, actual);
}
}
```

```
class DifferenceOfSquaresCalculator {
int computeSquareOfSumTo(int input) {
return (int) Math.pow(input * (input + 1) / 2, 2);
}
int computeSumOfSquaresTo(int input) {
return input * (input + 1) * (2 * input + 1) / 6;
}
int computeDifferenceOfSquares(int input) {
return computeSquareOfSumTo(input) - computeSumOfSquaresTo(input);
}
}
```

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?

## Community comments