-
Notifications
You must be signed in to change notification settings - Fork 1
/
problem38.fsx
43 lines (32 loc) · 929 Bytes
/
problem38.fsx
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
(* Project Euler Problem 38
* By Weisi Dai <[email protected]>
*)
open Weisi.Dai.Algorithms
let ubound = 9
let makeConcat x n =
[| 1 .. n |]
|> Array.map (fun i -> (i * x).ToString())
|> String.concat ""
let univStr =
String.concat ""
<| seq { for i in 1 .. ubound do yield i.ToString() }
let isPanDigital (str: string) =
str.ToCharArray()
|> Array.sort
|> Array.map (fun ch -> ch.ToString())
|> String.concat ""
|> univStr.Equals
let atLeast n x =
(makeConcat x n).Length >= 9
let moreThan n x =
(makeConcat x n).Length > 9
let pandigits = seq {
for n in 2 .. 9 do
let range = FindRange 1 (atLeast n) (moreThan n)
for x in fst range .. snd range do
let concat = makeConcat x n
if isPanDigital concat then yield concat
}
let problem38 = Seq.max pandigits
let main = printfn "The answer is %s." (problem38)
main