Improve Haskell solution for Problem 23 (much faster)

This commit is contained in:
2024-11-13 22:17:42 +01:00
parent 5fa2b14842
commit 5608a19230

View File

@ -10,6 +10,7 @@
-- --
-- Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers. -- Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
import Data.List import Data.List
import qualified Data.Set as Set
import ProjectEuler (sumProperDivisors) import ProjectEuler (sumProperDivisors)
@ -17,7 +18,7 @@ isAbundant :: (Integral a) => a -> Bool
isAbundant n = sumProperDivisors n > n isAbundant n = sumProperDivisors n > n
abundantSums :: (Integral a) => [a] abundantSums :: (Integral a) => [a]
abundantSums = nub [ x + y | x <- abundantList, y <- abundantList, x + y <= 28123, y >= x ] abundantSums = Set.toList $ Set.fromList [ x + y | x <- abundantList, y <- abundantList, x + y <= 28123, y >= x ]
where abundantList = [ x | x <- [12..28123], isAbundant x ] where abundantList = [ x | x <- [12..28123], isAbundant x ]
sumNotAbundant :: (Integral a) => a sumNotAbundant :: (Integral a) => a