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

## to Difference Of Squares in the Haskell Track

Published at Aug 16 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution 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.

## Getting Started

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

## Running the tests

To run the test suite, execute the following command:

``````stack test
``````

#### If you get an error message like this...

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

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

#### Otherwise, if you get an error message like this...

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

## Running GHCi

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

``````stack ghci
``````

## Feedback, Issues, Pull Requests

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!

## 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.hs

``````{-# 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' (+)``````