Created
January 19, 2012 06:11
-
-
Save darkhelmet/1638297 to your computer and use it in GitHub Desktop.
Number classification in Haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data Classification = Perfect | Abundant | Deficient deriving (Eq, Show, Ord) | |
squareRoot :: Int -> Int | |
squareRoot = floor . sqrt . fromIntegral | |
isFactor :: Int -> Int -> Bool | |
isFactor a b = a `mod` b == 0 | |
factors :: Int -> [Int] | |
factors n = lowFactors ++ highFactors | |
where lowFactors = filter (isFactor n) [1..limit] | |
limit = squareRoot n | |
highFactors = map (n `div`) lowFactors | |
classify :: Int -> (Int, Classification) | |
classify n | |
| sumOfFactors == n' = (n, Perfect) | |
| sumOfFactors < n' = (n, Deficient) | |
| sumOfFactors > n' = (n, Abundant) | |
where n' = 2 * n | |
sumOfFactors = sum $ factors n | |
perfectNumbers = filter (\(n, c) -> c == Perfect) $ map classify [1..] |
It was originally just returning the classification, but I wanted the number too....didn't think too hard about it. It's not for anything, I was just watching a talk where he was discussing this algorithm.
Do you have a link to the talk? I'd be interested.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! Are you planning to use
classify
for something else where you need the result type to be(Int, Classification)
? If not, you could simplify the return type ofclassify
and slightly simplifyperfectNumbers
.