-
Notifications
You must be signed in to change notification settings - Fork 0
/
pe038.ml
32 lines (30 loc) · 1011 Bytes
/
pe038.ml
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
open Core.Std
open Core_extended.Std
open Util
let pandigital_products = fold_range
~init:[]
~f:(fun acc n ->
let b_range_left = 1 in
let b_range_right = (Common.pow 10 (9 / n)) - 1 in
fold_range
~init:acc
~f:(fun acc p ->
let rec iter acc = function
| cur when cur > n -> acc
| cur -> iter (acc ^ (Int.to_string (p * cur))) (cur + 1)
in
let str = iter "" 1 in
let uniq = List.dedup (String.to_list str) in
if String.length str = 9 &&
List.length uniq = 9 &&
List.hd_exn uniq = '1' then
(Int.of_string str, p, n) :: acc
else
acc
)
b_range_left b_range_right
)
2 9
let () = List.map pandigital_products ~f:(fun (prod, _, _) -> prod)
|> List.max_exn ~cmp:Int.compare
|> Printf.printf "%d\n"