diff --git a/src/projectEuler/question13.hs b/src/projectEuler/question13.hs index 7418d14..eb4c463 100644 --- a/src/projectEuler/question13.hs +++ b/src/projectEuler/question13.hs @@ -221,4 +221,6 @@ ans :: [Integer] ans = take 10 $ reverse . digits $ sum oneHundredFiftyDigitNumbers main :: IO () -main = print ans +main = do + print ans + print (sum oneHundredFiftyDigitNumbers) diff --git a/src/projectEuler/question5.hs b/src/projectEuler/question5.hs index 8f5af4f..cde8b7b 100644 --- a/src/projectEuler/question5.hs +++ b/src/projectEuler/question5.hs @@ -63,3 +63,5 @@ test = map (filter . multipleOf) [1..10] -- lol solve = foldl1 lcm [1..20] + +main = print solve diff --git a/src/projectEuler/question6.hs b/src/projectEuler/question6.hs index deb48e4..3939a0c 100644 --- a/src/projectEuler/question6.hs +++ b/src/projectEuler/question6.hs @@ -13,16 +13,29 @@ Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. -} -upperRange = 100 +upperRange :: Integer +upperRange = 1_000_000 + +square :: Num a => a -> a square n = n^2 + +squares :: [Integer] squares = map square [1..upperRange] + +sum' :: [Integer] -> Integer sum' = go 0 where go acc [] = acc go acc (x:xs) = go (acc+x) xs -sumOfSquares = sum' squares +sumOfSquares :: Integer +sumOfSquares = sum squares -squareOfTheSum = (sum [1..upperRange])^2 +squareOfTheSum :: Integer +squareOfTheSum = sum [1..upperRange] ^2 +solution :: Integer solution = squareOfTheSum - sumOfSquares + +main :: IO () +main = print solution diff --git a/src/projectEuler/question7.hs b/src/projectEuler/question7.hs index 975242f..ff08ef5 100644 --- a/src/projectEuler/question7.hs +++ b/src/projectEuler/question7.hs @@ -1,6 +1,9 @@ -- https://projecteuler.net/problem=7 -- Find the 10_001 prime number +-- import Data.Numbers.Primes (primes) +import Math.NumberTheory.Primes (primes) + primes1 :: [Integer] primes1 = 2:3:prs where @@ -30,3 +33,8 @@ primes2 = 2:([3..] `minus` composites) primes3 = sieve [2..] where sieve (p : xs) = p : sieve [x | x <- xs, x `mod` p > 0] + +main :: IO () +main = print $ "Arithmoi - Math.NumberTheory.Primes: " <> show ans + +ans = primes !! 10_000_000 diff --git a/src/projectEuler/question8.hs b/src/projectEuler/question8.hs index be09d61..c2d45ed 100644 --- a/src/projectEuler/question8.hs +++ b/src/projectEuler/question8.hs @@ -28,6 +28,7 @@ What is the value of this product? module Main where import Data.Char (ord) +import Data.Foldable (maximumBy) import Data.List (sort) import Data.List.Split (splitOn) @@ -46,9 +47,16 @@ digits = go [] main :: IO () main = print ans -ans :: [[Integer]] -ans = (windowsOf 13 . digits) thousandDigitNum --- "9878799272442" +ans :: ([Integer], Integer) +ans = (l, p) + where + l = maximumBy c $ (windowsOf 13 . digits) thousandDigitNum + p = product l + c a b + | product a > product b = GT + | product a < product b = LT + | otherwise = EQ +-- "([5,5,7,6,6,8,9,6,6,4,8,9,5],23514624000)" -- what dose it mean to have the greatest product in adjacent digits? -- why ask it like that??? diff --git a/src/projectEuler/question9.hs b/src/projectEuler/question9.hs index 68e02e1..ca61345 100644 --- a/src/projectEuler/question9.hs +++ b/src/projectEuler/question9.hs @@ -17,14 +17,39 @@ Find the product `abc`. -} main :: IO () -main = print answer +main = do + print answer + print (product answer) -answer :: String -answer = "I dunno" +-- head $ [(a,b,c) | a <- [1..limit], b <- [a+1..limit], c <- [limit - a - b], a < b, b < c, a^2 + b^2 == c^2] +answer :: [Integer] +answer = head $ + [ [a, b, c] | + a <- [1 .. limit], + b <- [a + 1 .. limit], + c <- [limit - a - b], + b < c, + a ^ 2 + b ^ 2 == c ^ 2] + where limit = 1000 + +limit = 1000 +version1 = [ [a, b, c] | + a <- [1 .. limit], + b <- [a + 1 .. limit], + c <- [limit - a - b], + b < c, + a ^ 2 + b ^ 2 == c ^ 2] + +version2 = [ [a, b, c] | + a <- [1 .. limit], + b <- [a + 1 .. limit], + c <- [limit - a - b], + b < c, + a ^ 2 + b ^ 2 == c ^ 2] solve :: Integer -> [(Integer, Integer, Integer)] -solve x = takeWhile (\(a,b,c) -> a + b + c <= 1000) $ primitiveTriplesUnder x +solve x = takeWhile (\(a,b,c) -> a + b + c == 1000) $ primitiveTriplesUnder x euclid'sFormula :: Num c => (c, c) -> (c, c, c) euclid'sFormula (m, n) = (a,b,c) @@ -87,19 +112,19 @@ ans' limit = [(a, b, c) , b < c ] -{- +{- - Solution to Project Euler problem 9 - Copyright (c) Project Nayuki. All rights reserved. - - + - - https://www.nayuki.io/page/project-euler-solutions - https://github.com/nayuki/Project-Euler-solutions -} --- Computers are fast, so we can implement a brute-force search to directly solve the problem. -perim = 1000 -main = putStrLn (show ans) -ans = head [a * b * (perim - a - b) | a <- [1..perim], b <- [a+1..perim], isIntegerRightTriangle a b] -isIntegerRightTriangle a b = a < b && b < c - && a * a + b * b == c * c - where c = perim - a - b \ No newline at end of file +-- -- Computers are fast, so we can implement a brute-force search to directly solve the problem. +-- perim = 1000 +-- main = putStrLn (show ans) +-- ans = head [a * b * (perim - a - b) | a <- [1..perim], b <- [a+1..perim], isIntegerRightTriangle a b] +-- isIntegerRightTriangle a b = a < b && b < c +-- && a * a + b * b == c * c +-- where c = perim - a - b