Published at Jun 18 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.

Execute the tests with:

```
$ mix test
```

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by
commenting out the relevant `@tag :pending`

with a `#`

symbol.

For example:

```
# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
```

Or, you can enable all the tests by commenting out the
`ExUnit.configure`

line in the test suite.

```
# ExUnit.configure exclude: :pending, trace: true
```

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

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.

```
defmodule DifferenceOfSquaresTest do
use ExUnit.Case
# @tag :pending
test "square of sum to 5" do
assert Squares.square_of_sum(5) == 225
end
@tag :pending
test "sum of squares to 5" do
assert Squares.sum_of_squares(5) == 55
end
@tag :pending
test "difference of sum to 5" do
assert Squares.difference(5) == 170
end
@tag :pending
test "square of sum to 10" do
assert Squares.square_of_sum(10) == 3025
end
@tag :pending
test "sum of squares to 10" do
assert Squares.sum_of_squares(10) == 385
end
@tag :pending
test "difference of sum to 10" do
assert Squares.difference(10) == 2640
end
@tag :pending
test "square of sum to 100" do
assert Squares.square_of_sum(100) == 25_502_500
end
@tag :pending
test "sum of squares to 100" do
assert Squares.sum_of_squares(100) == 338_350
end
@tag :pending
test "difference of sum to 100" do
assert Squares.difference(100) == 25_164_150
end
end
```

```
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)
```

```
defmodule Squares do
@moduledoc """
Calculate sum of squares, square of sum, difference between two sums from 1 to a given end number.
"""
@square 2
@doc """
Calculate sum of squares from 1 to a given end number.
"""
@spec sum_of_squares(pos_integer) :: pos_integer
def sum_of_squares(number), do: Enum.reduce(1..number, &add_square/2)
@doc """
Calculate square of sum from 1 to a given end number.
"""
@spec square_of_sum(pos_integer) :: pos_integer
def square_of_sum(number) do
1..number
|> Enum.sum()
|> :math.pow(@square)
end
@doc """
Calculate difference between sum of squares and square of sum from 1 to a given end number.
"""
@spec difference(pos_integer) :: pos_integer
def difference(number), do: square_of_sum(number) - sum_of_squares(number)
defp add_square(number, acc) do
number
|> :math.pow(@square)
|> Kernel.+(acc)
end
end
```

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,373 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments