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

# rootulp's solution

## to Difference Of Squares in the Java Track

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.

# Running the tests

You can run all the tests for an exercise by entering

``````\$ gradle test
``````

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

### DifferenceOfSquaresCalculatorTest.java

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

}``````

Find this solution interesting? Ask the author a question to learn more.

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!

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