-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler32.hs
More file actions
84 lines (62 loc) · 2.44 KB
/
euler32.hs
File metadata and controls
84 lines (62 loc) · 2.44 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module Main where
import Data.Traversable
import Data.List
make_lists xs n = sequenceA $ replicate n xs
-- Convert a number to a list of digits. Order is
-- reversed.
number_to_list 0 = []
number_to_list x = ntl_helper x
where ntl_helper 0 = []
ntl_helper x = (x `mod` 10) : ntl_helper (x `div` 10)
-- Convert a list of digits to a number.
list_to_number [] = 0
list_to_number s@(x:xs) =
x * 10^((length s)-1) + list_to_number xs
-- Recursively search for duplicates in the list.
has_duplicates [] = False
has_duplicates (x:xs)
| x `elem` xs = True
| otherwise = has_duplicates xs
-- Helper: Checks a list for duplicate digits
-- and returns true if the number is pandigital.
detect_pand n = not $ has_duplicates $ number_to_list n
-- Detects mutual pandigital numbers by converting
-- both to a list, concatinating, and looking for
-- duplicates.
detect_mutual_pand x y =
let xl = number_to_list x
yl = number_to_list y
in not . has_duplicates $ xl ++ yl
-- Three argument version of the above.
detect_mutual_pand3 x y z =
let xl = number_to_list x
yl = number_to_list y
zl = number_to_list z
in not . has_duplicates $ xl ++ yl ++ zl
-- Helper: returns true if the number doesn't
-- contain the number 0.
nozero x = notElem 0 $ number_to_list x
-- A simple map function to yield pairs and their products.
tuple_products xs = map (\(x,y) -> (x,y,x*y)) xs
-- Filter a list to return only pandigital triplets.
pand_products_filter xs =
filter (\(x,y,z) -> detect_mutual_pand3 x y z) $
filter (\(_,_,z) -> nozero z) xs
-- Collect all two-, three-, and four-digit pandigitals
pand_twos = [x|x<-[11..98], detect_pand x, nozero x]
pand_threes = [x|x<-[123..987], detect_pand x,nozero x]
pand_fours = [x|x<-[1234..9876], detect_pand x, nozero x]
-- Build, then filter the products.
pand23 = [(x,y) | x<-pand_twos, y<-pand_threes, detect_mutual_pand x y]
pand23products = tuple_products pand23
pand23products_filtered = pand_products_filter pand23products
pand14 = [(x,y) | x<-[2..9], y<-pand_fours, detect_mutual_pand x y]
pand14products = tuple_products pand14
pand14products_filtered = pand_products_filter pand14products
-- Remove duplicate products.
euler32 =
nubBy (\(_,_,x) (_,_,y) -> x == y) $
pand23products_filtered ++ pand14products_filtered
-- Produce the sum.
euler32sum = sum $ map (\(_,_,x) -> x) euler32
main = putStrLn $ show $ euler32sum