commit c93bc758cea618245a7776c4c14551ff2399f5fd Author: Jesse McDonald Date: Sat Aug 1 03:04:26 2015 -0500 Solutions to the first eight Project Euler problems. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..33c34b5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +*.o +*.hi +Problem[0-9] +Problem[0-9][0-9] +Problem[0-9][0-9][0-9] +!*.hs diff --git a/Problem1.hs b/Problem1.hs new file mode 100644 index 0000000..200099f --- /dev/null +++ b/Problem1.hs @@ -0,0 +1,2 @@ +-- Find the sum of all the multiples of 3 or 5 below 1000. +main = print $ sum [ n | n <- [1..999], n `mod` 3 == 0 || n `mod` 5 == 0 ] diff --git a/Problem2.hs b/Problem2.hs new file mode 100644 index 0000000..d5f73af --- /dev/null +++ b/Problem2.hs @@ -0,0 +1,4 @@ +-- By considering the terms in the Fibonacci sequence whose values do not exceed +-- four million, find the sum of the even-valued terms. +fibs = 1 : 2 : zipWith (+) fibs (tail fibs) +main = print $ sum $ filter even $ takeWhile (<= 4000000) fibs diff --git a/Problem3.hs b/Problem3.hs new file mode 100644 index 0000000..88e810f --- /dev/null +++ b/Problem3.hs @@ -0,0 +1,10 @@ +-- What is the largest prime factor of the number 600851475143 ? +n `divides` m = m `mod` n == 0 +primes = go [2..] where go (p:ps) = p : go (filter (\n -> not (p `divides` n)) ps) + +factors n = go primes n + where go (p:ps) n | n < p = [] + | p `divides` n = p : go (p:ps) (n `div` p) + | otherwise = go ps n + +main = print $ last $ factors 600851475143 diff --git a/Problem4.hs b/Problem4.hs new file mode 100644 index 0000000..9e857b2 --- /dev/null +++ b/Problem4.hs @@ -0,0 +1,7 @@ +-- Find the largest palindrome made from the product of two 3-digit numbers. +import Data.List + +-- Shortcut: 924 * 962 = 888888, a palindrome, so at least one factor must be >= 924. +products = sortBy (flip compare) [ n * m | n <- [999,998..924], m <- [999,998..100] ] +palindrome n = show n == reverse (show n) +main = print $ head $ filter palindrome products diff --git a/Problem5.hs b/Problem5.hs new file mode 100644 index 0000000..0ad61ca --- /dev/null +++ b/Problem5.hs @@ -0,0 +1,4 @@ +-- What is the smallest positive number that is evenly +-- divisible by all of the numbers from 1 to 20? +import Data.List +main = print $ foldl1' lcm [1..20] diff --git a/Problem6.hs b/Problem6.hs new file mode 100644 index 0000000..39392e8 --- /dev/null +++ b/Problem6.hs @@ -0,0 +1,3 @@ +-- Find the difference between the sum of the squares of the first +-- one hundred natural numbers and the square of the sum. +main = print $ (sum [1..100])^2 - sum (map (^2) [1..100]) diff --git a/Problem7.hs b/Problem7.hs new file mode 100644 index 0000000..76e9d3a --- /dev/null +++ b/Problem7.hs @@ -0,0 +1,4 @@ +-- What is the 10 001st prime number? +primes :: [Int] +primes = let go (p:ps) = p : go [ n | n <- ps, n `mod` p /= 0 ] in go [2..] +main = print $ primes !! 10000 diff --git a/Problem8.hs b/Problem8.hs new file mode 100644 index 0000000..fd7125c --- /dev/null +++ b/Problem8.hs @@ -0,0 +1,27 @@ +-- Find the thirteen adjacent digits in the 1000-digit number that +-- have the greatest product. What is the value of this product? +import Data.List + +number = "73167176531330624919225119674426574742355349194934" ++ + "96983520312774506326239578318016984801869478851843" ++ + "85861560789112949495459501737958331952853208805511" ++ + "12540698747158523863050715693290963295227443043557" ++ + "66896648950445244523161731856403098711121722383113" ++ + "62229893423380308135336276614282806444486645238749" ++ + "30358907296290491560440772390713810515859307960866" ++ + "70172427121883998797908792274921901699720888093776" ++ + "65727333001053367881220235421809751254540594752243" ++ + "52584907711670556013604839586446706324415722155397" ++ + "53697817977846174064955149290862569321978468622482" ++ + "83972241375657056057490261407972968652414535100474" ++ + "82166370484403199890008895243450658541227588666881" ++ + "16427171479924442928230863465674813919123162824586" ++ + "17866458359124566529476545682848912883142607690042" ++ + "24219022671055626321111109370544217506941658960408" ++ + "07198403850962455444362981230987879927244284909188" ++ + "84580156166097919133875499200524063689912560717606" ++ + "05886116467109405077541002256983155200055935729725" ++ + "71636269561882670428252483600823257530420752963450" + +main = print $ maximum $ map (product . take 13) $ take 988 $ + tails $ map (read . (:[])) number