ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

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.

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

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!

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

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

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

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