-
Notifications
You must be signed in to change notification settings - Fork 5
/
euler.ml
175 lines (144 loc) · 3.59 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
(* Project Euler in OCaml
* John Evans <[email protected]>
*)
(* Euler #1
* Answer: 233168
*
* If we list all the natural numbers below 10 that are multiples of 3 or 5,
* we get 3, 5, 6 and 9. The sum of these multiples is 23.
*
* Find the sum of all the multiples of 3 or 5 below 1000.
*)
let rec range a b =
if a > b then []
else a :: range (a+1) b
;;
let sum_list =
List.fold_left ( + ) 0
;;
let divisible_by_3_or_5 n =
(n mod 3 == 0) || (n mod 5 == 0)
;;
let euler1 = fun() ->
sum_list (List.filter divisible_by_3_or_5 (range 3 999))
;;
(* Euler #2
* Answer: 4613732
*
* Each new term in the Fibonacci sequence is generated by adding the previous
* two terms. By starting with 1 and 2, the first 10 terms will be:
*
* 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
*
* Find the sum of all the even-valued terms in the sequence which do not
* exceed four million.
*)
let rec e2search a b acc =
if b > 4000000 then acc
else e2search b (a + b) (if b mod 2 == 0 then acc + b else acc)
;;
let euler2 = fun() ->
e2search 1 2 0
;;
(* Euler #3:
* Answer: 6857 [ NOT WORKING YET ]
*
* The prime factors of 13195 are 5, 7, 13 and 29.
*
* What is the largest prime factor of the number 600851475143 ?
*)
(* let rec largest_factor_of_n_below_n_acc n x =
if n mod x == 0 then x
else largest_factor_of_n_below_n_acc n (x - 1)
;;
let largest_factor_of_n_below_n n =
largest_factor_of_n_below_n_acc n (n - 1)
;;
let is_prime n =
largest_factor_of_n_below_n n == 1
;;
let rec e3search f t =
if f mod t == 0 && is_prime f then f
else e3search (f-1) t
;;
let euler3 =
let target = 600851475143
let max_factor = ceil (sqrt target)
e3search max_factor target
;; *)
(* Problem #4
* Answer: 906609
*
* A palindromic number reads the same both ways. The largest
* palindrome made from the product of two 2-digit numbers is 9009 =
* 91 99.
*
* Find the largest palindrome made from the product of two 3-digit
* numbers.
*)
(* let rec list_of_string s =
match s with
"" -> []
| _ -> (String.get s 0)::list_of_string (String.sub s 1 ((String.length s) - 1))
;;
let string_from_char c =
String.make 1 c
;;
let rec string_of_list xs =
match xs with
[] -> ""
| _ -> (string_from_char (List.hd xs))::string_of_list (List.tail xs)
;;
(* Really? *)
let rec rev s =
match s with
[] -> s
| x::xs -> (rev xs)::[x;]
s;;
let rec rev (l, a) = match l with
[] -> a
| (x::xs) -> rev (xs, (x::a))
let rev l = rev (l, [])
;;
let is_palindromic_number n =
(string_of_int n) == rev (string_of_int n);;
(* let s = string_of_int n;
s == rev s
;; *)
let e4search i j acc =
if j > 999 then acc
else
if i > 999 then e4search 1 (j + 1) acc
else
let p = i * j
e4search (i + 1) j (if is_palindromic_number p then acc + p else acc)
;;
let euler4 =
e4search 1 1 0
;; *)
let eulers = [|euler1, euler2|];;
let print_euler n =
print_char '#';
print_int n;
print_string ": ";
let euler = eulers.(n) in
print_int euler
;;
let main () =
let num_args = Array.length Sys.argv in
if num_args > 0 then
for i = 1 to num_args - 1 do
print_euler int_of_string Sys.argv.(i)
done
else
for i = 0 to Array.length eulers do
print_int eulers.(i)
done
;;
(* if Sys.arg
let print_all =
print_int euler1;
print_char '\n';
print_int euler2;
print_char '\n';; *)
main ();;