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

Published at Aug 16 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.

For installation and learning resources, refer to the exercism help page.

To run the test suite, execute the following command:

```
stack test
```

```
No .cabal file found in directory
```

You are probably running an old stack version and need to upgrade it.

```
No compiler found, expected minor version match with...
Try running "stack setup" to install the correct GHC...
```

Just do as it says and it will download and install the correct compiler version:

```
stack setup
```

If you want to play with your solution in GHCi, just run the command:

```
stack ghci
```

The exercism/haskell repository on GitHub is the home for all of the Haskell exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!

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.

```
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)
import Squares (difference, squareOfSums, sumOfSquares)
main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs
specs :: Spec
specs = do
describe "squareOfSums" $ do
it "square of sum 1" $ squareOfSums 1 `shouldBe` 1
it "square of sum 5" $ squareOfSums 5 `shouldBe` 225
it "square of sum 100" $ squareOfSums 100 `shouldBe` 25502500
describe "sumOfSquares" $ do
it "sum of squares 1" $ sumOfSquares 1 `shouldBe` 1
it "sum of squares 5" $ sumOfSquares 5 `shouldBe` 55
it "sum of squares 100" $ sumOfSquares 100 `shouldBe` 338350
describe "differenceOfSquares" $ do
it "difference of squares 1" $ difference 1 `shouldBe` 0
it "difference of squares 5" $ difference 5 `shouldBe` 170
it "difference of squares 100" $ difference 100 `shouldBe` 25164150
-- Track-specific tests.
describe "Integral tests" $ do
describe "squareOfSums" $ do
it "squareOfSums (6 :: Int)" $
squareOfSums (6 :: Int)
`shouldBe` (441 :: Int)
it "squareOfSums (7 :: Integer)" $
squareOfSums (7 :: Integer)
`shouldBe` (784 :: Integer)
describe "sumOfSquares" $ do
it "sumOfSquares (8 :: Int)" $
sumOfSquares (8 :: Int)
`shouldBe` (204 :: Int)
it "sumOfSquares (9 :: Integer)" $
sumOfSquares (9 :: Integer)
`shouldBe` (285 :: Integer)
describe "difference" $ do
it "difference (11 :: Int)" $
difference (11 :: Int)
`shouldBe` (3850 :: Int)
it "difference (12 :: Integer)" $
difference (12 :: Integer)
`shouldBe` (5434 :: Integer)
{-
describe "huge difference" $
it "difference (1234567890 :: Integer)" $
difference (1234567890 :: Integer)
`shouldBe` (580764307309260838625720836817589660 :: Integer)
-}
```

```
module Squares (difference, squareOfSums, sumOfSquares) where
import Data.List (foldl1')
difference :: Integral a => a -> a
difference n = subtract (sumOfSquares n) (squareOfSums n)
squareOfSums :: Integral a => a -> a
squareOfSums n = (sum' [1..n])^2
sumOfSquares :: Integral a => a -> a
sumOfSquares n = sum' $ map (^2) [1..n]
-- quicker than sum
sum' :: Integral a => [a] -> a
sum' = foldl1' (+)
```

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

## Community comments