Improve Haskell code for a few problems
This commit is contained in:
parent
4f9bfc608c
commit
51bde9da48
@ -14,19 +14,16 @@ isPrime 1 = False
|
|||||||
isPrime 2 = True
|
isPrime 2 = True
|
||||||
isPrime 3 = True
|
isPrime 3 = True
|
||||||
isPrime n =
|
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]
|
where candidates = [5,11..limit]
|
||||||
limit = floor(sqrt(fromIntegral n)) + 1
|
limit = floor(sqrt(fromIntegral n)) + 1
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
|
||||||
sumProperDivisors :: (Integral a) => a -> a
|
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
|
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 :: (Integral a) => a -> Int
|
||||||
countDivisors n = length $ nub $ concat [ [x, n `div` x] | x <- [1..limit], n `mod` x == 0 ]
|
countDivisors n = length $ nub $ concat [ [x, n `div` x] | x <- [1..limit], n `mod` x == 0 ]
|
||||||
where limit = floor $ sqrt $ fromIntegral n
|
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
|
isPandigital n = n_length == length (nub n_char) && '0' `notElem` n_char && digitToInt (maximum n_char) == n_length
|
||||||
where n_char = show n
|
where n_char = show n
|
||||||
n_length = length n_char
|
n_length = length n_char
|
||||||
|
|
||||||
|
@ -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?
|
-- 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 :: Int -> String -> Int
|
||||||
nDigitProduct n s
|
nDigitProduct n s
|
||||||
|
@ -18,7 +18,6 @@
|
|||||||
-- What is the value of the first triangle number to have over five hundred divisors?
|
-- What is the value of the first triangle number to have over five hundred divisors?
|
||||||
|
|
||||||
import Data.List (nub)
|
import Data.List (nub)
|
||||||
|
|
||||||
import ProjectEuler (countDivisors)
|
import ProjectEuler (countDivisors)
|
||||||
|
|
||||||
triangNumbers :: [Int]
|
triangNumbers :: [Int]
|
||||||
|
@ -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)?
|
-- 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 :: Day -> Day -> Int
|
||||||
countSundaysFirst start end = let days = [start .. end]
|
countSundaysFirst start end = let days = [start .. end]
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
-- as the sum of two abundant numbers is less than this limit.
|
-- 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.
|
-- 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 qualified Data.Set as Set
|
||||||
|
|
||||||
import ProjectEuler (sumProperDivisors)
|
import ProjectEuler (sumProperDivisors)
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
--
|
--
|
||||||
-- What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
|
-- 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
|
main = do
|
||||||
let result = sort (permutations "0123456789") !! 999999
|
let result = sort (permutations "0123456789") !! 999999
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
-- 4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125
|
-- 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?
|
-- 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 :: (Integral a) => a -> [a]
|
||||||
powerCombinations n = nub [ x^y | x <- [2..n], y <- [2..n] ]
|
powerCombinations n = nub [ x^y | x <- [2..n], y <- [2..n] ]
|
||||||
|
@ -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.
|
-- 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.
|
-- 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 :: 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)) ]
|
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)) ]
|
||||||
|
@ -44,10 +44,9 @@
|
|||||||
--
|
--
|
||||||
-- How many hands does Player 1 win?
|
-- How many hands does Player 1 win?
|
||||||
|
|
||||||
import Data.List
|
import Data.List (sort, sortBy, groupBy, minimumBy, maximumBy)
|
||||||
import Data.Function
|
import Data.Function (on)
|
||||||
import Data.Maybe
|
import Data.Maybe (fromJust)
|
||||||
import System.IO
|
|
||||||
|
|
||||||
data Value = Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King | Ace
|
data Value = Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King | Ace
|
||||||
deriving (Eq, Ord, Show, Bounded, Enum)
|
deriving (Eq, Ord, Show, Bounded, Enum)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user