Skip to content

Instantly share code, notes, and snippets.

@btc
Created March 5, 2015 00:14
Show Gist options
  • Save btc/bfbdcb4ea6a9403cf63e to your computer and use it in GitHub Desktop.
Save btc/bfbdcb4ea6a9403cf63e to your computer and use it in GitHub Desktop.
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