-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbignum.ml
More file actions
283 lines (212 loc) · 5.72 KB
/
bignum.ml
File metadata and controls
283 lines (212 loc) · 5.72 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
(** Representation for bnat:
- list of bits
- lower bits are at the head of the list
- leading bit must be B1 for a non-zero number
*)
type bit = B0 | B1
type bnat = bit list
(*****************************************************************************)
(** * Auxiliary functions *)
(** Smart constructor, ensuring normalization of [B0] to [] *)
let bcons b n = (* returns b+2*n *)
match b, n with
| B0, [] -> []
| _ -> b::n
(** Auxiliary functions for addition and subtraction *)
let two_bits_repr n = (* requires n <= 3 *)
(* returns (b0,b1) such that n=2*b1+b0 *)
match n with
| [] -> (B0,B0)
| [B1] -> (B1,B0)
| [b0;b1] -> (b0,b1)
| _ -> assert false
(** Conversion of a bit to a (nonnegative) primitive integers *)
let bit_to_nat b =
match b with
| B0 -> 0
| B1 -> 1
(** Conversion of a bnat to a (nonnegative) primitive integers *)
let rec bnat_to_nat n =
match n with
| [] -> 0
| b::n' -> bit_to_nat b + 2 * (bnat_to_nat n')
(** Conversion of a bit to a bnat *)
let bit_to_bnat b =
match b with
| B0 -> []
| B1 -> [B1]
(** Conversion of a bit its opposite bit *)
let bit_flip b = (* returns 1-b *)
match b with
| B0 -> B1
| B1 -> B0
(*****************************************************************************)
(** * Constants and comparisons *)
(** Constants *)
let zero = []
let one = [B1]
let two = [B0;B1]
(** Comparison algorithm,
returns -1, 0 or 1, according to sign(n1-n2).
Complexity: O(min(log n1, log n2)) *)
let rec cmp n1 n2 =
match n1, n2 with
| [], [] -> 0
| [], _ -> -1
| _, [] -> 1
| b1::n1', b2::n2' ->
let c = cmp n1' n2' in
if c <> 0 then c
else if b1 = b2 then 0
else if b1 = B0 then -1
else 1
(** Basic comparisons *)
let (^=) n1 n2 =
cmp n1 n2 = 0
let (^<=) n1 n2 =
cmp n1 n2 <= 0
let (^<) n1 n2 =
cmp n1 n2 < 0
(** Symmetric comparisons *)
let (^<>) n1 n2 =
not (n1 ^= n2)
let (^>=) n1 n2 =
n2 ^<= n1
let (^>) n1 n2 =
n2 ^< n1
(*****************************************************************************)
(** * Addition and subtraction *)
(** Multiplication by 2
Complexity: O(1) *)
let mul2 n =
bcons B0 n
(** Successor
Complexity: O(log n) *)
let rec succ n = (* returns n+1 *)
match n with
| [] -> [B1]
| B0::n' -> B1::n'
| B1::n' -> B0::(succ n')
(** Addition of a bit to a bnat *)
let add_bit b n = (* returns n+b *)
if b = B0
then n
else succ n
(** Addition
Complexity: O(max(log n1, log n2)) *)
let (^+) n1 n2 =
let rec aux n1 n2 b = (* b is the carry bit *)
match n1, n2 with
| [], _ -> add_bit b n2
| _, [] -> add_bit b n1
| b1::n1', b2::n2' ->
let r = add_bit b (add_bit b1 (bit_to_bnat b2)) in
let (b3,b') = two_bits_repr r in
b3 :: (aux n1' n2' b')
in
aux n1 n2 B0
(** Predecessor *)
let rec pred n = (* returns max(n-1,0) *)
match n with
| [] -> []
| B1::[] -> []
| B1::n' -> bcons B0 n'
| B0::n' -> B1::(pred n')
(** Subtraction of a bit to a bnat *)
let sub_bit b n = (* returns n-b *)
if b = B0
then n
else pred n
(* Subtraction
Complexity: O(log n1) *)
let (^-) n1 n2 = (* requires n1 >= n2 *)
let rec aux n1 n2 b = (* b is the carry bit *)
match n1, n2 with
| [], [] -> if b = B0 then [] else assert false
| [], _ -> assert false
| _, [] -> sub_bit b n1
| b1::n1', b2::n2' ->
(* b1-b2-b = b3-2b', thus 2+b1-b2-b = b3+2*(1-b') *)
let r = sub_bit b (sub_bit b2 (add_bit b1 two)) in
let (b3,bneg') = two_bits_repr r in
let b' = bit_flip bneg' in
bcons b3 (aux n1' n2' b')
in
aux n1 n2 B0
(*****************************************************************************)
(** * Multiplication and exponentiation *)
(** Multiplication (naive)
Complexity: O(log n1 * log n2) *)
let rec (^*) n1 n2 =
match n1 with
| [] -> zero
| b::n1' ->
let r = mul2 (n1' ^* n2) in
if b = B0 then r else r ^+ n2
(** Power (naive)
Complexity: O(log n1 * n2 ^ 2) (?) *)
let rec (^^) n1 n2 =
if n2 ^= zero
then one
else n1 ^* n1 ^^ (n2 ^- one)
(*****************************************************************************)
(** * Division, modulo and co-primality *)
(** Even test
Complexity O(1) *)
let even n =
match n with
| [] -> true
| B0::n' -> true
| _ -> false
(** Division by 2
Complexity O(1) *)
let div2 n =
match n with
| [] -> []
| b::n' -> n'
(* Shiftr O (n2) *)
let shiftr n1 n2 =
if n2 ^= zero
then n1
else shiftr (div2 n1) (pred n2)
(** Modulo
Complexity O(log n1 * log n2) *)
let rec (^%) n1 n2 = (* requires n2 > 0 *)
match n1 with
| [] -> zero
| b::n1' ->
let r = add_bit b (mul2 (n1' ^% n2)) in
if n2 ^<= r then r ^- n2 else r
(** Coprime
Complexity O(log n1 + log n2)^2) *)
let rec coprime n1 n2 =
let c = cmp n1 n2 in
if c < 0 then
coprime n2 n1 (* swap to ensure n1 >= n2 in the code below *)
else if n2 ^= zero then
n1 ^= one (* zero is only coprime with one *)
else if c = 0 then
n1 ^= one (* a non-zero number is coprime with itself only if it is one *)
else
let simpl n = (* half a number if it is even *)
if even n then div2 n else n in
if even n1 || even n2
then coprime (simpl n1) (simpl n2)
else coprime (div2 (n1 ^- n2)) n2
(* alternative 1:
match even n1, even n2 with
| true, true -> coprime (div2 n1) (div2 n2)
| true, false -> coprime (div2 n1) n2
| false, true -> coprime n1 (div2 n2)
| false, false -> coprime (n1 ^- n2) n2
*)
(* alternative 2:
if even n1 && even n2 then
coprime (div2 n1) (div2 n2)
else if even n1 then
coprime (div2 n1) n2
else if even n2 then
coprime n1 (div2 n2)
else
coprime (n1 ^- n2) n2
*)