-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEuler51.hs
68 lines (52 loc) · 1.99 KB
/
Euler51.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import Data.List
import qualified Data.Map as Map
import Data.Ord
import Primes
{- This is too slow and also possibly incorrect
digits :: [Maybe Int]
digits = Nothing : map Just [0..9]
numbersPerhapsWithStars = map (:[]) digits ++ [d:ds | ds<-numbersWithStars, d<-digits]
numbersWithStars = filter (elem Nothing) numbersPerhapsWithStars
toNumber :: [Maybe Int] -> Int -> Int
toNumber [] _ = 0
toNumber (Just d:ds) def = d + 10 * (toNumber ds def)
toNumber (Nothing:ds) def = def + 10 * (toNumber ds def)
family :: [Maybe Int] -> [Int]
family ds = map (toNumber ds) [0..9]
primeFamily :: [Maybe Int] -> [Int]
primeFamily = filter isPrime . family
countPrimes :: [Maybe Int] -> Int
countPrimes = length . primeFamily
earliestLargeFamily :: Int -> [Maybe Int]
earliestLargeFamily k = head . filter ((>k) . countPrimes) $ numbersWithStars
printPrimeFamily :: [Maybe Int] -> IO ()
printPrimeFamily ds = do
putStrLn . toString $ ds
mapM_ print $ primeFamily ds
-}
-- We represent a family as a list of Maybe Int. [Just 5, Nothing, Just 1] represents the family 1*5.
toString' :: [Maybe Int] -> String
toString' [] = ""
toString' (x:xs) = toChar x : toString' xs
where toChar (Just d) = head . show $ d
toChar Nothing = '*'
toString = reverse . toString'
digits :: Int -> [Int]
digits 0 = []
digits x = (x `mod` 10) : (digits $ x `div` 10)
-- All the families that a given number belongs to, assuming stars are replaced by a given digit
families' :: Int -> Int -> [[Maybe Int]]
families' n k
| k `elem` digits n = filter (elem Nothing) $ families'' (digits n) k
| otherwise = []
families'' :: [Int] -> Int -> [[Maybe Int]]
families'' [] _ = [[]]
families'' (d:ds) k
| d == k = addJust ++ addNothing
| otherwise = addJust
where addJust = map (Just d:) $ families'' ds k
addNothing = map (Nothing:) $ families'' ds k
families :: Int -> [[Maybe Int]]
families n = concatMap (families' n) [0..9]
countSet :: Ord a => [a] -> Map.Map a Int
countSet = Map.fromListWith (+) . map (\a -> (a,1))