From 51bde9da48ba3cfe3b94d3d0d1e9cca1b348a340 Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Wed, 20 Nov 2024 19:38:40 +0100 Subject: [PATCH] Improve Haskell code for a few problems --- Haskell/ProjectEuler.hs | 6 +----- Haskell/p008.hs | 2 +- Haskell/p012.hs | 1 - Haskell/p019.hs | 2 +- Haskell/p023.hs | 2 +- Haskell/p024.hs | 2 +- Haskell/p029.hs | 2 +- Haskell/p052.hs | 2 +- Haskell/p054.hs | 7 +++---- 9 files changed, 10 insertions(+), 16 deletions(-) diff --git a/Haskell/ProjectEuler.hs b/Haskell/ProjectEuler.hs index 1ad7517..ada821b 100644 --- a/Haskell/ProjectEuler.hs +++ b/Haskell/ProjectEuler.hs @@ -14,19 +14,16 @@ isPrime 1 = False isPrime 2 = True isPrime 3 = True isPrime n = - odd n && n `mod` 3 /= 0 && null [ x | x <- candidates, n `mod` x == 0 || n `mod` (x+2) == 0 ] + n > 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 - digitSum :: (Integral a, Show a) => a -> Int 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..floor $ sqrt $ fromIntegral n], let y = n `div` x, n `mod` x == 0 ] + 1 - countDivisors :: (Integral a) => a -> Int countDivisors n = length $ nub $ concat [ [x, n `div` x] | x <- [1..limit], n `mod` x == 0 ] where limit = floor $ sqrt $ fromIntegral n @@ -35,4 +32,3 @@ isPandigital :: Integer -> Bool isPandigital n = n_length == length (nub n_char) && '0' `notElem` n_char && digitToInt (maximum n_char) == n_length where n_char = show n n_length = length n_char - diff --git a/Haskell/p008.hs b/Haskell/p008.hs index 5050e36..4f92aec 100644 --- a/Haskell/p008.hs +++ b/Haskell/p008.hs @@ -23,7 +23,7 @@ -- -- Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? -import Data.Char +import Data.Char (digitToInt) nDigitProduct :: Int -> String -> Int nDigitProduct n s diff --git a/Haskell/p012.hs b/Haskell/p012.hs index 651966a..43522f9 100644 --- a/Haskell/p012.hs +++ b/Haskell/p012.hs @@ -18,7 +18,6 @@ -- What is the value of the first triangle number to have over five hundred divisors? import Data.List (nub) - import ProjectEuler (countDivisors) triangNumbers :: [Int] diff --git a/Haskell/p019.hs b/Haskell/p019.hs index ec585ff..f6a63e7 100644 --- a/Haskell/p019.hs +++ b/Haskell/p019.hs @@ -11,7 +11,7 @@ -- -- How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? -import Data.Time.Calendar +import Data.Time.Calendar (Day, DayOfWeek(Sunday), dayOfWeek, fromGregorian) countSundaysFirst :: Day -> Day -> Int countSundaysFirst start end = let days = [start .. end] diff --git a/Haskell/p023.hs b/Haskell/p023.hs index 67e64e9..27b578c 100644 --- a/Haskell/p023.hs +++ b/Haskell/p023.hs @@ -9,7 +9,7 @@ -- as the sum of two abundant numbers is less than this limit. -- -- Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers. -import Data.List +import Data.List ((\\)) import qualified Data.Set as Set import ProjectEuler (sumProperDivisors) diff --git a/Haskell/p024.hs b/Haskell/p024.hs index c69cf0f..c52c14e 100644 --- a/Haskell/p024.hs +++ b/Haskell/p024.hs @@ -5,7 +5,7 @@ -- -- What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? -import Data.List +import Data.List (sort, permutations) main = do let result = sort (permutations "0123456789") !! 999999 diff --git a/Haskell/p029.hs b/Haskell/p029.hs index 82a23bc..6a9484f 100644 --- a/Haskell/p029.hs +++ b/Haskell/p029.hs @@ -10,7 +10,7 @@ -- 4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125 -- -- How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100? -import Data.List +import Data.List (nub) powerCombinations :: (Integral a) => a -> [a] powerCombinations n = nub [ x^y | x <- [2..n], y <- [2..n] ] diff --git a/Haskell/p052.hs b/Haskell/p052.hs index 42efda3..63375a6 100644 --- a/Haskell/p052.hs +++ b/Haskell/p052.hs @@ -1,7 +1,7 @@ -- It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order. -- -- Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits. -import Data.List +import Data.List (sort) smallestPermutedMultiples :: Integer smallestPermutedMultiples = head [ x | x <- [1..], sort (show x) == sort (show (2 * x)), sort (show x) == sort (show (3 * x)), sort (show x) == sort (show (4 * x)), sort (show x) == sort (show (5 * x)), sort (show x) == sort (show (6 * x)) ] diff --git a/Haskell/p054.hs b/Haskell/p054.hs index 0ac5f92..3e1e36a 100644 --- a/Haskell/p054.hs +++ b/Haskell/p054.hs @@ -44,10 +44,9 @@ -- -- How many hands does Player 1 win? -import Data.List -import Data.Function -import Data.Maybe -import System.IO +import Data.List (sort, sortBy, groupBy, minimumBy, maximumBy) +import Data.Function (on) +import Data.Maybe (fromJust) data Value = Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King | Ace deriving (Eq, Ord, Show, Bounded, Enum)