🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # epequeno's solution

## to Difference Of Squares in the Elm Track

Published at Apr 19 2021 · 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.

You are not expected to discover an efficient solution to this yourself from first principles; research is allowed, indeed, encouraged. Finding the best algorithm for the problem is a key skill in software engineering.

## Elm Installation

Refer to the Installing Elm page for information about installing elm.

## Writing the Code

The code you have to write is located inside the `src/` directory of the exercise. Elm automatically installs packages dependencies the first time you run the tests so we can start by running the tests from the exercise directory with:

``````\$ elm-test
``````

To automatically run tests again when you save changes:

``````\$ elm-test --watch
``````

As you work your way through the tests suite in the file `tests/Tests.elm`, be sure to remove the `skip <|` calls from each test until you get them all passing!

## Source

Problem 6 at Project Euler http://projecteuler.net/problem=6

## Submitting Incomplete Solutions

It is possible to submit an incomplete solution so you can see how others have completed the exercise.

### Tests.elm

``````module Tests exposing (tests)

import DifferenceOfSquares exposing (difference, squareOfSum, sumOfSquares)
import Expect
import Test exposing (..)

tests : Test
tests =
describe "DifferenceOfSquares"
[ describe "square the sum of the numbers up to the given number"
[ test "square of sum 5" <|
\() -> Expect.equal 225 (squareOfSum 5)
, skip <|
test "square of sum 10" <|
\() -> Expect.equal 3025 (squareOfSum 10)
, skip <|
test "square of sum 100" <|
\() -> Expect.equal 25502500 (squareOfSum 100)
]
, describe "sum the squares of the numbers up to the given number"
[ skip <|
test "sum of squares 5" <|
\() -> Expect.equal 55 (sumOfSquares 5)
, skip <|
test "sum of squares 10" <|
\() -> Expect.equal 385 (sumOfSquares 10)
, skip <|
test "sum of squares 100" <|
\() -> Expect.equal 338350 (sumOfSquares 100)
]
, describe "subtract sum of squares from square of sums"
[ skip <|
test "difference of squares 0" <|
\() -> Expect.equal 0 (difference 0)
, skip <|
test "difference of squares 5" <|
\() -> Expect.equal 170 (difference 5)
, skip <|
test "difference of squares 10" <|
\() -> Expect.equal 2640 (difference 10)
, skip <|
test "difference of squares 100" <|
\() -> Expect.equal 25164150 (difference 100)
]
]``````
``````module DifferenceOfSquares exposing (difference, squareOfSum, sumOfSquares)

squareOfSum : Int -> Int
squareOfSum n =
(List.range 1 n |> List.sum) ^ 2

sumOfSquares : Int -> Int
sumOfSquares n =
List.range 1 n
|> List.map (\x -> x ^ 2)
|> List.sum

difference : Int -> Int
difference n =
squareOfSum n - sumOfSquares n``````