Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/SolutionsTo50.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,21 @@ solutionsTo50_ n = case n of
18 -> solution18
_ -> putStrLn "Solution not present in program"

-- | Solves Project Euler Problem 1:
-- Find the sum of all the multiples of 3 or 5 below 1000.
-- This is solved by taking the union of multiples of 3 and 5 and summing them.
solution1 :: Integer
solution1 = sum $ union [3,6..999] [5,10..999]

-- | Solves Project Euler Problem 2:
-- By considering the terms in the Fibonacci sequence whose values do not exceed four million,
-- find the sum of the even-valued terms.
solution2 :: Integer
solution2 = sum . filter even $ takeWhile (<= 4000000) fibs

-- | Solves Project Euler Problem 3:
-- Find the largest prime factor of the number 600851475143.
-- This is solved by generating prime factors and finding the maximum.
solution3 :: Integer
solution3 = let primes = 2 : filter (null . tail . primeFactors) [3,5..]
primeFactors n = factor n primes
Expand All @@ -44,27 +53,42 @@ solution3 = let primes = 2 : filter (null . tail . primeFactors) [3,5..]
| otherwise = factor n ps
in maximum $ primeFactors 600851475143

-- | Solves Project Euler Problem 4:
-- Find the largest palindrome made from the product of two 3-digit numbers.
-- This is solved by iterating through products of 3-digit numbers and checking for palindromes.
solution4 :: Integer
solution4 = maximum [x | y<-[999,998..100], z<-[999,998..y], let x = y*z, isPalindromic x]

-- | Solves Project Euler Problem 5:
-- What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
-- This is solved by finding the least common multiple (LCM) of numbers from 1 to 20.
solution5 :: Integer
solution5 = foldr1 lcm [1..20]

-- | Solves Project Euler Problem 6:
-- Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
solution6 :: Integer
solution6 = sumSquare - squareSums
where squareSums = sum $ map (^2) [1..100]
sumSquare = (^2) $ sum [1..100]

-- | Solves Project Euler Problem 8:
-- Find the thirteen adjacent digits in the 1000-digit number that have the greatest product.
solution8 :: Integer
solution8 = maximum . map product . takeWhile (\l -> length l >= 13) . map (take 13) . tails $ digitArr
where digitArr = map digitToInt digitString

-- | Solves Project Euler Problem 13:
-- Work out the first ten digits of the sum of the following one hundred 50-digit numbers
-- (stored in p13numbers.txt).
solution13 = do
numbers <- fmap ( map read . lines ) (readFile "p13numbers.txt")
putStrLn . take 10 . show . sum $ numbers



-- | Solves Project Euler Problem 18:
-- Find the maximum total from top to bottom of the triangle (stored in triangle.txt).
solution18 = do
a <- answer
putStrLn $ show a
Expand Down