Avatar of atmantree

atmantree's solution

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

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

difference :: Integral a => a -> a
difference n = squareOfSums n - sumOfSquares n

squareOfSums :: Integral a => a -> a
squareOfSums n = (div (n*(n+1)) 2) ^ 2

sumOfSquares :: Integral a => a -> a
sumOfSquares n = n*(n+1)*(2*n+1) `div` 6

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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?