one more solution plus a better version of 42 that uses the io monad …

`…correctly`
Evan Klitzke committed Apr 26, 2008
1 parent d1d9bff commit 05989d8e8bb37e55a188153d9697fb9eb33e965f
Showing with 130 additions and 6 deletions.
1. +13 −5 42/EulerProblem.hs
2. +42 −1 54/EulerProblem.hs
3. +25 −0 55/EulerProblem.hs
4. +11 −0 56/EulerProblem.hs
5. +39 −0 57/EulerProblem.hs
 @@ -3,6 +3,7 @@ module Main where import Data.Char import Data.Set hiding (map) import Foreign +import Control.Monad triangles = fromList [(n * (n + 1)) `div` 2 | n <- [1..500]] @@ -11,11 +12,18 @@ ordOffset = (ord 'A') - 1 score :: String -> Int score s = sum [(ord c) - ordOffset | c <- s] -wordsTxt :: [String] -wordsTxt = read ("[" ++ (unsafePerformIO (readFile "words.txt")) ++ "]") +wordsTxt :: IO [String] +wordsTxt = do words <- readFile "words.txt" + let words' = "[" ++ words ++ "]" + return \$ read words' -wordScores = map score wordsTxt -answer = length [w | w <- wordScores, w `member` triangles] +wordScores :: IO [Int] +wordScores = do words <- wordsTxt + return \$ map score words +answer :: IO Int +answer = do scores <- wordScores + return \$ length [w | w <- scores, w `member` triangles] -main = print answer +main = do a <- answer + print a
 @@ -0,0 +1,25 @@ +module Main where + +palindrome :: Eq a => [a] -> Bool +palindrome [] = True +palindrome [_] = True +palindrome (x:xs) = (x == (last xs)) && (palindrome (init xs)) + +isNumPalindrome :: (Show a, Integral a) => a -> Bool +isNumPalindrome = palindrome . show + +reverseNum :: (Show a, Integral a, Read a) => a -> a +reverseNum = read . reverse . show + +--reverseAndAdd :: Integral a => a -> a +reverseAndAdd n = n + (reverseNum n) + +--lyrchel :: Integral a => a -> Bool +lyrchel n = lyrchel_ 50 n + where + lyrchel_ 0 _ = True + lyrchel_ c n = if isNumPalindrome raa then False else lyrchel_ (c-1) raa + where + raa = reverseAndAdd n + +main = print \$ length [x | x <- [1..9999], lyrchel x]
 @@ -0,0 +1,11 @@ +module Main where + +import Data.Char + +digitalSum :: Integral a => a -> Int +digitalSum = sum . map digitToInt . show + +powers :: Integral a => [a] +powers = [a^b | a <- [1..99], b <- [1..99]] + +main = print \$ maximum \$ map digitalSum powers
 @@ -0,0 +1,39 @@ +data Fraction = Fract Int Int + +num :: Fraction -> Int +num Frac n d = n + +denom :: Fraction -> Int +denom Frac n d = d + +-- Remove the first element in a list matching a pattern +removeFirst :: Eq a => a -> [a] -> [a] +removeFirst _ [] = [] +removeFirst t (x:xs) = if x == t then xs else x : (removeFirst t xs) + +reduce :: Fraction -> Fraction +reduce Frac n d + +--isReduced :: Fraction -> Bool + +fracDiv :: Int -> Fraction -> Fraction +fracDiv x (Frac n d) = Frac ( + + +multisetDifference :: Eq a => [a] -> [a] -> [a] +multisetDifference as [] = as +multisetDifference [] _ = [] +multisetDifference (a:as) bs + | a `elem` bs = multisetDifference as (removeFirst a bs) + | otherwise = a : (multisetDifference as bs) + +-- reduces a fraction. for example, reduceFrac 2 4 = (1, 2) +--reduceFrac :: Integer -> Integer -> (Integer, Integer) +reduceFrac a b = (product as', product bs') + where + as = factor a + bs = factor b + as' = multisetDifference as bs + bs' = multisetDifference bs as + +fracDiv ::