Exercism v3 launches on Sept 1st 2021. Learn more! ðŸš€ðŸš€ðŸš€

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.

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

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!

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.

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

```
{
"version": "3.0.0",
"summary": "Exercism problems in Elm.",
"repository": "https://github.com/exercism/elm.git",
"license": "BSD3",
"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
```

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,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments