I’m in the process of familiarizing myself with Haskell (wouldn’t say it’s learning at this point), so decided to work on some Project Euler problems. Here are my solutions to problems 1-5. One thing to notice – they are unusually long for a Haskell program and as usual – take them with a grain of salt.

## Project Euler 1 in Haskell

mul35 :: Int -> Int mul35 n = sum xs where xs = [i | i <- [1..n-1], (i `mod` 3 == 0) || (i `mod` 5 == 0)] main = do print $ mul35 10 print $ mul35 1000

## Project Euler 2 in Haskell

fibs :: [Int] fibs = 1 : 2 : [a + b | (a, b) <- zip fibs (tail fibs)] sumFibs :: Int -> Int sumFibs ms = sum [x | x <- takeWhile (< ms) fibs, even x] main = do print $ sumFibs 4000000

## Project Euler 3 in Haskell

lpf :: Int -> Int lpf n = if md == n then n else lpf (n `div` md) where md = head [x | x <- [2..n], n `mod` x == 0] main = do print $ lpf 13195 print $ lpf 600851475143

## Project Euler 4 in Haskell

import Data.List (tails) numsLen :: Int -> [Int] numsLen l = [mx,mx-1..mi] where mi = 10^(l-1) mx = 10^l - 1 digits :: Int -> [Int] digits 0 = [] digits n = n `mod` 10 : digits (n `div` 10) palindrome :: Int -> Bool palindrome n = digits n == reverse (digits n) lpal :: Int -> Int lpal d = maximum ps where ns = numsLen d xss = tails ns ps = [y | x:xs <- xss, y <- map (* x) xs, palindrome y] main = do print $ lpal 2 print $ lpal 3

## Project Euler 5 in Haskell

lcmm :: [Int] -> Int lcmm [] = error "Empty list" lcmm [x] = x lcmm xs = foldr1 lcm xs hdbf :: Int -> Int hdbf n = lcmm [1..n] main = do print $ hdbf 10 print $ hdbf 20 print $ hdbf 30