Skip to content

Commit

Permalink
Updated Haskell solution to have (crappy but working) cli
Browse files Browse the repository at this point in the history
  • Loading branch information
John Evans committed Mar 14, 2011
1 parent 0c926c5 commit d36ef93
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 42 deletions.
109 changes: 74 additions & 35 deletions EulerMain.hs
Expand Up @@ -6,40 +6,79 @@ import System( getArgs )
-- bench 50 $ do putStr (show (length (take 500 primes)))
-- bench 50 $ do putStr (show (length (take 500 fast_primes)))

solutions :: [Maybe Integer]
solutions = [Just euler1,
Just euler2,
Just euler3,
Just euler4,
Nothing, -- 5
Just euler6,
Nothing, -- 7
Just euler8,
Just euler9,
Just euler10,
Just euler11,
Nothing, -- 12
Just euler13,
Nothing, -- 14
Nothing, -- 15
Just euler16,
Nothing, -- 17
Nothing, -- 18
Nothing, -- 19
Just euler20,
Nothing, -- 21
Nothing, -- 22
Nothing, -- 23
Nothing, -- 24
Just euler25,
Nothing, -- 26
Nothing, -- 27
Nothing, -- 28
Just euler29,
Just euler30,
Nothing, -- 31
Nothing, -- 32
Nothing, -- 33
Just euler34,
Nothing, -- 35
Just euler36,
Nothing, -- 37
Nothing, -- 38
Nothing, -- 39
Nothing, -- 40
Nothing, -- 41
Nothing, -- 42
Nothing, -- 43
Nothing, -- 44
Nothing, -- 45
Nothing, -- 46
Nothing, -- 47
Nothing, -- 48
Nothing, -- 49
Nothing, -- 50
Nothing, -- 51
Just euler52]

main = do
args <- getArgs
print $ show args
putStrLn "Project Euler results (via Haskell):"
putStrLn $ "#1: " ++ (show euler1)
putStrLn $ "#2: " ++ (show euler2)
putStrLn $ "#3: " ++ (show euler3)
putStrLn $ "#4: " ++ (show euler4)
putStrLn "#5: Too slow."
-- putStrLn $ "#5: " ++ (show euler5)
putStrLn $ "#6: " ++ (show euler6)
putStrLn "#7: Too slow."
-- putStrLn $ "#7: " ++ (show euler7)
putStrLn $ "#8: " ++ (show euler8)
putStrLn $ "#9: " ++ (show euler9)
putStrLn $ "#10: " ++ (show euler10)
putStrLn $ "#11: " ++ (show euler11)
putStrLn "#12: Too slow."
-- putStrLn $ "#12: " ++ (show euler12)
putStrLn $ "#13: " ++ (show euler13)
putStrLn "#14: Too slow."
-- putStrLn $ "#14: " ++ (show euler14)
-- putStrLn "#15: Not done yet."
putStrLn $ "#16: " ++ (show euler16)
putStrLn $ "#20: " ++ (show euler20)
putStrLn "#21: Too slow."
-- putStrLn $ "#21: " ++ (show euler21)
putStrLn $ "#25: " ++ (show euler25)
putStrLn $ "#29: " ++ (show euler29)
putStrLn $ "#30: " ++ (show euler30)
putStrLn $ "#34: " ++ (show euler34)
putStrLn "#35: Too slow."
-- putStrLn $ "#35: " ++ (show euler35)
putStrLn $ "#36: " ++ (show euler36)
putStrLn $ "#52: " ++ (show euler52)

display :: Int -> Maybe Integer -> String
display idx (Just n) = "#" ++ (show idx) ++ ": " ++ (show n)
display idx Nothing = "#" ++ (show idx) ++ ": N/A"


displayAtN :: Int -> String
displayAtN n = display n (solutions !! n)

displayNs :: [Int] -> String
displayNs = unlines . (map displayAtN)


doMain :: [String] -> IO()
doMain args = putStrLn $ if 0 >= (length args)
then displayNs [1..(length solutions)]
else displayNs (map read args)


main = do
args <- getArgs
doMain args
13 changes: 7 additions & 6 deletions ProjectEuler.hs
Expand Up @@ -230,17 +230,17 @@ nthPrime n = primes !! n
--
-- Find the greatest product of five consecutive digits in the
-- 1000-digit number.
euler8 :: Int
euler8 = maximum (productsOfNConsecutiveDigits 5 euler8_input)
euler8 :: Integer
euler8 = fromIntegral (maximum (productsOfNConsecutiveDigits 5 euler8_input))


-- From the PE forum (adapted to use my equivalent functions)... very nice.
--
-- Cheats in that it also looks at products of strings less than 5
-- when it gets near the end of the list but that doesn't *really*
-- matter for this problem.
euler8Alt :: Int
euler8Alt = maximum . map (product . take 5) . tails $ stringDigits euler8_input
euler8Alt :: Integer
euler8Alt = fromIntegral . maximum . map (product . take 5) . tails $ stringDigits euler8_input

-- The definition of stringDigits does not immediately follow because
-- it was not originally created until much later, and this function
Expand Down Expand Up @@ -311,8 +311,9 @@ euler10 = sum (takeWhile (< 2000000) primes)
--
-- What is the greatest product of four adjacent numbers in any
-- direction (up, down, left, right, or diagonally) in the 20x20 grid?
euler11 :: Int
euler11 = maximum [e11MaxColProduct, e11MaxRowProduct] --, e11_max_diag_product]
euler11 :: Integer
euler11 = fromIntegral (maximum [e11MaxColProduct, e11MaxRowProduct])
--, e11_max_diag_product]


-- Find the maximum of all the maximum products from all of the rows.
Expand Down
2 changes: 1 addition & 1 deletion README
Expand Up @@ -49,7 +49,7 @@ Reia (1) -cli
Ruby (7+) +cli
Scala (2) +cli
Scheme (7) -cli
Haskell (21+) -cli
Haskell (21+) +cli


That's 23 languages with 130+ (overlapping) solutions.
Expand Down

0 comments on commit d36ef93

Please sign in to comment.