Published at Jul 13 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.

Execute the tests with:

```
$ elixir difference_of_squares_test.exs
```

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
```

For more detailed information about the Elixir track, please see the help page.

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.

```
if !System.get_env("EXERCISM_TEST_EXAMPLES") do
Code.load_file("difference_of_squares.exs", __DIR__)
end
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)
defmodule DifferenceOfSquaresTest do
use ExUnit.Case
# @tag :pending
test "square of sums to 5" do
assert Squares.square_of_sums(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 sums to 5" do
assert Squares.difference(5) == 170
end
@tag :pending
test "square of sums to 10" do
assert Squares.square_of_sums(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 sums to 10" do
assert Squares.difference(10) == 2640
end
@tag :pending
test "square of sums to 100" do
assert Squares.square_of_sums(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 sums to 100" do
assert Squares.difference(100) == 25_164_150
end
end
```

```
defmodule Squares do
@moduledoc """
Calculate sum of squares, square of sums, difference between two sums from 1 to a given end number.
"""
@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
# from https://en.wikipedia.org/wiki/Square_pyramidal_number
(2 * :math.pow(number, 3) + 3 * number * number + number) / 6
end
@doc """
Calculate square of sums from 1 to a given end number.
"""
@spec square_of_sums(pos_integer) :: pos_integer
def square_of_sums(number) do
# n*(n+1)/2 is standard formula for sum(1..n)
:math.pow((number * (number + 1) / 2), 2)
end
@doc """
Calculate difference between sum of squares and square of sums from 1 to a given end number.
"""
@spec difference(pos_integer) :: pos_integer
def difference(number) do
square_of_sums(number) - sum_of_squares(number)
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,388 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments