Improve Haskell code for a few problems
This commit is contained in:
parent
91a8d44801
commit
187483b624
@ -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
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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] ]
|
||||||
|
|
||||||
|
@ -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]
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user