 # paulfioravanti's solution

## to Difference Of Squares in the Elixir Track

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.

## Running tests

Execute the tests with:

``````\$ mix test
``````

### Pending tests

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.

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

### difference_of_squares_test.exs

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

### test_helper.exs

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

@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)

number
|> :math.pow(@square)
|> Kernel.+(acc)
end
end``````