Solutions to the first eight Project Euler problems.

This commit is contained in:
Jesse D. McDonald 2015-08-01 03:04:26 -05:00
commit c93bc758ce
9 changed files with 67 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
*.o
*.hi
Problem[0-9]
Problem[0-9][0-9]
Problem[0-9][0-9][0-9]
!*.hs

2
Problem1.hs Normal file
View File

@ -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 ]

4
Problem2.hs Normal file
View File

@ -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

10
Problem3.hs Normal file
View File

@ -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

7
Problem4.hs Normal file
View File

@ -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

4
Problem5.hs Normal file
View File

@ -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]

3
Problem6.hs Normal file
View File

@ -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])

4
Problem7.hs Normal file
View File

@ -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

27
Problem8.hs Normal file
View File

@ -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