From cb781fe33f88fabe3ccc191072f9705a5ab78936 Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Sun, 10 Nov 2024 20:40:34 +0100 Subject: [PATCH] Improve code in a few Haskell solutions --- Haskell/ProjectEuler.hs | 2 +- Haskell/p001.hs | 2 +- Haskell/p003.hs | 6 +++--- Haskell/p006.hs | 2 +- Haskell/p009.hs | 2 +- Haskell/p016.hs | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Haskell/ProjectEuler.hs b/Haskell/ProjectEuler.hs index 662263a..ae7cc21 100644 --- a/Haskell/ProjectEuler.hs +++ b/Haskell/ProjectEuler.hs @@ -23,7 +23,7 @@ digitSum n = sum $ map digitToInt $ show n sumProperDivisors :: (Integral a) => a -> a -sumProperDivisors n = sum [ if x /= y then x + y else x | x <- [2..ceiling $ sqrt $ fromIntegral n], let y = n `div` x, n `mod` x == 0 ] + 1 +sumProperDivisors n = sum [ if x /= y then x + y else x | x <- [2..floor $ sqrt $ fromIntegral n], let y = n `div` x, n `mod` x == 0 ] + 1 countDivisors :: (Integral a) => a -> Int diff --git a/Haskell/p001.hs b/Haskell/p001.hs index e17768f..d1d0ee3 100644 --- a/Haskell/p001.hs +++ b/Haskell/p001.hs @@ -3,7 +3,7 @@ -- Find the sum of all the multiples of 3 or 5 below 1000. sumMultiples :: Integer -sumMultiples = sum(filter p [ n | n <- [1..999] ]) +sumMultiples = sum $ filter p [ n | n <- [1..999] ] where p n = n `mod` 3 == 0 || n `mod` 5 == 0 main = do diff --git a/Haskell/p003.hs b/Haskell/p003.hs index c8f9805..fa6846f 100644 --- a/Haskell/p003.hs +++ b/Haskell/p003.hs @@ -5,9 +5,9 @@ import ProjectEuler (isPrime) maxPrimeFactor :: Integer -> Integer maxPrimeFactor n - | isPrime n = n - | n `mod` 2 == 0 = maxPrimeFactor $ n `div` 2 - | otherwise = maxPrimeFactor $ n `div` head [i | i <- [3,5..], n `mod` i == 0 && isPrime i] + | isPrime n = n + | even n = maxPrimeFactor $ n `div` 2 + | otherwise = maxPrimeFactor $ n `div` head [i | i <- [3,5..], n `mod` i == 0 && isPrime i] main = do let result = maxPrimeFactor 600851475143 diff --git a/Haskell/p006.hs b/Haskell/p006.hs index d6cb483..6dd7f9a 100644 --- a/Haskell/p006.hs +++ b/Haskell/p006.hs @@ -10,7 +10,7 @@ -- --Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. -sumSquareDiff :: Integer -> Integer +sumSquareDiff :: (Integral a) => a -> a sumSquareDiff n = (sum [1..n]^2) - (sum $ map (^2) [1..n]) main = do diff --git a/Haskell/p009.hs b/Haskell/p009.hs index a95d0c3..d3e92c7 100644 --- a/Haskell/p009.hs +++ b/Haskell/p009.hs @@ -9,7 +9,7 @@ -- Find the product abc. pythagoreanTriplet :: (Integral a) => a -> (a, a, a) -pythagoreanTriplet n = head [ (x, y, z) | x <- [1..n], y <- [1..n], z <- [1..n], x + y + z == n, x^2 + y^2 == z^2] +pythagoreanTriplet n = head [ (x, y, z) | x <- [1..n], y <- [x..n], z <- [y..n], x + y + z == n, x^2 + y^2 == z^2] prodTriplet :: (Integral a) => (a, a, a) -> a prodTriplet (x, y, z) = x * y * z diff --git a/Haskell/p016.hs b/Haskell/p016.hs index 96b6219..13698ae 100644 --- a/Haskell/p016.hs +++ b/Haskell/p016.hs @@ -6,5 +6,5 @@ import ProjectEuler (digitSum) main = do let result = digitSum $ 2 ^ 1000 - putStrLn $ "Project Euler, Problem 10\n" + putStrLn $ "Project Euler, Problem 16\n" ++ "Answer: " ++ (show result)