-
Notifications
You must be signed in to change notification settings - Fork 0
/
int.js
467 lines (359 loc) · 9.49 KB
/
int.js
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
var Int = function(num) {
// can be called as a function
if (!(this instanceof Int)) {
return new Int(num);
}
var self = this;
// copy existing Int object
if (num instanceof Int){
self._s = num._s;
self._d = num._d.slice();
return;
}
// default value 0
num = num || 0;
// sign
self._s = ((num += '').charAt(0) === '-') ? 1 : 0;
// digits
self._d = [];
num = num.replace(/^[+-]/, '');
var orig = num;
// remove any leading - or + as well as other invalid characters
num = num.replace(/[^\d]/g, '');
// detect if value is not a number
if (orig !== num) {
self._nan = true;
return;
}
// _d is the array of single digits making up the number
var ln = num.length;
for (var i=0 ; i<ln ; ++i) {
self._d.push(+num[i]);
}
trim_zeros(self);
// zeros are normalized to positive
// TODO (shtylman) consider not doing this and only checking in toString?
if (self._d.length === 0) {
self._s = 0;
}
};
/// add num and return new integer
Int.prototype.add = function(num) {
var self = this;
var num = ensure_int(num);
if(self._s != num._s) {
num._s ^= 1;
var res = self.sub(num);
num._s ^= 1;
return res;
}
// a will be the smaller number
if (self._d.length < num._d.length) {
var a = self._d;
var b = num._d;
var out = Int(num);
}
else {
var a = num._d;
var b = self._d;
var out = Int(self);
}
var la = a.length;
var lb = b.length;
// clone the larger number
var res = out._d;
var carry = 0;
for (var i = lb - 1, j = la - 1; i >= 0, j >= 0 ; --i, --j) {
res[i] += carry + a[j];
carry = 0;
if (res[i] >= 10) {
res[i] -= 10;
carry = 1;
}
}
// carry the rest of the way
for (; i >= 0 ; --i) {
res[i] += carry;
carry = 0;
if (res[i] >= 10) {
res[i] -= 10;
carry = 1;
}
// no carry, rest of the number will be unchanged
if (carry === 0) {
break;
}
}
// remaining carry?
if (carry > 0) {
res.unshift(1);
}
return out;
}
Int.prototype.sub = function(num) {
var self = this;
// some operations are destructive
var num = Int(num);
if(self._s != num._s) {
num._s ^= 1;
var res = this.add(num);
num._s ^= 1;
return res;
}
var s1 = self._s;
var s2 = num._s;
// make numbers positive for determining the greater one
// in absolute terms
self._s = num._s = 0;
// make a the smaller number (abs value)
var c = self.lt(num);
var a = c ? self._d : num._d;
var b = c ? num._d : self._d;
// restore original signs
self._s = s1;
num._s = s2;
var la = a.length;
var lb = b.length;
var out = Int((c) ? num : self);
out._s = num._s & self._s; // ??
var res = out._d;
// basic subtraction for common size
var borrow = 0;
for (var i = lb - 1, j = la - 1; i >= 0, j >= 0 ; --i, --j) {
res[i] -= a[j] + borrow;
borrow = 0;
if (res[i] < 0) {
res[i] += 10;
borrow = 1;
}
}
// carry the rest of the way
for (; i >= 0 ; --i) {
res[i] -= borrow;
borrow = 0;
if (res[i] < 0) {
res[i] += 10;
borrow = 1;
}
// no carry, rest of the number will be unchanged
if (borrow === 0) {
break;
}
}
// flip the sign if sub num was larger
c && (out._s ^= 1);
trim_zeros(out);
// TODO the subtraction should just be smarter
if (out._d.length === 0) {
out._s = 0;
}
return out;
};
Int.prototype.mul = function(num) {
var self = this;
var r = self._d.length >= (num = Int(num))._d.length;
var a = (r ? self : num)._d;
var b = (r ? num : self)._d;
var la = a.length;
var lb = b.length;
var sum = Int();
var zeros = [];
// loop for smaller number
for (var i = lb - 1 ; i >= 0 ; --i) {
var out = Int();
// insert proper number of trailing 0s
var val = out._d = out._d.concat(zeros);
// reset carry
var carry = 0;
// top number
for (var j = la - 1; j >= 0; --j) {
// multiplication result
var mul = b[i] * a[j] + carry;
// this is the single digit we keep
var res = mul % 10;
// carry amount
carry = Math.floor(mul / 10);
// insert the number into our new integer
val.unshift(res);
}
// apply any remaining carry
if (carry) {
val.unshift(carry);
}
sum = sum.add(out);
zeros.push(0);
}
sum._s = self._s ^ num._s;
return sum;
};
Int.prototype.div = function(num) {
var self = this;
// copy since we change sign of num
var num = Int(num);
if(num.eq0()) {
throw new Error('Division by 0');
}
else if(self.eq0()) {
return Int();
}
// copy since we do destructive things
var numerator = self._d.slice();
var quo = Int();
quo._s = self._s ^ num._s;
// normalize num to positive number
var orig_s = num._s;
num._s = 0;
// remainder from previous calculation
var rem = Int();
while (numerator.length) {
// long division
// shift numbers off the numerator until we have achieved size
// every number after the first causes a 0 to be inserted
// numbers shifted in from the remainder should not cause 0 insertion
var c = 0;
while (numerator.length && rem.lt(num)) {
if (c++ > 0) {
quo._d.push(0);
}
// shift a number from numerator to our running num
rem._d.push(numerator.shift());
// important to trim here since 009 - N won't be done right otherwise
trim_zeros(rem);
}
var count = 0;
while(rem.gte(num) && ++count) {
rem = rem.sub(num);
}
if (count === 0) {
quo._d.push(0);
break;
}
quo._d.push(count);
}
var rlen = rem._d.length;
if (rlen > 1 || (quo._s && rlen > 0)) {
rem = rem.add(5);
}
if (quo._s && (rlen !== rem._d.length || rem._d[0] >= 5)) {
quo = quo.sub(1);
}
// put back the sign of num
num._s = orig_s;
return trim_zeros(quo);
};
Int.prototype.mod = function(num) {
return this.sub(this.div(num).mul(num));
};
Int.prototype.pow = function(num) {
var out = Int(this);
if((num = (Int(num))) == 0) {
return out.set(1);
}
for(var i = Math.abs(num); --i; out.set(out.mul(this)));
return num < 0 ? out.set((Int(1)).div(out)) : out;
};
/// set this number to the value of num
Int.prototype.set = function(num) {
this.constructor(num);
return this;
};
/// -1 if self < n, 0 if self == n, 1 if self > n
Int.prototype.cmp = function(num) {
var self = this;
var num = ensure_int(num);
if (self._s != num._s) {
return self._s ? -1 : 1;
}
var a = self._d;
var b = num._d;
var la = a.length;
var lb = b.length;
if (la != lb) {
return ((la > lb) ^ self._s) ? 1 : -1;
}
for (var i = 0; i < la; ++i) {
if (a[i] != b[i]) {
return ((a[i] > b[i]) ^ self._s) ? 1 : -1;
}
}
// no differences
return 0;
};
Int.prototype.neg = function() {
var out = Int(this);
out._s ^= 1;
return out;
};
Int.prototype.abs = function() {
var out = Int(this);
out._s = 0;
return out;
};
// alphabet for converting to a specific base
var alphabet = '0123456789abcdefghijklmnopqrstuvwxyz';
Int.prototype.valueOf = Int.prototype.toString = function(radix){
var self = this;
if (self._nan) {
return NaN;
}
if (radix === 'number' || !radix || radix === 10) {
return (self._s && self._d.length ? '-' : '') + ((self._d.length) ? self._d.join('') : '0');
}
if (radix < 2 || radix > 36) {
throw RangeError('radix out of range: ' + radix);
}
var radix_pow = Math.pow(radix, 6);
var rem = self;
var result = '';
while (true) {
var div = rem.div(radix_pow);
var int = rem.sub(div.mul(radix_pow));
var digits = (+int.toString()).toString(radix);
rem = div;
if (rem.eq(0)) {
return digits + result;
}
else {
while (digits.length < 6) {
digits = '0' + digits;
}
result = '' + digits + result;
}
}
};
Int.prototype.gt = function (num) {
return this.cmp(num) > 0;
};
Int.prototype.gte = function (num) {
return this.cmp(num) >= 0;
};
Int.prototype.eq = function (num) {
return this.cmp(num) === 0;
};
Int.prototype.eq0 = function () {
return this._d.length === 0;
};
Int.prototype.ne = function (num) {
return this.cmp(num) !== 0;
};
Int.prototype.lt = function (num) {
return this.cmp(num) < 0;
};
Int.prototype.lte = function (num) {
return this.cmp(num) <= 0;
};
/// private api
function ensure_int(val) {
if (val instanceof Int) {
return val;
}
return Int(val);
}
/// remove leading 0's from the int
function trim_zeros(int) {
while (int._d.length && int._d[0] === 0) {
int._d.shift();
}
return int;
}
// module.exports = Int;