Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Problems 1-6,8-9

  • Loading branch information...
commit 92284cad65afe741be398cb065f7ccfd7f67c6c0 1 parent 005ac64
Eric Wilson authored
View
3  001.hs
@@ -0,0 +1,3 @@
+euler1 :: Int -> Int
+euler1 = sum . mult35
+ where mult35 n = [ x | x <- [1..n-1], mod x 3 == 0 || mod x 5 == 0]
View
9 002.hs
@@ -0,0 +1,9 @@
+fib :: Int -> Int
+fib n = fst $ fibPair n
+ where
+ fibPair 1 = (1,1)
+ fibPair n = fibNext $ fibPair (n-1)
+ fibNext (x,y) = (y, x+y)
+
+euler2 :: Int -> Int
+euler2 n = sum $ filter even $ takeWhile (< n) [ fib k | k <- [1..] ]
View
27 003.hs
@@ -0,0 +1,27 @@
+{--
+ There must be a better way.
+ Sorry this is unreadable, and resorts to using Integer.
+--}
+
+isPrime :: Integer -> Bool
+isPrime = null . smallFactors
+
+smallFactors :: Integer -> [Integer]
+smallFactors n = takeWhile (\d -> d*d <= n) $ factors n
+ where factors n = [ d | d <- [2..n-1], mod n d == 0]
+
+smallPrimeFactors :: Integer -> [Integer]
+smallPrimeFactors = filter isPrime . smallFactors
+
+reduceByList :: Integer -> [Integer] -> Integer
+reduceByList big [] = big
+reduceByList big (div:divs) = reduceByList (reduce big div) divs
+ where reduce num div =
+ if mod num div /= 0
+ then num
+ else reduce (quot num div) div
+
+euler3 :: Integer -> Integer
+euler3 n = if reduceByList n (smallPrimeFactors n) == 1
+ then last (smallPrimeFactors n)
+ else reduceByList n (smallPrimeFactors n)
View
11 004.hs
@@ -0,0 +1,11 @@
+numToList :: Int -> [Int]
+numToList 0 = []
+numToList x = mod x 10 : numToList (quot x 10)
+
+palNum :: Int -> Bool
+palNum = isPal . numToList
+ where isPal x = (x == reverse x)
+
+euler4 :: Int -> Int -> Int
+euler4 x y = maximum $ palProds x y
+ where palProds min max = filter palNum [x*y | x <- [min..max], y <- [min..max], y >= x ]
View
9 005.hs
@@ -0,0 +1,9 @@
+findMultiple :: Int -> Int -> Int
+findMultiple currentProd next =
+ if mod currentProd next == 0
+ then currentProd
+ else currentProd * firstPrimeDivisor next
+ where firstPrimeDivisor n = head [ d | d <- [2..n], mod n d == 0 ]
+
+euler5 :: Int -> Int
+euler5 n = foldl findMultiple 1 [1..n]
View
9 006.hs
@@ -0,0 +1,9 @@
+squareOfSum :: Int -> Int
+squareOfSum n = (sumFirstNums n) ^ 2
+ where sumFirstNums n = quot (n * (n + 1)) 2
+
+sumOfSquares :: Int -> Int
+sumOfSquares n = quot (n * (n+1) * (2*n + 1)) 6
+
+euler6 :: Int -> Int
+euler6 n = squareOfSum n - sumOfSquares n
View
9 008.hs
@@ -0,0 +1,9 @@
+import Data.Char
+
+findMax :: [Int] -> Int
+findMax = head . foldl next [0,0,0,0,0]
+ where next (oldMax: old: olds) new = max oldMax (old * new * product olds) : olds ++ [new]
+
+euler8 :: [Char] -> Int
+euler8 = findMax . stringToNums
+ where stringToNums = map digitToInt
View
5 009.hs
@@ -0,0 +1,5 @@
+triplesOfSum :: Int -> [[Int]]
+triplesOfSum n = [[n-z-y,y,z] | z <- [1..quot n 2], y <- [z, z-1..1], (n - z - y)^2 + y^2 == z^2 ]
+
+euler9 :: Int -> Int
+euler9 = product . head . triplesOfSum
Please sign in to comment.
Something went wrong with that request. Please try again.