-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathp038.hs
More file actions
26 lines (19 loc) · 695 Bytes
/
p038.hs
File metadata and controls
26 lines (19 loc) · 695 Bytes
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
{-
- Solution to Project Euler problem 38
- Copyright (c) Project Nayuki. All rights reserved.
-
- https://www.nayuki.io/page/project-euler-solutions
- https://github.com/nayuki/Project-Euler-solutions
-}
import Data.List (sort)
main = putStrLn (show ans)
ans = foldl1 max $ concat [[number (concatProd n x) | x <- [1 .. 10 ^ (div 9 n) - 1], pandigital (concatProd n x)] | n <- [2..9]]
digits 0 = [0]
digits n = digits' n where
digits' 0 = []
digits' n = (digits' (div n 10)) ++ [mod n 10]
number d = number' d 0 where
number' [] acc = acc
number' (d:ds) acc = number' ds (acc * 10 + d)
pandigital d = (sort d) == [1..9]
concatProd n x = concat [digits (x * i) | i <- [1..n]]