From 6bee470b8ae70775a9efbf4d87f0f45a6afc8f29 Mon Sep 17 00:00:00 2001 From: Bill Ewanick Date: Thu, 28 Sep 2023 16:13:48 -0400 Subject: [PATCH] Add questions 1-3 --- .vscode/settings.json | 2 ++ src/projectEuler/question1.hs | 28 ++++++++++++++++++++++++++++ src/projectEuler/question2.hs | 16 ++++++++++++++++ src/projectEuler/question3.hs | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 src/projectEuler/question1.hs create mode 100644 src/projectEuler/question2.hs create mode 100644 src/projectEuler/question3.hs diff --git a/.vscode/settings.json b/.vscode/settings.json index 9700597..74f9a48 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,6 +4,8 @@ "concat", "coprime", "elems", + "Factorisation", + "factorise", "foldl" ] } \ No newline at end of file diff --git a/src/projectEuler/question1.hs b/src/projectEuler/question1.hs new file mode 100644 index 0000000..3b25706 --- /dev/null +++ b/src/projectEuler/question1.hs @@ -0,0 +1,28 @@ +{- +https://projecteuler.net/problem=1 +If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. +The sum of these multiples is 23. + +Find the sum of all the multiples of 3 or 5 below 1000. +-} +module Main where + +main :: IO () +main = print ans + +ans :: Integer +ans = sum $ allNumbersUnder 1000 + +isMultipleOf :: Integral a => a -> a -> Bool +isMultipleOf n x = x `rem` n == 0 + +all3sUnder :: Integral a => a -> [a] +all3sUnder n = filter (isMultipleOf 3) [1..n-1] +all5sUnder :: Integral a => a -> [a] +all5sUnder n = filter (isMultipleOf 5) [1..n-1] + +allXsUnderN :: Integral a => a -> a -> [a] +allXsUnderN x n = filter (isMultipleOf x) [1..n-1] + +allNumbersUnder :: Integral a => a -> [a] +allNumbersUnder l = allXsUnderN 3 l <> allXsUnderN 5 l diff --git a/src/projectEuler/question2.hs b/src/projectEuler/question2.hs new file mode 100644 index 0000000..92016bf --- /dev/null +++ b/src/projectEuler/question2.hs @@ -0,0 +1,16 @@ +{- +https://projecteuler.net/problem=2 +Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: + +1, 2, 3, 5, 8, 13, 21, 34, 55, 89,... + +By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. +-} +module Main where + +main :: IO () +main = print ans + +ans = sum $ filter even $ takeWhile(< 4_000_000) fibs + +fibs = 1 : 2 : zipWith (+) fibs (tail fibs) diff --git a/src/projectEuler/question3.hs b/src/projectEuler/question3.hs new file mode 100644 index 0000000..a27997c --- /dev/null +++ b/src/projectEuler/question3.hs @@ -0,0 +1,34 @@ +{- +https://projecteuler.net/problem=3 + +The prime factors of 13195 are 5, 7, 13, and 29. + +What is the largest prime factor of the number 600851475143? +-} +module Main where + +import Math.NumberTheory.Primes (Prime (unPrime), + UniqueFactorisation (factorise), + primes) + +main :: IO () +main = print ans + +ans :: Int +ans = last $ filter (isFactor bigNum) primes' + +primes' :: [Int] +primes' = takeWhile (< sqrt') $ map unPrime primes + where + sqrt' = floor $ sqrt (toEnum bigNum) + +bigNum :: Int +bigNum = 600851475143 + +isFactor :: Int -> Int -> Bool +isFactor x n = x `rem` n == 0 + +ans' :: [Int] +ans' = map (unPrime . fst) $ factorise (fromEnum bigNum) +-- [(Prime 71,1),(Prime 839,1),(Prime 1471,1),(Prime 6857,1)] +-- [71,839,1471,6857]