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
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.
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!
Taken from Chapter 2 of Functional Thinking by Neal Ford. http://shop.oreilly.com/product/0636920029687.do
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
{-# 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)
aliquotSum :: Int -> Int
aliquotSum n = sum $ filter (\x -> mod n x == 0) [1..(n-1)]
classify :: Int -> Maybe Classification
classify n
| n < 1 = Nothing
| aliquotSum n == n = Just Perfect
| aliquotSum n > n = Just Abundant
| otherwise = Just Deficient
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.
Level up your programming skills with 3,373 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments