Improve Haskell code for a few problems

This commit is contained in:
daniele 2024-11-22 21:43:19 +01:00
parent 91a8d44801
commit 187483b624
Signed by: fuxino
GPG Key ID: 981A2B2A3BBF5514
10 changed files with 22 additions and 19 deletions

View File

@ -1,5 +1,6 @@
module ProjectEuler module ProjectEuler
( isPrime ( isPrime
, lcmm
, digitSum , digitSum
, sumProperDivisors , sumProperDivisors
, countDivisors , countDivisors
@ -18,6 +19,11 @@ isPrime n =
where candidates = [5,11..limit] where candidates = [5,11..limit]
limit = floor(sqrt(fromIntegral n)) + 1 limit = floor(sqrt(fromIntegral n)) + 1
lcmm :: (Integral n) => [n] -> n
lcmm values
| length values == 2 = lcm (head values) (last values)
| otherwise = lcm (head values) (lcmm (tail values))
digitSum :: (Integral a, Show a) => a -> Int digitSum :: (Integral a, Show a) => a -> Int
digitSum n = sum $ map digitToInt $ show n digitSum n = sum $ map digitToInt $ show n

View File

@ -2,11 +2,11 @@
-- --
-- Find the sum of all the multiples of 3 or 5 below 1000. -- Find the sum of all the multiples of 3 or 5 below 1000.
sumMultiples :: Integer sumMultiples :: Int
sumMultiples = sum $ filter p [1..999] sumMultiples = sum $ filter p [1..999]
where p n = n `mod` 3 == 0 || n `mod` 5 == 0 where p n = n `mod` 3 == 0 || n `mod` 5 == 0
main = do main = do
let result = sumMultiples let result = sumMultiples
putStrLn $ "Project Euler, Problem 1\n" putStrLn $ "Project Euler, Problem 1\n"
++ "Answer: " ++ show result ++ "Answer: " ++ show result

View File

@ -4,15 +4,15 @@
-- --
-- By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. -- By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
fib :: Integer -> Integer fib :: Int -> Int
fib 0 = 0 fib 0 = 0
fib 1 = 1 fib 1 = 1
fib n = fib (n-1) + fib(n-2) fib n = fib (n-1) + fib(n-2)
sumEvenFib :: Integer sumEvenFib :: Int
sumEvenFib = sum $ filter even $ takeWhile (<=4000000) (map fib [0..]) sumEvenFib = sum $ filter even $ takeWhile (<=4000000) (map fib [0..])
main = do main = do
let result = sumEvenFib let result = sumEvenFib
putStrLn $ "Project Euler, Problem 2\n" putStrLn $ "Project Euler, Problem 2\n"
++ "Answer: " ++ show result ++ "Answer: " ++ show result

View File

@ -3,7 +3,7 @@
import ProjectEuler (isPrime) import ProjectEuler (isPrime)
maxPrimeFactor :: Integer -> Integer maxPrimeFactor :: Int -> Int
maxPrimeFactor n maxPrimeFactor n
| isPrime n = n | isPrime n = n
| even n = maxPrimeFactor $ n `div` 2 | even n = maxPrimeFactor $ n `div` 2

View File

@ -2,10 +2,10 @@
-- --
-- Find the largest palindrome made from the product of two 3-digit numbers. -- Find the largest palindrome made from the product of two 3-digit numbers.
isPalindrome :: Integer -> Bool isPalindrome :: Int -> Bool
isPalindrome n = show n == reverse (show n) isPalindrome n = show n == reverse (show n)
maxPalindrome :: Integer maxPalindrome :: Int
maxPalindrome = maxPalindrome =
maximum $ filter isPalindrome [ x * y | x <- [100..999], y <- [100..999] ] maximum $ filter isPalindrome [ x * y | x <- [100..999], y <- [100..999] ]

View File

@ -2,10 +2,7 @@
-- --
-- What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? -- What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
lcmm :: (Integral n) => [n] -> n import ProjectEuler (lcmm)
lcmm values
| length values == 2 = lcm (head values) (last values)
| otherwise = lcm (head values) (lcmm (tail values))
main = do main = do
let result = lcmm [1..20] let result = lcmm [1..20]

View File

@ -10,8 +10,8 @@
-- --
--Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. --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 :: Int -> Int
sumSquareDiff n = (sum [1..n]^2) - sum (map (^2) [1..n]) sumSquareDiff n = (sum [1..n] ^2) - sum (map (^2) [1..n])
main = do main = do
let result = sumSquareDiff 100 let result = sumSquareDiff 100

View File

@ -4,7 +4,7 @@
-- --
import ProjectEuler (isPrime) import ProjectEuler (isPrime)
nthPrime :: (Integral a) => Int -> a nthPrime :: Int -> Int
nthPrime n = last $ take n [ x | x <- [1..], isPrime x ] nthPrime n = last $ take n [ x | x <- [1..], isPrime x ]
main = do main = do

View File

@ -8,10 +8,10 @@
-- --
-- Find the product abc. -- Find the product abc.
pythagoreanTriplet :: (Integral a) => a -> (a, a, a) pythagoreanTriplet :: Int -> (Int, Int, Int)
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] 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 :: (Int, Int, Int) -> Int
prodTriplet (x, y, z) = x * y * z prodTriplet (x, y, z) = x * y * z
main = do main = do

View File

@ -4,7 +4,7 @@
import ProjectEuler (isPrime) import ProjectEuler (isPrime)
sumPrimes :: (Integral a) => a -> a sumPrimes :: Int -> Int
sumPrimes n = sum [ x | x <- [1..n], isPrime x ] sumPrimes n = sum [ x | x <- [1..n], isPrime x ]
main = do main = do