-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem24.ml
More file actions
39 lines (35 loc) · 868 Bytes
/
problem24.ml
File metadata and controls
39 lines (35 loc) · 868 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
27
28
29
30
31
32
33
34
35
36
37
38
let swap ind1 ind2 tbl =
let temp = tbl.(ind1) in
tbl.(ind1) <- tbl.(ind2);
tbl.(ind2) <- temp
;;
let rec reverse ind1 ind2 tbl =
match (ind2-ind1) with
| 0 -> ()
| 1 -> swap ind1 ind2 tbl
| _ -> swap ind1 ind2 tbl;
reverse (ind1 + 1) (ind2 - 1) tbl
;;
let permutations =
let perm = Array.make 10 0
in
let k = ref 0
in
let l = ref 0
in
Array.iteri (fun ind _ -> perm.(ind) <- ind) perm;
for i = 2 to 1000000 do
k := 8;
while (perm.(!k) >= perm.((!k)+1)) do
k := (!k) - 1;
done;
l := 9;
while (perm.(!k) >= perm.(!l)) do
l := (!l) - 1;
done;
swap (!k) (!l) perm;
reverse ((!k)+1) 9 perm;
done;
Array.iter (fun value -> Printf.printf "%d" value) perm;
Printf.printf "\n"
;;