 # rootulp's solution

## to Difference Of Squares in the Elm 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.

## Elm Installation

Refer to the Exercism help page for Elm installation and learning resources.

## Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed.

``````\$ elm-package install --yes
``````

Execute the tests with:

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

Automatically run tests again when you save changes:

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

As you work your way through the test suite, 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's possible to submit an incomplete solution so you can see how others have completed the exercise.

### Tests.elm

``````module Tests exposing (..)

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

### elm-package.json

``````{
"version": "3.0.0",
"summary": "Exercism problems in Elm.",
"repository": "https://github.com/exercism/elm.git",
"source-directories": [
".",
".."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "5.0.0 <= v < 6.0.0",
"elm-community/elm-test": "4.0.0 <= v < 5.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}``````
``````module DifferenceOfSquares exposing (..)

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

squareOfSum: Int -> Int
squareOfSum n =
List.range 0 n
|> List.sum
|> square

sumOfSquares: Int -> Int
sumOfSquares n =
List.range 0 n
|> List.map square
|> List.sum

square: number -> number
square n =
n ^ 2``````