# nicolemon's solution

## to Perfect Numbers in the Haskell Track

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

#### Note:

This exercise has changed since this solution was written.

Determine if a number is perfect, abundant, or deficient based on Nicomachus' (60 - 120 CE) classification scheme for natural numbers.

The Greek mathematician Nicomachus devised a classification scheme for natural numbers, identifying each as belonging uniquely to the categories of perfect, abundant, or deficient based on their aliquot sum. The aliquot sum is defined as the sum of the factors of a number not including the number itself. For example, the aliquot sum of 15 is (1 + 3 + 5) = 9

• Perfect: aliquot sum = number
• 6 is a perfect number because (1 + 2 + 3) = 6
• 28 is a perfect number because (1 + 2 + 4 + 7 + 14) = 28
• Abundant: aliquot sum > number
• 12 is an abundant number because (1 + 2 + 3 + 4 + 6) = 16
• 24 is an abundant number because (1 + 2 + 3 + 4 + 6 + 8 + 12) = 36
• Deficient: aliquot sum < number
• 8 is a deficient number because (1 + 2 + 4) = 7
• Prime numbers are deficient

Implement a way to determine whether a given number is perfect. Depending on your language track, you may also need to implement a way to determine whether a given number is abundant or deficient.

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

Taken from Chapter 2 of Functional Thinking by Neal Ford. http://shop.oreilly.com/product/0636920029687.do

## Submitting Incomplete Solutions

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

### Tests.hs

``````{-# LANGUAGE RecordWildCards #-}

import Data.Foldable     (for_)
import Test.Hspec        (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import PerfectNumbers

main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs

specs :: Spec
specs = describe "classify" \$ for_ cases test
where

test Case{..} = it description assertion
where
assertion = classify number `shouldBe` expected

data Case = Case { description :: String
, number      :: Int
, expected    :: Maybe Classification
}

cases :: [Case]
cases = [ Case { description = "Smallest perfect number is classified correctly"
, number      = 6
, expected    = Just Perfect
}
, Case { description = "Medium perfect number is classified correctly"
, number      = 28
, expected    = Just Perfect
}
, Case { description = "Large perfect number is classified correctly"
, number      = 33550336
, expected    = Just Perfect
}
, Case { description = "Smallest abundant number is classified correctly"
, number      = 12
, expected    = Just Abundant
}
, Case { description = "Medium abundant number is classified correctly"
, number      = 30
, expected    = Just Abundant
}
, Case { description = "Large abundant number is classified correctly"
, number      = 33550335
, expected    = Just Abundant
}
, Case { description = "Smallest prime deficient number is classified correctly"
, number      = 2
, expected    = Just Deficient
}
, Case { description = "Smallest non-prime deficient number is classified correctly"
, number      = 4
, expected    = Just Deficient
}
, Case { description = "Medium deficient number is classified correctly"
, number      = 32
, expected    = Just Deficient
}
, Case { description = "Large deficient number is classified correctly"
, number      = 33550337
, expected    = Just Deficient
}
, Case { description = "Edge case (no factors other than itself) is classified correctly"
, number      = 1
, expected    = Just Deficient
}
, Case { description = "Zero is rejected (not a natural number)"
, number      = 0
, expected    = Nothing
}
, Case { description = "Negative integer is rejected (not a natural number)"
, number      = -1
, expected    = Nothing
}
]``````
``````module PerfectNumbers (classify, Classification(..)) where

data Classification = Deficient | Perfect | Abundant deriving (Eq, Show)

classify :: Int -> Maybe Classification
classify n
| n <= 0 = Nothing
| n > aliquotSum = Just Deficient
| n == aliquotSum = Just Perfect
| n < aliquotSum = Just Abundant
| otherwise = Nothing
where aliquotSum = sum properDivisors
properDivisors = [m | m <- [1..div n 2], mod n m == 0]``````