Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

start on 24, and some refactoring

  • Loading branch information...
commit 941a38c30ec30e44dff836e1449e18fdc5fad0c7 1 parent d4612d6
Eric Wilson authored
View
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
View
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]
View
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
View
8 EulerMath/BigNum.hs
@@ -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)
View
15 EulerMath/Divisors.hs
@@ -1,5 +1,6 @@
module EulerMath.Divisors
-( divisors
+( d
+, divisors
, primeFactorization
, primeFactorizationFS
, primeFactors
@@ -7,6 +8,18 @@ module EulerMath.Divisors
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 ]
Please sign in to comment.
Something went wrong with that request. Please try again.