start on 24, and some refactoring

Eric Wilson committed Feb 16, 2011
Showing with 70 additions and 3 deletions.
1. +10 −1 014.hs
2. +20 −0 023.hs
3. +19 −0 024.hs
4. +7 −1 EulerMath/BigNum.hs
5. +14 −1 EulerMath/Divisors.hs
11 014.hs
 @@ -1,4 +1,5 @@ import qualified Data.Map as Map +import qualified Data.Set as Set import Data.Maybe loop :: (Integral a) => a -> [a] @@ -21,7 +22,15 @@ listPrevious :: (Integral a) => [a] -> [a] listPrevious [] = [] listPrevious (x:xs) = allPrevious x ++ listPrevious xs - +subtractList :: (Integral a) => Set.Set a -> [a] -> Set.Set a +subtractList xs ys = Set.difference xs (Set.fromList ys) + +removePrevious :: (Integral a) => (Set.Set a, [a]) -> (Set.Set a, [a]) +removePrevious (xs, ys) = (subtractList xs prev, filter f prev) + where + prev = listPrevious ys + f = (<= 500) + next :: (Integral a) => a -> a next n = if mod n 2 == 0
20 023.hs
 @@ -0,0 +1,20 @@ +import EulerMath.Divisors + +isAbundant :: Int -> Bool +isAbundant n = d n > n + +abundants :: [Int] +abundants = filter isAbundant [12..28123] + +sumTwoAbundants :: Int -> Bool +sumTwoAbundants = sumOfList abundants + +sumOfList :: [Int] -> Int -> Bool +sumOfList [] n = False +sumOfList (x:xs) n + | n < 2*x = False + | elem (n-x) \$ takeWhile (<= n-x) (x:xs) = True + | otherwise = sumOfList xs n + +euler23 :: Int -> Int +euler23 n = sum \$ filter (not . sumTwoAbundants) [2,4..n]
19 024.hs
 @@ -0,0 +1,19 @@ +import EulerMath.BigNum +import Data.List + +facts = map fact [10,9..1] + +digits = [0..9] + +linComb :: (Integral a) => a -> [a] -> [a] +linComb _ [] = [] +linComb n (x:xs) = quot n x : (linComb (mod n x) xs) + +--permute :: (Integral a) => [a] -> [a] -> [a] +--permute xs (y:ys) = inc xs y : (permute rem + +inc :: [Int] -> Int -> [Int] +inc xs y = newHead : (filter (/=newHead) sorted) + where + sorted = sort xs + newHead = sorted !! y
 @@ -1,11 +1,17 @@ module EulerMath.BigNum ( fact -, digitSum ) where +, digitSum +, numDigits + ) where digitSum :: (Integral a) => a -> a digitSum 0 = 0 digitSum n = mod n 10 + digitSum (quot n 10) +numDigits :: (Integral a) => a -> a +numDigits 0 = 0 +numDigits n = 1 + numDigits (quot n 10) + fact :: (Integral a) => a -> a fact 0 = 1 fact n = n * fact (n-1)
 @@ -1,12 +1,25 @@ module EulerMath.Divisors -( divisors +( d +, divisors , primeFactorization , primeFactorizationFS , primeFactors ) where import EulerMath.Primes +properDivPairs :: (Integral a) => a -> [(a,a)] +properDivPairs n = takeWhile (\(a, b) -> a <= b) [ (d, quot n d) | d <- [1..quot n 2], mod n d == 0 ] + +d :: (Integral a) => a -> a +d n = divisorSum n - n + where + divisorSum = sum . map plus' . properDivPairs + plus' (a, b) = + if a < b + then a + b + else a + divisors :: (Integral a) => a -> [a] divisors n = [ d | d <- [1..n], mod n d == 0 ]