 # davearonson's solution

## to Difference Of Squares in the Elixir Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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:

``````\$ elixir difference_of_squares_test.exs
``````

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

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

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

``````if !System.get_env("EXERCISM_TEST_EXAMPLES") do
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``````