diff --git a/Haskell/ProjectEuler.hs b/Haskell/ProjectEuler.hs index ada821b..b4b6b39 100644 --- a/Haskell/ProjectEuler.hs +++ b/Haskell/ProjectEuler.hs @@ -1,5 +1,6 @@ module ProjectEuler ( isPrime +, lcmm , digitSum , sumProperDivisors , countDivisors @@ -18,6 +19,11 @@ isPrime n = where candidates = [5,11..limit] 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 n = sum $ map digitToInt $ show n diff --git a/Haskell/p001.hs b/Haskell/p001.hs index 7e09712..7bc8713 100644 --- a/Haskell/p001.hs +++ b/Haskell/p001.hs @@ -2,11 +2,11 @@ -- -- Find the sum of all the multiples of 3 or 5 below 1000. -sumMultiples :: Integer +sumMultiples :: Int 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 putStrLn $ "Project Euler, Problem 1\n" ++ "Answer: " ++ show result diff --git a/Haskell/p002.hs b/Haskell/p002.hs index 6fb8531..269e9b4 100644 --- a/Haskell/p002.hs +++ b/Haskell/p002.hs @@ -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. -fib :: Integer -> Integer +fib :: Int -> Int fib 0 = 0 fib 1 = 1 fib n = fib (n-1) + fib(n-2) -sumEvenFib :: Integer +sumEvenFib :: Int sumEvenFib = sum $ filter even $ takeWhile (<=4000000) (map fib [0..]) -main = do +main = do let result = sumEvenFib putStrLn $ "Project Euler, Problem 2\n" ++ "Answer: " ++ show result diff --git a/Haskell/p003.hs b/Haskell/p003.hs index 13c574f..7226e4c 100644 --- a/Haskell/p003.hs +++ b/Haskell/p003.hs @@ -3,7 +3,7 @@ import ProjectEuler (isPrime) -maxPrimeFactor :: Integer -> Integer +maxPrimeFactor :: Int -> Int maxPrimeFactor n | isPrime n = n | even n = maxPrimeFactor $ n `div` 2 diff --git a/Haskell/p004.hs b/Haskell/p004.hs index c0a0e8e..e5f26a9 100644 --- a/Haskell/p004.hs +++ b/Haskell/p004.hs @@ -2,10 +2,10 @@ -- -- 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) -maxPalindrome :: Integer +maxPalindrome :: Int maxPalindrome = maximum $ filter isPalindrome [ x * y | x <- [100..999], y <- [100..999] ] diff --git a/Haskell/p005.hs b/Haskell/p005.hs index 11c346f..b32b130 100644 --- a/Haskell/p005.hs +++ b/Haskell/p005.hs @@ -2,10 +2,7 @@ -- -- What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? -lcmm :: (Integral n) => [n] -> n -lcmm values - | length values == 2 = lcm (head values) (last values) - | otherwise = lcm (head values) (lcmm (tail values)) +import ProjectEuler (lcmm) main = do let result = lcmm [1..20] diff --git a/Haskell/p006.hs b/Haskell/p006.hs index a762445..7e69002 100644 --- a/Haskell/p006.hs +++ b/Haskell/p006.hs @@ -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. -sumSquareDiff :: (Integral a) => a -> a -sumSquareDiff n = (sum [1..n]^2) - sum (map (^2) [1..n]) +sumSquareDiff :: Int -> Int +sumSquareDiff n = (sum [1..n] ^2) - sum (map (^2) [1..n]) main = do let result = sumSquareDiff 100 diff --git a/Haskell/p007.hs b/Haskell/p007.hs index 7e025ab..ebf9987 100644 --- a/Haskell/p007.hs +++ b/Haskell/p007.hs @@ -4,7 +4,7 @@ -- import ProjectEuler (isPrime) -nthPrime :: (Integral a) => Int -> a +nthPrime :: Int -> Int nthPrime n = last $ take n [ x | x <- [1..], isPrime x ] main = do diff --git a/Haskell/p009.hs b/Haskell/p009.hs index eea6997..a068474 100644 --- a/Haskell/p009.hs +++ b/Haskell/p009.hs @@ -8,10 +8,10 @@ -- -- 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] -prodTriplet :: (Integral a) => (a, a, a) -> a +prodTriplet :: (Int, Int, Int) -> Int prodTriplet (x, y, z) = x * y * z main = do diff --git a/Haskell/p010.hs b/Haskell/p010.hs index 42109ac..d8bc5fd 100644 --- a/Haskell/p010.hs +++ b/Haskell/p010.hs @@ -4,7 +4,7 @@ import ProjectEuler (isPrime) -sumPrimes :: (Integral a) => a -> a +sumPrimes :: Int -> Int sumPrimes n = sum [ x | x <- [1..n], isPrime x ] main = do