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

Published at Jul 13 2018
·
1 comment

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

```
import java.util.stream.IntStream;
public final class DifferenceOfSquaresCalculator {
public int computeSquareOfSumTo(int num) {
return (int) Math.pow(IntStream.rangeClosed(0, num).sum(), 2);
}
public int computeSumOfSquaresTo(int num) {
return IntStream.rangeClosed(0, num).map(x -> (int) Math.pow(x, 2)).sum();
}
public int computeDifferenceOfSquares(int num) {
return Math.abs(computeSquareOfSumTo(num) - computeSumOfSquaresTo(num));
}
}
```

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

Hey @rootulp!

For some addition fun around this exercise, check out a small collection of styles that have arisen: http://exercism.io/submissions/85467bc28d41452f92f21640d0946e90

Would be interesting to hear your thoughts on the trade-offs of each style!