From 27a7e873c16ace9ed468cb63785f9689e928a716 Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Tue, 12 Nov 2024 18:25:14 +0100 Subject: [PATCH] Code style improvement for Haskell solutions --- Haskell/ProjectEuler.hs | 2 +- Haskell/p001.hs | 4 ++-- Haskell/p002.hs | 2 +- Haskell/p003.hs | 2 +- Haskell/p004.hs | 4 ++-- Haskell/p005.hs | 2 +- Haskell/p006.hs | 4 ++-- Haskell/p007.hs | 2 +- Haskell/p009.hs | 2 +- Haskell/p010.hs | 4 ++-- Haskell/p012.hs | 2 +- Haskell/p013.hs | 2 +- Haskell/p014.hs | 6 +++--- Haskell/p016.hs | 2 +- Haskell/p020.hs | 2 +- Haskell/p021.hs | 4 ++-- Haskell/p023.hs | 2 +- Haskell/p025.hs | 6 +++--- 18 files changed, 27 insertions(+), 27 deletions(-) diff --git a/Haskell/ProjectEuler.hs b/Haskell/ProjectEuler.hs index ae7cc21..5b5864e 100644 --- a/Haskell/ProjectEuler.hs +++ b/Haskell/ProjectEuler.hs @@ -13,7 +13,7 @@ isPrime 1 = False isPrime 2 = True isPrime 3 = True isPrime n = - n `mod` 2 /= 0 && n `mod` 3 /= 0 && null [ x | x <- candidates, n `mod` x == 0 || n `mod` (x+2) == 0 ] + odd n && n `mod` 3 /= 0 && null [ x | x <- candidates, n `mod` x == 0 || n `mod` (x+2) == 0 ] where candidates = [5,11..limit] limit = floor(sqrt(fromIntegral n)) + 1 diff --git a/Haskell/p001.hs b/Haskell/p001.hs index d1d0ee3..7e09712 100644 --- a/Haskell/p001.hs +++ b/Haskell/p001.hs @@ -3,10 +3,10 @@ -- 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 [1..999] where p n = n `mod` 3 == 0 || n `mod` 5 == 0 main = do let result = sumMultiples putStrLn $ "Project Euler, Problem 1\n" - ++ "Answer: " ++ (show result) + ++ "Answer: " ++ show result diff --git a/Haskell/p002.hs b/Haskell/p002.hs index 89a4c5c..6fb8531 100644 --- a/Haskell/p002.hs +++ b/Haskell/p002.hs @@ -15,4 +15,4 @@ sumEvenFib = sum $ filter even $ takeWhile (<=4000000) (map fib [0..]) main = do let result = sumEvenFib putStrLn $ "Project Euler, Problem 2\n" - ++ "Answer: " ++ (show result) + ++ "Answer: " ++ show result diff --git a/Haskell/p003.hs b/Haskell/p003.hs index fa6846f..13c574f 100644 --- a/Haskell/p003.hs +++ b/Haskell/p003.hs @@ -12,4 +12,4 @@ maxPrimeFactor n main = do let result = maxPrimeFactor 600851475143 putStrLn $ "Project Euler, Problem 3\n" - ++ "Answer: " ++ (show result) + ++ "Answer: " ++ show result diff --git a/Haskell/p004.hs b/Haskell/p004.hs index 3040842..c0a0e8e 100644 --- a/Haskell/p004.hs +++ b/Haskell/p004.hs @@ -3,7 +3,7 @@ -- Find the largest palindrome made from the product of two 3-digit numbers. isPalindrome :: Integer -> Bool -isPalindrome n = show n == (reverse $ show n) +isPalindrome n = show n == reverse (show n) maxPalindrome :: Integer maxPalindrome = @@ -12,4 +12,4 @@ maxPalindrome = main = do let result = maxPalindrome putStrLn $ "Project Euler, Problem 4\n" - ++ "Answer: " ++ (show result) + ++ "Answer: " ++ show result diff --git a/Haskell/p005.hs b/Haskell/p005.hs index d74decc..11c346f 100644 --- a/Haskell/p005.hs +++ b/Haskell/p005.hs @@ -10,4 +10,4 @@ lcmm values main = do let result = lcmm [1..20] putStrLn $ "Project Euler, Problem 5\n" - ++ "Answer: " ++ (show result) + ++ "Answer: " ++ show result diff --git a/Haskell/p006.hs b/Haskell/p006.hs index 6dd7f9a..a762445 100644 --- a/Haskell/p006.hs +++ b/Haskell/p006.hs @@ -11,9 +11,9 @@ --Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. sumSquareDiff :: (Integral a) => a -> a -sumSquareDiff n = (sum [1..n]^2) - (sum $ map (^2) [1..n]) +sumSquareDiff n = (sum [1..n]^2) - sum (map (^2) [1..n]) main = do let result = sumSquareDiff 100 putStrLn $ "Project Euler, Problem 6\n" - ++ "Answer: " ++ (show result) + ++ "Answer: " ++ show result diff --git a/Haskell/p007.hs b/Haskell/p007.hs index a1ea5ed..7e025ab 100644 --- a/Haskell/p007.hs +++ b/Haskell/p007.hs @@ -10,4 +10,4 @@ nthPrime n = last $ take n [ x | x <- [1..], isPrime x ] main = do let result = nthPrime 10001 putStrLn $ "Project Euler, Problem 7\n" - ++ "Answer: " ++ (show result) + ++ "Answer: " ++ show result diff --git a/Haskell/p009.hs b/Haskell/p009.hs index d3e92c7..eea6997 100644 --- a/Haskell/p009.hs +++ b/Haskell/p009.hs @@ -17,4 +17,4 @@ prodTriplet (x, y, z) = x * y * z main = do let result = prodTriplet $ pythagoreanTriplet 1000 putStrLn $ "Project Euler, Problem 9\n" - ++ "Answer: " ++ (show result) + ++ "Answer: " ++ show result diff --git a/Haskell/p010.hs b/Haskell/p010.hs index 06e4f8f..42109ac 100644 --- a/Haskell/p010.hs +++ b/Haskell/p010.hs @@ -5,9 +5,9 @@ import ProjectEuler (isPrime) sumPrimes :: (Integral a) => a -> a -sumPrimes n = sum [ x | x <- [1..n], isPrime(x) ] +sumPrimes n = sum [ x | x <- [1..n], isPrime x ] main = do let result = sumPrimes 2000000 putStrLn $ "Project Euler, Problem 10\n" - ++ "Answer: " ++ (show result) + ++ "Answer: " ++ show result diff --git a/Haskell/p012.hs b/Haskell/p012.hs index 780adf1..651966a 100644 --- a/Haskell/p012.hs +++ b/Haskell/p012.hs @@ -30,4 +30,4 @@ triang500 = head [ x | x <- triangNumbers, countDivisors x > 500 ] main = do let result = triang500 putStrLn $ "Project Euler, Problem 12\n" - ++ "Answer: " ++ (show result) + ++ "Answer: " ++ show result diff --git a/Haskell/p013.hs b/Haskell/p013.hs index 5740529..4f05a5c 100644 --- a/Haskell/p013.hs +++ b/Haskell/p013.hs @@ -209,4 +209,4 @@ main = do ] putStrLn $ "Project Euler, Problem 13\n" - ++ "Answer: " ++ (show result) + ++ "Answer: " ++ show result diff --git a/Haskell/p014.hs b/Haskell/p014.hs index d810b45..02f1956 100644 --- a/Haskell/p014.hs +++ b/Haskell/p014.hs @@ -17,8 +17,8 @@ collatz :: (Integral a) => a -> [a] collatz 1 = [1] collatz n - | even n = n:(collatz $ n `div` 2) - | odd n = n:(collatz $ 3 * n + 1) + | even n = n:collatz (n `div` 2) + | odd n = n:collatz (3 * n + 1) maxCollatzLength :: Int -> Int maxCollatzLength n = snd $ maximum $ zip [ length (collatz x) | x <- [1..n-1] ] [1..n-1] @@ -26,4 +26,4 @@ maxCollatzLength n = snd $ maximum $ zip [ length (collatz x) | x <- [1..n-1] ] main = do let result = maxCollatzLength 1000000 putStrLn $ "Project Euler, Problem 14\n" - ++ "Answer: " ++ (show result) + ++ "Answer: " ++ show result diff --git a/Haskell/p016.hs b/Haskell/p016.hs index 13698ae..7a5a9e5 100644 --- a/Haskell/p016.hs +++ b/Haskell/p016.hs @@ -7,4 +7,4 @@ import ProjectEuler (digitSum) main = do let result = digitSum $ 2 ^ 1000 putStrLn $ "Project Euler, Problem 16\n" - ++ "Answer: " ++ (show result) + ++ "Answer: " ++ show result diff --git a/Haskell/p020.hs b/Haskell/p020.hs index b7bcccc..4deccee 100644 --- a/Haskell/p020.hs +++ b/Haskell/p020.hs @@ -14,4 +14,4 @@ factorial n = n * factorial (n - 1) main = do let result = digitSum $ factorial 100 putStrLn $ "Project Euler, Problem 20\n" - ++ "Answer: " ++ (show result) + ++ "Answer: " ++ show result diff --git a/Haskell/p021.hs b/Haskell/p021.hs index 23360c4..e29e261 100644 --- a/Haskell/p021.hs +++ b/Haskell/p021.hs @@ -12,7 +12,7 @@ properDivisors :: (Integral a) => a -> [a] properDivisors n = [ x | x <- [1..n-1], n `mod` x == 0] amicable :: (Integral a) => a -> a -> Bool -amicable x y = x /= y && (sumProperDivisors x) == y && (sumProperDivisors y) == x +amicable x y = x /= y && sumProperDivisors x == y && sumProperDivisors y == x sumAmicable :: (Integral a) => a -> a sumAmicable n = sum [ x | x <- [1..n-1], amicable x $ sumProperDivisors x ] @@ -20,4 +20,4 @@ sumAmicable n = sum [ x | x <- [1..n-1], amicable x $ sumProperDivisors x ] main = do let result = sumAmicable 10000 putStrLn $ "Project Euler, Problem 21\n" - ++ "Answer: " ++ (show result) + ++ "Answer: " ++ show result diff --git a/Haskell/p023.hs b/Haskell/p023.hs index c5ffb72..44bb637 100644 --- a/Haskell/p023.hs +++ b/Haskell/p023.hs @@ -26,4 +26,4 @@ sumNotAbundant = sum $ [1..28123] \\ abundantSums main = do let result = sumNotAbundant putStrLn $ "Project Euler, Problem 23\n" - ++ "Answer: " ++ (show result) + ++ "Answer: " ++ show result diff --git a/Haskell/p025.hs b/Haskell/p025.hs index 63d1c19..bed31e8 100644 --- a/Haskell/p025.hs +++ b/Haskell/p025.hs @@ -20,11 +20,11 @@ -- What is the index of the first term in the Fibonacci sequence to contain 1000 digits? fibs :: [Integer] -fibs = 0:1:(zipWith (+) fibs (tail fibs)) +fibs = 0:1:zipWith (+) fibs (tail fibs) -thousandDigitFib = length $ takeWhile (\x -> (length $ show x) < 1000) $ fibs +thousandDigitFib = length $ takeWhile (\x -> length (show x) < 1000) fibs main = do let result = thousandDigitFib putStrLn $ "Project Euler, Problem 25\n" - ++ "Answer: " ++ (show result) + ++ "Answer: " ++ show result