Created
March 5, 2015 00:14
-
-
Save btc/bfbdcb4ea6a9403cf63e to your computer and use it in GitHub Desktop.
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
import Data.Ord() | |
import Data.List.Ordered (member) | |
import Data.List (sort) | |
main :: IO() | |
main = do | |
let limit = 28123 | |
let sumsOfTwoAbundantNumbers = sort $ pairwiseSums $ takeWhile (<limit) abundantNumbers | |
let notASum = not . flip member sumsOfTwoAbundantNumbers | |
-- TODO(btc): will |notASum| be recomputed for every element in [1..limit]? | |
print $ sum $ filter notASum [1..limit] | |
pairwiseSums :: [Int] -> [Int] | |
pairwiseSums xs = map (uncurry (+)) (unorderedPairs xs xs) | |
unorderedPairs :: Ord a => [a] -> [a] -> [(a,a)] | |
unorderedPairs as bs = [(a, b) | a <- as, b <-bs, a <= b] | |
abundantNumbers :: [Int] | |
abundantNumbers = filter isAbundant [0..] | |
isAbundant :: Integral a => a -> Bool | |
isAbundant x = x < sum (divisors x) | |
divisors :: Integral a => a -> [a] | |
divisors x = 1 : filter (\n -> x `mod` n == 0) [2..x `div` 2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment