/
big_int.cr
395 lines (319 loc) · 7.39 KB
/
big_int.cr
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
require "c/string"
require "./big"
# A BigInt can represent arbitrarily large integers.
#
# It is implemented under the hood with [GMP](https://gmplib.org/).
struct BigInt < Int
include Comparable(Int::Signed)
include Comparable(Int::Unsigned)
include Comparable(BigInt)
include Comparable(Float)
# Creates a BigInt with the value zero.
#
# ```
# BigInt.new # => 0
# ```
def initialize
LibGMP.init(out @mpz)
end
# Creates a BigInt with the value denoted by *str* in the given *base*.
#
# Raises `ArgumentError` if the string doesn't denote a valid integer.
#
# ```
# BigInt.new("123456789123456789123456789123456789") # => 123456789123456789123456789123456789
# BigInt.new("1234567890ABCDEF", base: 16) # => 1311768467294899695
# ```
def initialize(str : String, base = 10)
err = LibGMP.init_set_str(out @mpz, str, base)
if err == -1
raise ArgumentError.new("invalid BigInt: #{str}")
end
end
# Creates a BigInt from the given *num*.
def initialize(num : Int::Signed)
if LibC::Long::MIN <= num <= LibC::Long::MAX
LibGMP.init_set_si(out @mpz, num)
else
LibGMP.init_set_str(out @mpz, num.to_s, 10)
end
end
# ditto
def initialize(num : Int::Unsigned)
if num <= LibC::ULong::MAX
LibGMP.init_set_ui(out @mpz, num)
else
LibGMP.init_set_str(out @mpz, num.to_s, 10)
end
end
# ditto
def initialize(num : Float)
LibGMP.init_set_d(out @mpz, num)
end
# Returns `num`. Useful for generic code that does `T.new(...)` with `T`
# being a `Number`.
def self.new(num : BigInt)
num
end
# :nodoc:
def initialize(@mpz : LibGMP::MPZ)
end
# :nodoc:
def self.new
LibGMP.init(out mpz)
yield pointerof(mpz)
new(mpz)
end
def <=>(other : BigInt)
LibGMP.cmp(mpz, other)
end
def <=>(other : Int::Signed)
if LibC::Long::MIN <= other <= LibC::Long::MAX
LibGMP.cmp_si(mpz, other)
else
self <=> BigInt.new(other)
end
end
def <=>(other : Int::Unsigned)
if other <= LibC::ULong::MAX
LibGMP.cmp_ui(mpz, other)
else
self <=> BigInt.new(other)
end
end
def <=>(other : Float)
LibGMP.cmp_d(mpz, other)
end
def +(other : BigInt) : BigInt
BigInt.new { |mpz| LibGMP.add(mpz, self, other) }
end
def +(other : Int) : BigInt
if other < 0
self - other.abs
else
BigInt.new { |mpz| LibGMP.add_ui(mpz, self, other) }
end
end
def -(other : BigInt) : BigInt
BigInt.new { |mpz| LibGMP.sub(mpz, self, other) }
end
def -(other : Int) : BigInt
if other < 0
self + other.abs
else
BigInt.new { |mpz| LibGMP.sub_ui(mpz, self, other) }
end
end
def - : BigInt
BigInt.new { |mpz| LibGMP.neg(mpz, self) }
end
def abs : BigInt
BigInt.new { |mpz| LibGMP.abs(mpz, self) }
end
def *(other : BigInt) : BigInt
BigInt.new { |mpz| LibGMP.mul(mpz, self, other) }
end
def *(other : Int::Signed) : BigInt
BigInt.new { |mpz| LibGMP.mul_si(mpz, self, other) }
end
def *(other : Int::Unsigned) : BigInt
BigInt.new { |mpz| LibGMP.mul_ui(mpz, self, other) }
end
def /(other : BigInt) : BigInt
check_division_by_zero other
BigInt.new { |mpz| LibGMP.fdiv_q(mpz, self, other) }
end
def /(other : Int) : BigInt
check_division_by_zero other
if other < 0
-(self / other.abs)
else
BigInt.new { |mpz| LibGMP.fdiv_q_ui(mpz, self, other) }
end
end
def %(other : BigInt) : BigInt
check_division_by_zero other
BigInt.new { |mpz| LibGMP.fdiv_r(mpz, self, other) }
end
def %(other : Int) : BigInt
check_division_by_zero other
BigInt.new { |mpz| LibGMP.fdiv_r_ui(mpz, self, other.abs) }
end
def ~ : BigInt
BigInt.new { |mpz| LibGMP.com(mpz, self) }
end
def &(other : Int) : BigInt
BigInt.new { |mpz| LibGMP.and(mpz, self, other.to_big_i) }
end
def |(other : Int) : BigInt
BigInt.new { |mpz| LibGMP.ior(mpz, self, other.to_big_i) }
end
def ^(other : Int) : BigInt
BigInt.new { |mpz| LibGMP.xor(mpz, self, other.to_big_i) }
end
def >>(other : Int) : BigInt
BigInt.new { |mpz| LibGMP.fdiv_q_2exp(mpz, self, other) }
end
def <<(other : Int) : BigInt
BigInt.new { |mpz| LibGMP.mul_2exp(mpz, self, other) }
end
def **(other : Int) : BigInt
if other < 0
raise ArgumentError.new("negative exponent isn't supported")
end
BigInt.new { |mpz| LibGMP.pow_ui(mpz, self, other) }
end
def inspect
to_s
end
def inspect(io)
to_s io
end
def hash
to_u64
end
# Returns a string representation of self.
#
# ```
# puts BigInt.new("123456789101101987654321").to_s # => 123456789101101987654321
# ```
def to_s
String.new(to_cstr)
end
# ditto
def to_s(io)
str = to_cstr
io.write_utf8 Slice.new(str, LibC.strlen(str))
end
# Returns a string containing the representation of big radix base (2 through 36).
#
# ```
# puts BigInt.new("123456789101101987654321").to_s(8) # => 32111154373025463465765261
# puts BigInt.new("123456789101101987654321").to_s(16) # => 1a249b1f61599cd7eab1
# puts BigInt.new("123456789101101987654321").to_s(36) # => k3qmt029k48nmpd
# ```
def to_s(base : Int)
raise "Invalid base #{base}" unless 2 <= base <= 36
cstr = LibGMP.get_str(nil, base, self)
String.new(cstr)
end
def digits : Array(Int32)
ary = [] of Int32
self.to_s.each_char { |c| ary << c - '0' }
ary
end
def popcount
LibGMP.popcount(self)
end
def to_i
to_i32
end
def to_i8
to_i64.to_i8
end
def to_i16
to_i64.to_i16
end
def to_i32
to_i64.to_i32
end
def to_i64
LibGMP.get_si(self)
end
def to_u
to_u32
end
def to_u8
to_u64.to_u8
end
def to_u16
to_u64.to_u16
end
def to_u32
to_u64.to_u32
end
def to_u64
LibGMP.get_ui(self).to_u64
end
def to_f
to_f64
end
def to_f32
to_f64.to_f32
end
def to_f64
LibGMP.get_d(self)
end
def to_big_i
self
end
def clone
self
end
private def check_division_by_zero(value)
if value == 0
raise DivisionByZero.new
end
end
private def mpz
pointerof(@mpz)
end
private def to_cstr
LibGMP.get_str(nil, 10, mpz)
end
def to_unsafe
mpz
end
end
struct Int
include Comparable(BigInt)
def <=>(other : BigInt)
-(other <=> self)
end
def +(other : BigInt) : BigInt
other + self
end
def -(other : BigInt) : BigInt
if self < 0
-(abs + other)
else
# The line below segfault on linux 32 bits for a (yet) unknown reason:
#
# BigInt.new { |mpz| LibGMP.ui_sub(mpz, self.to_u64, other) }
#
# So for now we do it a bit slower.
to_big_i - other
end
end
def *(other : BigInt) : BigInt
other * self
end
def /(other : BigInt) : BigInt
to_big_i / other
end
def %(other : BigInt) : BigInt
to_big_i % other
end
# Returns a BigInt representing this integer.
def to_big_i : BigInt
BigInt.new(self)
end
end
struct Float
include Comparable(BigInt)
def <=>(other : BigInt)
-(other <=> self)
end
# Returns a BigInt representing this float (rounded using `floor`).
def to_big_i : BigInt
BigInt.new(self)
end
end
class String
# Returns a BigInt from this string, in the given *base*.
#
# Raises `ArgumentError` if this string doesn't denote a valid integer.
def to_big_i(base = 10) : BigInt
BigInt.new(self, base)
end
end