-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler.ml
127 lines (107 loc) · 4.49 KB
/
euler.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
(* Author: Bahman Movaqar <Bahman@BahmanM.com> *)
open Batteries
open BatPrintf
module Utils = struct
let time f =
let t1 = Sys.time() in
let result = f() in
let t2 = Sys.time() in
let t = ((t2 -. t1) *. 1_000_000.) |> int_of_float in
(result, t)
let thousand_sep n =
let rec f idx ll =
match ll with
| [] -> []
| hd :: tl when idx mod 3 = 0 && (List.is_empty tl = false)->
hd :: ',' :: (f (idx + 1) tl)
| hd :: tl ->
hd :: (f (idx + 1) tl)
in
n
|> String.to_list |> List.rev
|> f 1
|> List.rev |> String.of_list
end
(*******************************************************************************)
module Printer = struct
type problem_t = unit -> string
let print_header () =
printf "\n\n";
printf " ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ \n";
printf " ┃ PROJECT EULER ANSWERS ┃ \n";
printf " ┣━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫ \n";
printf " ┃ PROBLEM # ┃ ANSWER ┃ TIME (µs) ┃ \n";
printf " ┣━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫ \n"
let separator_line =
sprintf " ┠────────────┼──────────────────────────────────┼────────────────────────────┨ \n"
let print_last_line () =
printf " ┗━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ \n"
let result_line n result t =
sprintf " ┃ %10s │ %32s │ %26s ┃ \n"
(Utils.thousand_sep (string_of_int n))
result
(Utils.thousand_sep (string_of_int t))
let rec print_problems = function
| [] -> ()
| p :: [] -> print_string (p()); printf "%!";
| p :: tl -> begin
print_string (p());
print_string separator_line;
printf "%!";
print_problems tl
end
end
(*******************************************************************************)
let problems : Printer.problem_t list =
[
(* 1 *)
begin fun _ ->
let (r, t) = Utils.time (fun _ -> P1.solution 1000) in
Printer.result_line 1 (Utils.thousand_sep (string_of_int r)) t
end;
(* 2 *)
begin fun _ ->
let (r, t) = Utils.time (fun _ -> P2.solution 4_000_000) in
Printer.result_line 2 (Utils.thousand_sep (string_of_int r)) t
end;
(* 3 *)
begin fun _ ->
let (r, t) = Utils.time (fun _ -> P3.solution 600_851_475_143) in
Printer.result_line 3 (Utils.thousand_sep (string_of_int r)) t
end;
(* 15 *)
begin fun _ ->
let (r, t) = Utils.time (fun _ -> P15.solution 20) in
Printer.result_line 15 (Utils.thousand_sep (Big_int.string_of_big_int r)) t
end;
(* 22 *)
begin fun _ ->
let s = begin
File.with_file_in
"./test/res/p022_names.txt"
(fun i -> IO.read_all i)
end in
let (r, t) = Utils.time (fun _ -> P22.solve s) in
Printer.result_line 22 (Utils.thousand_sep (Int64.to_string r)) t
end;
(* 41 *)
begin fun _ ->
let (r, t) = Utils.time (fun _ -> P41.solve 10) in
Printer.result_line 41 (Utils.thousand_sep (string_of_int r)) t
end;
(* 45 *)
begin fun _ ->
let (r, t) = Utils.time (fun _ -> P45.solve 285) in
Printer.result_line 45 (Utils.thousand_sep (string_of_int r)) t
end;
(* 54 *)
begin fun _ ->
let (r, t) = Utils.time (fun _ -> P54.solution None) in
Printer.result_line 54 (Utils.thousand_sep (string_of_int r)) t
end;
]
(*******************************************************************************)
let _ =
Printer.print_header();
problems |> Printer.print_problems;
Printer.print_last_line()