trackler 2.1.0.25 → 2.1.0.26

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. checksums.yaml +4 -4
  2. data/CONTRIBUTING.md +10 -0
  3. data/bin/bootstrap +6 -0
  4. data/common/CONTRIBUTING.md +2 -2
  5. data/lib/trackler/version.rb +1 -1
  6. data/tracks/coffeescript/LICENSE +2 -2
  7. data/tracks/coffeescript/README.md +0 -5
  8. data/tracks/coldfusion/LICENSE +2 -2
  9. data/tracks/coldfusion/README.md +0 -5
  10. data/tracks/crystal/LICENSE +2 -2
  11. data/tracks/crystal/README.md +0 -5
  12. data/tracks/dlang/.travis.yml +4 -2
  13. data/tracks/dlang/LICENSE +2 -2
  14. data/tracks/dlang/README.md +3 -6
  15. data/tracks/dlang/bin/test-all-exercises +20 -0
  16. data/tracks/dlang/bin/test-exercise +16 -0
  17. data/tracks/dlang/config.json +0 -18
  18. data/tracks/dlang/exercises/difference-of-squares/difference_of_squares.d +6 -3
  19. data/tracks/dlang/exercises/difference-of-squares/difference_of_squares_example.d +17 -0
  20. data/tracks/dlang/exercises/gigasecond/gigasecond.d +7 -3
  21. data/tracks/dlang/exercises/gigasecond/gigasecond_example.d +15 -0
  22. data/tracks/dlang/exercises/hello-world/hello_world.d +4 -3
  23. data/tracks/dlang/exercises/hello-world/hello_world_example.d +9 -0
  24. data/tracks/dlang/exercises/nucleotide-count/nucleotide_count.d +1 -1
  25. data/tracks/dlang/exercises/raindrops/raindrops.d +6 -3
  26. data/tracks/dlang/exercises/raindrops/raindrops_example.d +20 -0
  27. data/tracks/dlang/exercises/rna-transcription/rna_transcription.d +7 -3
  28. data/tracks/dlang/exercises/rna-transcription/rna_transcription_example.d +15 -0
  29. data/tracks/ecmascript/exercises/accumulate/package.json +25 -39
  30. data/tracks/ecmascript/exercises/acronym/package.json +25 -39
  31. data/tracks/ecmascript/exercises/all-your-base/package.json +25 -39
  32. data/tracks/ecmascript/exercises/allergies/package.json +25 -39
  33. data/tracks/ecmascript/exercises/anagram/package.json +25 -39
  34. data/tracks/ecmascript/exercises/atbash-cipher/package.json +25 -39
  35. data/tracks/ecmascript/exercises/beer-song/package.json +25 -39
  36. data/tracks/ecmascript/exercises/binary/package.json +25 -39
  37. data/tracks/ecmascript/exercises/binary-search/example.js +3 -1
  38. data/tracks/ecmascript/exercises/binary-search/package.json +25 -39
  39. data/tracks/ecmascript/exercises/binary-search-tree/package.json +25 -39
  40. data/tracks/ecmascript/exercises/bob/bob.js +1 -0
  41. data/tracks/ecmascript/exercises/bob/package.json +25 -39
  42. data/tracks/ecmascript/exercises/bracket-push/package.json +25 -39
  43. data/tracks/ecmascript/exercises/circular-buffer/package.json +25 -39
  44. data/tracks/ecmascript/exercises/clock/package.json +25 -39
  45. data/tracks/ecmascript/exercises/connect/example.js +4 -1
  46. data/tracks/ecmascript/exercises/connect/package.json +25 -39
  47. data/tracks/ecmascript/exercises/crypto-square/package.json +25 -39
  48. data/tracks/ecmascript/exercises/custom-set/package.json +25 -39
  49. data/tracks/ecmascript/exercises/diamond/example.js +3 -4
  50. data/tracks/ecmascript/exercises/diamond/package.json +25 -39
  51. data/tracks/ecmascript/exercises/difference-of-squares/package.json +25 -39
  52. data/tracks/ecmascript/exercises/etl/package.json +25 -39
  53. data/tracks/ecmascript/exercises/food-chain/package.json +25 -39
  54. data/tracks/ecmascript/exercises/gigasecond/package.json +25 -39
  55. data/tracks/ecmascript/exercises/grade-school/package.json +25 -39
  56. data/tracks/ecmascript/exercises/grains/lib/big-integer.js +1153 -1152
  57. data/tracks/ecmascript/exercises/grains/package.json +25 -39
  58. data/tracks/ecmascript/exercises/hamming/package.json +25 -39
  59. data/tracks/ecmascript/exercises/hello-world/package.json +25 -39
  60. data/tracks/ecmascript/exercises/hexadecimal/package.json +25 -39
  61. data/tracks/ecmascript/exercises/isogram/package.json +25 -39
  62. data/tracks/ecmascript/exercises/kindergarten-garden/package.json +25 -39
  63. data/tracks/ecmascript/exercises/largest-series-product/example.js +8 -2
  64. data/tracks/ecmascript/exercises/largest-series-product/package.json +25 -39
  65. data/tracks/ecmascript/exercises/leap/package.json +25 -39
  66. data/tracks/ecmascript/exercises/linked-list/package.json +25 -39
  67. data/tracks/ecmascript/exercises/luhn/package.json +25 -39
  68. data/tracks/ecmascript/exercises/matrix/package.json +25 -39
  69. data/tracks/ecmascript/exercises/meetup/example.js +3 -5
  70. data/tracks/ecmascript/exercises/meetup/meetup.spec.js +2 -5
  71. data/tracks/ecmascript/exercises/meetup/package.json +25 -39
  72. data/tracks/ecmascript/exercises/nth-prime/package.json +25 -39
  73. data/tracks/ecmascript/exercises/ocr-numbers/package.json +25 -39
  74. data/tracks/ecmascript/exercises/octal/package.json +25 -39
  75. data/tracks/ecmascript/exercises/package.json +24 -40
  76. data/tracks/ecmascript/exercises/palindrome-products/package.json +25 -39
  77. data/tracks/ecmascript/exercises/pangram/package.json +25 -39
  78. data/tracks/ecmascript/exercises/pascals-triangle/package.json +25 -39
  79. data/tracks/ecmascript/exercises/perfect-numbers/example.js +5 -7
  80. data/tracks/ecmascript/exercises/perfect-numbers/package.json +25 -39
  81. data/tracks/ecmascript/exercises/phone-number/package.json +25 -39
  82. data/tracks/ecmascript/exercises/pig-latin/package.json +25 -39
  83. data/tracks/ecmascript/exercises/prime-factors/package.json +25 -39
  84. data/tracks/ecmascript/exercises/pythagorean-triplet/package.json +25 -39
  85. data/tracks/ecmascript/exercises/queen-attack/package.json +25 -39
  86. data/tracks/ecmascript/exercises/queen-attack/queen-attack.spec.js +1 -2
  87. data/tracks/ecmascript/exercises/raindrops/package.json +25 -39
  88. data/tracks/ecmascript/exercises/rna-transcription/example.js +1 -2
  89. data/tracks/ecmascript/exercises/rna-transcription/package.json +25 -39
  90. data/tracks/ecmascript/exercises/robot-name/package.json +25 -39
  91. data/tracks/ecmascript/exercises/robot-simulator/package.json +25 -39
  92. data/tracks/ecmascript/exercises/roman-numerals/package.json +25 -39
  93. data/tracks/ecmascript/exercises/saddle-points/example.js +1 -1
  94. data/tracks/ecmascript/exercises/saddle-points/package.json +25 -39
  95. data/tracks/ecmascript/exercises/say/package.json +25 -39
  96. data/tracks/ecmascript/exercises/scrabble-score/package.json +25 -39
  97. data/tracks/ecmascript/exercises/secret-handshake/package.json +25 -39
  98. data/tracks/ecmascript/exercises/series/package.json +25 -39
  99. data/tracks/ecmascript/exercises/sieve/example.js +1 -1
  100. data/tracks/ecmascript/exercises/sieve/package.json +25 -39
  101. data/tracks/ecmascript/exercises/simple-cipher/package.json +25 -39
  102. data/tracks/ecmascript/exercises/space-age/package.json +25 -39
  103. data/tracks/ecmascript/exercises/strain/package.json +25 -39
  104. data/tracks/ecmascript/exercises/sum-of-multiples/package.json +25 -39
  105. data/tracks/ecmascript/exercises/triangle/package.json +25 -39
  106. data/tracks/ecmascript/exercises/trinary/package.json +25 -39
  107. data/tracks/ecmascript/exercises/two-bucket/package.json +25 -39
  108. data/tracks/ecmascript/exercises/word-count/package.json +25 -39
  109. data/tracks/ecmascript/exercises/wordy/package.json +25 -39
  110. data/tracks/ecmascript/package.json +25 -39
  111. data/tracks/factor/config.json +0 -6
  112. data/tracks/go/exercises/variable-length-quantity/.meta/gen.go +0 -8
  113. data/tracks/go/exercises/variable-length-quantity/cases_test.go +0 -9
  114. data/tracks/go/exercises/variable-length-quantity/example.go +5 -5
  115. data/tracks/go/exercises/variable-length-quantity/variable_length_quantity_test.go +2 -4
  116. data/tracks/groovy/LICENSE +2 -2
  117. data/tracks/groovy/README.md +0 -5
  118. data/tracks/haxe/LICENSE +2 -2
  119. data/tracks/haxe/README.md +0 -5
  120. data/tracks/nasm/LICENSE +21 -0
  121. data/tracks/nasm/README.md +1 -6
  122. data/tracks/nim/LICENSE +21 -0
  123. data/tracks/nim/README.md +0 -5
  124. data/tracks/plsql/LICENSE +2 -2
  125. data/tracks/plsql/README.md +0 -5
  126. data/tracks/plsql/config.json +52 -12
  127. data/tracks/pony/LICENSE +2 -2
  128. data/tracks/pony/README.md +0 -5
  129. data/tracks/powershell/LICENSE +21 -0
  130. data/tracks/powershell/README.md +0 -5
  131. data/tracks/prolog/LICENSE +2 -2
  132. data/tracks/prolog/README.md +1 -6
  133. data/tracks/purescript/LICENSE +2 -2
  134. data/tracks/purescript/README.md +0 -5
  135. data/tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb +1 -0
  136. data/tracks/ruby/exercises/meetup/meetup_test.rb +0 -1
  137. data/tracks/sml/LICENSE +21 -0
  138. data/tracks/sml/README.md +0 -5
  139. data/tracks/tcl/LICENSE +2 -2
  140. data/tracks/tcl/README.md +1 -6
  141. data/tracks/teco/LICENSE +2 -2
  142. data/tracks/teco/README.md +0 -5
  143. data/tracks/vbnet/LICENSE +21 -0
  144. data/tracks/vbnet/README.md +0 -5
  145. data/tracks/vimscript/config.json +5 -0
  146. data/tracks/vimscript/exercises/scrabble-score/example.vim +21 -0
  147. data/tracks/vimscript/exercises/scrabble-score/scrabble_score.vader +62 -0
  148. data/tracks/vimscript/exercises/scrabble-score/scrabble_score.vim +16 -0
  149. metadata +13 -2
@@ -1,1152 +1,1153 @@
1
- // copied from https://github.com/peterolson/BigInteger.js/blob/master/BigInteger.js
2
- // commit: https://github.com/peterolson/BigInteger.js/commit/58375195b3bc12475cb382b165ba251766a3614e#diff-82f314e495f1c3ec2a3bc0602627d16a
3
- var bigInt = (function (undefined) {
4
- "use strict";
5
-
6
- var BASE = 1e7,
7
- LOG_BASE = 7,
8
- MAX_INT = 9007199254740992,
9
- MAX_INT_ARR = smallToArray(MAX_INT),
10
- LOG_MAX_INT = Math.log(MAX_INT);
11
-
12
- function BigInteger(value, sign) {
13
- this.value = value;
14
- this.sign = sign;
15
- this.isSmall = false;
16
- }
17
-
18
- function SmallInteger(value) {
19
- this.value = value;
20
- this.sign = value < 0;
21
- this.isSmall = true;
22
- }
23
-
24
- function isPrecise(n) {
25
- return -MAX_INT < n && n < MAX_INT;
26
- }
27
-
28
- function smallToArray(n) { // For performance reasons doesn't reference BASE, need to change this function if BASE changes
29
- if (n < 1e7)
30
- return [n];
31
- if (n < 1e14)
32
- return [n % 1e7, Math.floor(n / 1e7)];
33
- return [n % 1e7, Math.floor(n / 1e7) % 1e7, Math.floor(n / 1e14)];
34
- }
35
-
36
- function arrayToSmall(arr) { // If BASE changes this function may need to change
37
- trim(arr);
38
- var length = arr.length;
39
- if (length < 4 && compareAbs(arr, MAX_INT_ARR) < 0) {
40
- switch (length) {
41
- case 0: return 0;
42
- case 1: return arr[0];
43
- case 2: return arr[0] + arr[1] * BASE;
44
- default: return arr[0] + (arr[1] + arr[2] * BASE) * BASE;
45
- }
46
- }
47
- return arr;
48
- }
49
-
50
- function trim(v) {
51
- var i = v.length;
52
- while (v[--i] === 0);
53
- v.length = i + 1;
54
- }
55
-
56
- function createArray(length) { // function shamelessly stolen from Yaffle's library https://github.com/Yaffle/BigInteger
57
- var x = new Array(length);
58
- var i = -1;
59
- while (++i < length) {
60
- x[i] = 0;
61
- }
62
- return x;
63
- }
64
-
65
- function truncate(n) {
66
- if (n > 0) return Math.floor(n);
67
- return Math.ceil(n);
68
- }
69
-
70
- function add(a, b) { // assumes a and b are arrays with a.length >= b.length
71
- var l_a = a.length,
72
- l_b = b.length,
73
- r = new Array(l_a),
74
- carry = 0,
75
- base = BASE,
76
- sum, i;
77
- for (i = 0; i < l_b; i++) {
78
- sum = a[i] + b[i] + carry;
79
- carry = sum >= base ? 1 : 0;
80
- r[i] = sum - carry * base;
81
- }
82
- while (i < l_a) {
83
- sum = a[i] + carry;
84
- carry = sum === base ? 1 : 0;
85
- r[i++] = sum - carry * base;
86
- }
87
- if (carry > 0) r.push(carry);
88
- return r;
89
- }
90
-
91
- function addAny(a, b) {
92
- if (a.length >= b.length) return add(a, b);
93
- return add(b, a);
94
- }
95
-
96
- function addSmall(a, carry) { // assumes a is array, carry is number with 0 <= carry < MAX_INT
97
- var l = a.length,
98
- r = new Array(l),
99
- base = BASE,
100
- sum, i;
101
- for (i = 0; i < l; i++) {
102
- sum = a[i] - base + carry;
103
- carry = Math.floor(sum / base);
104
- r[i] = sum - carry * base;
105
- carry += 1;
106
- }
107
- while (carry > 0) {
108
- r[i++] = carry % base;
109
- carry = Math.floor(carry / base);
110
- }
111
- return r;
112
- }
113
-
114
- BigInteger.prototype.add = function (v) {
115
- var value, n = parseValue(v);
116
- if (this.sign !== n.sign) {
117
- return this.subtract(n.negate());
118
- }
119
- var a = this.value, b = n.value;
120
- if (n.isSmall) {
121
- return new BigInteger(addSmall(a, Math.abs(b)), this.sign);
122
- }
123
- return new BigInteger(addAny(a, b), this.sign);
124
- };
125
- BigInteger.prototype.plus = BigInteger.prototype.add;
126
-
127
- SmallInteger.prototype.add = function (v) {
128
- var n = parseValue(v);
129
- var a = this.value;
130
- if (a < 0 !== n.sign) {
131
- return this.subtract(n.negate());
132
- }
133
- var b = n.value;
134
- if (n.isSmall) {
135
- if (isPrecise(a + b)) return new SmallInteger(a + b);
136
- b = smallToArray(Math.abs(b));
137
- }
138
- return new BigInteger(addSmall(b, Math.abs(a)), a < 0);
139
- };
140
- SmallInteger.prototype.plus = SmallInteger.prototype.add;
141
-
142
- function subtract(a, b) { // assumes a and b are arrays with a >= b
143
- var a_l = a.length,
144
- b_l = b.length,
145
- r = new Array(a_l),
146
- borrow = 0,
147
- base = BASE,
148
- i, difference;
149
- for (i = 0; i < b_l; i++) {
150
- difference = a[i] - borrow - b[i];
151
- if (difference < 0) {
152
- difference += base;
153
- borrow = 1;
154
- } else borrow = 0;
155
- r[i] = difference;
156
- }
157
- for (i = b_l; i < a_l; i++) {
158
- difference = a[i] - borrow;
159
- if (difference < 0) difference += base;
160
- else {
161
- r[i++] = difference;
162
- break;
163
- }
164
- r[i] = difference;
165
- }
166
- for (; i < a_l; i++) {
167
- r[i] = a[i];
168
- }
169
- trim(r);
170
- return r;
171
- }
172
-
173
- function subtractAny(a, b, sign) {
174
- var value, isSmall;
175
- if (compareAbs(a, b) >= 0) {
176
- value = subtract(a,b);
177
- } else {
178
- value = subtract(b, a);
179
- sign = !sign;
180
- }
181
- value = arrayToSmall(value);
182
- if (typeof value === "number") {
183
- if (sign) value = -value;
184
- return new SmallInteger(value);
185
- }
186
- return new BigInteger(value, sign);
187
- }
188
-
189
- function subtractSmall(a, b, sign) { // assumes a is array, b is number with 0 <= b < MAX_INT
190
- var l = a.length,
191
- r = new Array(l),
192
- carry = -b,
193
- base = BASE,
194
- i, difference;
195
- for (i = 0; i < l; i++) {
196
- difference = a[i] + carry;
197
- carry = Math.floor(difference / base);
198
- difference %= base;
199
- r[i] = difference < 0 ? difference + base : difference;
200
- }
201
- r = arrayToSmall(r);
202
- if (typeof r === "number") {
203
- if (sign) r = -r;
204
- return new SmallInteger(r);
205
- } return new BigInteger(r, sign);
206
- }
207
-
208
- BigInteger.prototype.subtract = function (v) {
209
- var n = parseValue(v);
210
- if (this.sign !== n.sign) {
211
- return this.add(n.negate());
212
- }
213
- var a = this.value, b = n.value;
214
- if (n.isSmall)
215
- return subtractSmall(a, Math.abs(b), this.sign);
216
- return subtractAny(a, b, this.sign);
217
- };
218
- BigInteger.prototype.minus = BigInteger.prototype.subtract;
219
-
220
- SmallInteger.prototype.subtract = function (v) {
221
- var n = parseValue(v);
222
- var a = this.value;
223
- if (a < 0 !== n.sign) {
224
- return this.add(n.negate());
225
- }
226
- var b = n.value;
227
- if (n.isSmall) {
228
- return new SmallInteger(a - b);
229
- }
230
- return subtractSmall(b, Math.abs(a), a >= 0);
231
- };
232
- SmallInteger.prototype.minus = SmallInteger.prototype.subtract;
233
-
234
- BigInteger.prototype.negate = function () {
235
- return new BigInteger(this.value, !this.sign);
236
- };
237
- SmallInteger.prototype.negate = function () {
238
- var sign = this.sign;
239
- var small = new SmallInteger(-this.value);
240
- small.sign = !sign;
241
- return small;
242
- };
243
-
244
- BigInteger.prototype.abs = function () {
245
- return new BigInteger(this.value, false);
246
- };
247
- SmallInteger.prototype.abs = function () {
248
- return new SmallInteger(Math.abs(this.value));
249
- };
250
-
251
- function multiplyLong(a, b) {
252
- var a_l = a.length,
253
- b_l = b.length,
254
- l = a_l + b_l,
255
- r = createArray(l),
256
- base = BASE,
257
- product, carry, i, a_i, b_j;
258
- for (i = 0; i < a_l; ++i) {
259
- a_i = a[i];
260
- for (var j = 0; j < b_l; ++j) {
261
- b_j = b[j];
262
- product = a_i * b_j + r[i + j];
263
- carry = Math.floor(product / base);
264
- r[i + j] = product - carry * base;
265
- r[i + j + 1] += carry;
266
- }
267
- }
268
- trim(r);
269
- return r;
270
- }
271
-
272
- function multiplySmall(a, b) { // assumes a is array, b is number with |b| < BASE
273
- var l = a.length,
274
- r = new Array(l),
275
- base = BASE,
276
- carry = 0,
277
- product, i;
278
- for (i = 0; i < l; i++) {
279
- product = a[i] * b + carry;
280
- carry = Math.floor(product / base);
281
- r[i] = product - carry * base;
282
- }
283
- while (carry > 0) {
284
- r[i++] = carry % base;
285
- carry = Math.floor(carry / base);
286
- }
287
- return r;
288
- }
289
-
290
- function shiftLeft(x, n) {
291
- var r = [];
292
- while (n-- > 0) r.push(0);
293
- return r.concat(x);
294
- }
295
-
296
- function multiplyKaratsuba(x, y) {
297
- var n = Math.max(x.length, y.length);
298
-
299
- if (n <= 400) return multiplyLong(x, y);
300
- n = Math.ceil(n / 2);
301
-
302
- var b = x.slice(n),
303
- a = x.slice(0, n),
304
- d = y.slice(n),
305
- c = y.slice(0, n);
306
-
307
- var ac = multiplyKaratsuba(a, c),
308
- bd = multiplyKaratsuba(b, d),
309
- abcd = multiplyKaratsuba(addAny(a, b), addAny(c, d));
310
-
311
- return addAny(addAny(ac, shiftLeft(subtract(subtract(abcd, ac), bd), n)), shiftLeft(bd, 2 * n));
312
- }
313
-
314
- BigInteger.prototype.multiply = function (v) {
315
- var value, n = parseValue(v),
316
- a = this.value, b = n.value,
317
- sign = this.sign !== n.sign,
318
- abs;
319
- if (n.isSmall) {
320
- if (b === 0) return CACHE[0];
321
- if (b === 1) return this;
322
- if (b === -1) return this.negate();
323
- abs = Math.abs(b);
324
- if (abs < BASE) {
325
- return new BigInteger(multiplySmall(a, abs), sign);
326
- }
327
- b = smallToArray(abs);
328
- }
329
- if (a.length + b.length > 4000) // Karatsuba is only faster for sufficiently large inputs
330
- return new BigInteger(multiplyKaratsuba(a, b), sign);
331
- return new BigInteger(multiplyLong(a, b), sign);
332
- };
333
-
334
- BigInteger.prototype.times = BigInteger.prototype.multiply;
335
-
336
- function multiplySmallAndArray(a, b, sign) { // a >= 0
337
- if (a < BASE) {
338
- return new BigInteger(multiplySmall(b, a), sign);
339
- }
340
- return new BigInteger(multiplyLong(b, smallToArray(a)), sign);
341
- }
342
- SmallInteger.prototype["_multiplyBySmall"] = function (a) {
343
- if (isPrecise(a.value * this.value)) {
344
- return new SmallInteger(a.value * this.value);
345
- }
346
- return multiplySmallAndArray(Math.abs(a.value), smallToArray(Math.abs(this.value)), this.sign !== a.sign);
347
- };
348
- BigInteger.prototype["_multiplyBySmall"] = function (a) {
349
- if (a.value === 0) return CACHE[0];
350
- if (a.value === 1) return this;
351
- if (a.value === -1) return this.negate();
352
- return multiplySmallAndArray(Math.abs(a.value), this.value, this.sign !== a.sign);
353
- };
354
- SmallInteger.prototype.multiply = function (v) {
355
- return parseValue(v)["_multiplyBySmall"](this);
356
- };
357
- SmallInteger.prototype.times = SmallInteger.prototype.multiply;
358
-
359
- function square(a) {
360
- var l = a.length,
361
- r = createArray(l + l),
362
- base = BASE,
363
- product, carry, i, a_i, a_j;
364
- for (i = 0; i < l; i++) {
365
- a_i = a[i];
366
- for (var j = 0; j < l; j++) {
367
- a_j = a[j];
368
- product = a_i * a_j + r[i + j];
369
- carry = Math.floor(product / base);
370
- r[i + j] = product - carry * base;
371
- r[i + j + 1] += carry;
372
- }
373
- }
374
- trim(r);
375
- return r;
376
- }
377
-
378
- BigInteger.prototype.square = function () {
379
- return new BigInteger(square(this.value), false);
380
- };
381
-
382
- SmallInteger.prototype.square = function () {
383
- var value = this.value * this.value;
384
- if (isPrecise(value)) return new SmallInteger(value);
385
- return new BigInteger(square(smallToArray(Math.abs(this.value))), false);
386
- };
387
-
388
- function divMod1(a, b) { // Left over from previous version. Performs faster than divMod2 on smaller input sizes.
389
- var a_l = a.length,
390
- b_l = b.length,
391
- base = BASE,
392
- result = createArray(b.length),
393
- divisorMostSignificantDigit = b[b_l - 1],
394
- // normalization
395
- lambda = Math.ceil(base / (2 * divisorMostSignificantDigit)),
396
- remainder = multiplySmall(a, lambda),
397
- divisor = multiplySmall(b, lambda),
398
- quotientDigit, shift, carry, borrow, i, l, q;
399
- if (remainder.length <= a_l) remainder.push(0);
400
- divisor.push(0);
401
- divisorMostSignificantDigit = divisor[b_l - 1];
402
- for (shift = a_l - b_l; shift >= 0; shift--) {
403
- quotientDigit = base - 1;
404
- quotientDigit = Math.floor((remainder[shift + b_l] * base + remainder[shift + b_l - 1]) / divisorMostSignificantDigit);
405
- carry = 0;
406
- borrow = 0;
407
- l = divisor.length;
408
- for (i = 0; i < l; i++) {
409
- carry += quotientDigit * divisor[i];
410
- q = Math.floor(carry / base);
411
- borrow += remainder[shift + i] - (carry - q * base);
412
- carry = q;
413
- if (borrow < 0) {
414
- remainder[shift + i] = borrow + base;
415
- borrow = -1;
416
- } else {
417
- remainder[shift + i] = borrow;
418
- borrow = 0;
419
- }
420
- }
421
- while (borrow !== 0) {
422
- quotientDigit -= 1;
423
- carry = 0;
424
- for (i = 0; i < l; i++) {
425
- carry += remainder[shift + i] - base + divisor[i];
426
- if (carry < 0) {
427
- remainder[shift + i] = carry + base;
428
- carry = 0;
429
- } else {
430
- remainder[shift + i] = carry;
431
- carry = 1;
432
- }
433
- }
434
- borrow += carry;
435
- }
436
- result[shift] = quotientDigit;
437
- }
438
- // denormalization
439
- remainder = divModSmall(remainder, lambda)[0];
440
- return [arrayToSmall(result), arrayToSmall(remainder)];
441
- }
442
-
443
- function divMod2(a, b) { // Implementation idea shamelessly stolen from Silent Matt's library http://silentmatt.com/biginteger/
444
- // Performs faster than divMod1 on larger input sizes.
445
- var a_l = a.length,
446
- b_l = b.length,
447
- result = [],
448
- part = [],
449
- base = BASE,
450
- guess, xlen, highx, highy, check;
451
- while (a_l) {
452
- part.unshift(a[--a_l]);
453
- if (compareAbs(part, b) < 0) {
454
- result.push(0);
455
- continue;
456
- }
457
- xlen = part.length;
458
- highx = part[xlen - 1] * base + part[xlen - 2];
459
- highy = b[b_l - 1] * base + b[b_l - 2];
460
- if (xlen > b_l) {
461
- highx = (highx + 1) * base;
462
- }
463
- guess = Math.ceil(highx / highy);
464
- do {
465
- check = multiplySmall(b, guess);
466
- if (compareAbs(check, part) <= 0) break;
467
- guess--;
468
- } while (guess);
469
- result.push(guess);
470
- part = subtract(part, check);
471
- }
472
- result.reverse();
473
- return [arrayToSmall(result), arrayToSmall(part)];
474
- }
475
-
476
- function divModSmall(value, lambda) {
477
- var length = value.length,
478
- quotient = createArray(length),
479
- base = BASE,
480
- i, q, remainder, divisor;
481
- remainder = 0;
482
- for (i = length - 1; i >= 0; --i) {
483
- divisor = remainder * base + value[i];
484
- q = truncate(divisor / lambda);
485
- remainder = divisor - q * lambda;
486
- quotient[i] = q | 0;
487
- }
488
- return [quotient, remainder | 0];
489
- }
490
-
491
- function divModAny(self, v) {
492
- var value, n = parseValue(v);
493
- var a = self.value, b = n.value;
494
- var quotient;
495
- if (b === 0) throw new Error("Cannot divide by zero");
496
- if (self.isSmall) {
497
- if (n.isSmall) {
498
- return [new SmallInteger(truncate(a / b)), new SmallInteger(a % b)];
499
- }
500
- return [CACHE[0], self];
501
- }
502
- if (n.isSmall) {
503
- if (b === 1) return [self, CACHE[0]];
504
- if (b == -1) return [self.negate(), CACHE[0]];
505
- var abs = Math.abs(b);
506
- if (abs < BASE) {
507
- value = divModSmall(a, abs);
508
- quotient = arrayToSmall(value[0]);
509
- var remainder = value[1];
510
- if (self.sign) remainder = -remainder;
511
- if (typeof quotient === "number") {
512
- if (self.sign !== n.sign) quotient = -quotient;
513
- return [new SmallInteger(quotient), new SmallInteger(remainder)];
514
- }
515
- return [new BigInteger(quotient, self.sign !== n.sign), new SmallInteger(remainder)];
516
- }
517
- b = smallToArray(abs);
518
- }
519
- var comparison = compareAbs(a, b);
520
- if (comparison === -1) return [CACHE[0], self];
521
- if (comparison === 0) return [CACHE[self.sign === n.sign ? 1 : -1], CACHE[0]];
522
-
523
- // divMod1 is faster on smaller input sizes
524
- if (a.length + b.length <= 200)
525
- value = divMod1(a, b);
526
- else value = divMod2(a, b);
527
-
528
- quotient = value[0];
529
- var qSign = self.sign !== n.sign,
530
- mod = value[1],
531
- mSign = self.sign;
532
- if (typeof quotient === "number") {
533
- if (qSign) quotient = -quotient;
534
- quotient = new SmallInteger(quotient);
535
- } else quotient = new BigInteger(quotient, qSign);
536
- if (typeof mod === "number") {
537
- if (mSign) mod = -mod;
538
- mod = new SmallInteger(mod);
539
- } else mod = new BigInteger(mod, mSign);
540
- return [quotient, mod];
541
- }
542
-
543
- BigInteger.prototype.divmod = function (v) {
544
- var result = divModAny(this, v);
545
- return {
546
- quotient: result[0],
547
- remainder: result[1]
548
- };
549
- };
550
- SmallInteger.prototype.divmod = BigInteger.prototype.divmod;
551
-
552
- BigInteger.prototype.divide = function (v) {
553
- return divModAny(this, v)[0];
554
- };
555
- SmallInteger.prototype.over = SmallInteger.prototype.divide = BigInteger.prototype.over = BigInteger.prototype.divide;
556
-
557
- BigInteger.prototype.mod = function (v) {
558
- return divModAny(this, v)[1];
559
- };
560
- SmallInteger.prototype.remainder = SmallInteger.prototype.mod = BigInteger.prototype.remainder = BigInteger.prototype.mod;
561
-
562
- BigInteger.prototype.pow = function (v) {
563
- var n = parseValue(v),
564
- a = this.value,
565
- b = n.value,
566
- value, x, y;
567
- if (b === 0) return CACHE[1];
568
- if (a === 0) return CACHE[0];
569
- if (a === 1) return CACHE[1];
570
- if (a === -1) return n.isEven() ? CACHE[1] : CACHE[-1];
571
- if (n.sign) {
572
- return CACHE[0];
573
- }
574
- if (!n.isSmall) throw new Error("The exponent " + n.toString() + " is too large.");
575
- if (this.isSmall) {
576
- if (isPrecise(value = Math.pow(a, b)))
577
- return new SmallInteger(truncate(value));
578
- }
579
- x = this;
580
- y = CACHE[1];
581
- while (true) {
582
- if (b & 1 === 1) {
583
- y = y.times(x);
584
- --b;
585
- }
586
- if (b === 0) break;
587
- b /= 2;
588
- x = x.square();
589
- }
590
- return y;
591
- };
592
- SmallInteger.prototype.pow = BigInteger.prototype.pow;
593
-
594
- BigInteger.prototype.modPow = function (exp, mod) {
595
- exp = parseValue(exp);
596
- mod = parseValue(mod);
597
- if (mod.isZero()) throw new Error("Cannot take modPow with modulus 0");
598
- var r = CACHE[1],
599
- base = this.mod(mod);
600
- if (base.isZero()) return CACHE[0];
601
- while (exp.isPositive()) {
602
- if (exp.isOdd()) r = r.multiply(base).mod(mod);
603
- exp = exp.divide(2);
604
- base = base.square().mod(mod);
605
- }
606
- return r;
607
- };
608
- SmallInteger.prototype.modPow = BigInteger.prototype.modPow;
609
-
610
- function compareAbs(a, b) {
611
- if (a.length !== b.length) {
612
- return a.length > b.length ? 1 : -1;
613
- }
614
- for (var i = a.length - 1; i >= 0; i--) {
615
- if (a[i] !== b[i]) return a[i] > b[i] ? 1 : -1;
616
- }
617
- return 0;
618
- }
619
-
620
- BigInteger.prototype.compareAbs = function (v) {
621
- var n = parseValue(v),
622
- a = this.value,
623
- b = n.value;
624
- if (n.isSmall) return 1;
625
- return compareAbs(a, b);
626
- };
627
- SmallInteger.prototype.compareAbs = function (v) {
628
- var n = parseValue(v),
629
- a = Math.abs(this.value),
630
- b = n.value;
631
- if (n.isSmall) {
632
- b = Math.abs(b);
633
- return a === b ? 0 : a > b ? 1 : -1;
634
- }
635
- return -1;
636
- };
637
-
638
- BigInteger.prototype.compare = function (v) {
639
- var n = parseValue(v),
640
- a = this.value,
641
- b = n.value;
642
- if (this.sign !== n.sign) {
643
- return n.sign ? 1 : -1;
644
- }
645
- if (n.isSmall) {
646
- return this.sign ? -1 : 1;
647
- }
648
- return compareAbs(a, b) * (this.sign ? -1 : 1);
649
- };
650
- BigInteger.prototype.compareTo = BigInteger.prototype.compare;
651
-
652
- SmallInteger.prototype.compare = function (v) {
653
- var n = parseValue(v),
654
- a = this.value,
655
- b = n.value;
656
- if (n.isSmall) {
657
- return a == b ? 0 : a > b ? 1 : -1;
658
- }
659
- if (a < 0 !== n.sign) {
660
- return a < 0 ? -1 : 1;
661
- }
662
- return a < 0 ? 1 : -1;
663
- };
664
- SmallInteger.prototype.compareTo = SmallInteger.prototype.compare;
665
-
666
- BigInteger.prototype.equals = function (v) {
667
- return this.compare(v) === 0;
668
- };
669
- SmallInteger.prototype.eq = SmallInteger.prototype.equals = BigInteger.prototype.eq = BigInteger.prototype.equals;
670
-
671
- BigInteger.prototype.notEquals = function (v) {
672
- return this.compare(v) !== 0;
673
- };
674
- SmallInteger.prototype.neq = SmallInteger.prototype.notEquals = BigInteger.prototype.neq = BigInteger.prototype.notEquals;
675
-
676
- BigInteger.prototype.greater = function (v) {
677
- return this.compare(v) > 0;
678
- };
679
- SmallInteger.prototype.gt = SmallInteger.prototype.greater = BigInteger.prototype.gt = BigInteger.prototype.greater;
680
-
681
- BigInteger.prototype.lesser = function (v) {
682
- return this.compare(v) < 0;
683
- };
684
- SmallInteger.prototype.lt = SmallInteger.prototype.lesser = BigInteger.prototype.lt = BigInteger.prototype.lesser;
685
-
686
- BigInteger.prototype.greaterOrEquals = function (v) {
687
- return this.compare(v) >= 0;
688
- };
689
- SmallInteger.prototype.geq = SmallInteger.prototype.greaterOrEquals = BigInteger.prototype.geq = BigInteger.prototype.greaterOrEquals;
690
-
691
- BigInteger.prototype.lesserOrEquals = function (v) {
692
- return this.compare(v) <= 0;
693
- };
694
- SmallInteger.prototype.leq = SmallInteger.prototype.lesserOrEquals = BigInteger.prototype.leq = BigInteger.prototype.lesserOrEquals;
695
-
696
- BigInteger.prototype.isEven = function () {
697
- return (this.value[0] & 1) === 0;
698
- };
699
- SmallInteger.prototype.isEven = function () {
700
- return (this.value & 1) === 0;
701
- };
702
-
703
- BigInteger.prototype.isOdd = function () {
704
- return (this.value[0] & 1) === 1;
705
- };
706
- SmallInteger.prototype.isOdd = function () {
707
- return (this.value & 1) === 1;
708
- };
709
-
710
- BigInteger.prototype.isPositive = function () {
711
- return !this.sign;
712
- };
713
- SmallInteger.prototype.isPositive = function () {
714
- return this.value > 0;
715
- };
716
-
717
- BigInteger.prototype.isNegative = function () {
718
- return this.sign;
719
- };
720
- SmallInteger.prototype.isNegative = function () {
721
- return this.value < 0;
722
- };
723
-
724
- BigInteger.prototype.isUnit = function () {
725
- return false;
726
- };
727
- SmallInteger.prototype.isUnit = function () {
728
- return Math.abs(this.value) === 1;
729
- };
730
-
731
- BigInteger.prototype.isZero = function () {
732
- return false;
733
- };
734
- SmallInteger.prototype.isZero = function () {
735
- return this.value === 0;
736
- };
737
- BigInteger.prototype.isDivisibleBy = function (v) {
738
- var n = parseValue(v);
739
- var value = n.value;
740
- if (value === 0) return false;
741
- if (value === 1) return true;
742
- if (value === 2) return this.isEven();
743
- return this.mod(n).equals(CACHE[0]);
744
- };
745
- SmallInteger.prototype.isDivisibleBy = BigInteger.prototype.isDivisibleBy;
746
-
747
- function isBasicPrime(v) {
748
- var n = v.abs();
749
- if (n.isUnit()) return false;
750
- if (n.equals(2) || n.equals(3) || n.equals(5)) return true;
751
- if (n.isEven() || n.isDivisibleBy(3) || n.isDivisibleBy(5)) return false;
752
- if (n.lesser(25)) return true;
753
- // we don't know if it's prime: let the other functions figure it out
754
- };
755
-
756
- BigInteger.prototype.isPrime = function () {
757
- var isPrime = isBasicPrime(this);
758
- if (isPrime !== undefined) return isPrime;
759
- var n = this.abs(),
760
- nPrev = n.prev();
761
- var a = [2, 3, 5, 7, 11, 13, 17, 19],
762
- b = nPrev,
763
- d, t, i, x;
764
- while (b.isEven()) b = b.divide(2);
765
- for (i = 0; i < a.length; i++) {
766
- x = bigInt(a[i]).modPow(b, n);
767
- if (x.equals(CACHE[1]) || x.equals(nPrev)) continue;
768
- for (t = true, d = b; t && d.lesser(nPrev) ; d = d.multiply(2)) {
769
- x = x.square().mod(n);
770
- if (x.equals(nPrev)) t = false;
771
- }
772
- if (t) return false;
773
- }
774
- return true;
775
- };
776
- SmallInteger.prototype.isPrime = BigInteger.prototype.isPrime;
777
-
778
- BigInteger.prototype.isProbablePrime = function (iterations) {
779
- var isPrime = isBasicPrime(this);
780
- if (isPrime !== undefined) return isPrime;
781
- var n = this.abs();
782
- var t = iterations === undefined ? 5 : iterations;
783
- // use the Fermat primality test
784
- for (var i = 0; i < t; i++) {
785
- var a = bigInt.randBetween(2, n.minus(2));
786
- if (!a.modPow(n.prev(), n).isUnit()) return false; // definitely composite
787
- }
788
- return true; // large chance of being prime
789
- };
790
- SmallInteger.prototype.isProbablePrime = BigInteger.prototype.isProbablePrime;
791
-
792
- BigInteger.prototype.next = function () {
793
- var value = this.value;
794
- if (this.sign) {
795
- return subtractSmall(value, 1, this.sign);
796
- }
797
- return new BigInteger(addSmall(value, 1), this.sign);
798
- };
799
- SmallInteger.prototype.next = function () {
800
- var value = this.value;
801
- if (value + 1 < MAX_INT) return new SmallInteger(value + 1);
802
- return new BigInteger(MAX_INT_ARR, false);
803
- };
804
-
805
- BigInteger.prototype.prev = function () {
806
- var value = this.value;
807
- if (this.sign) {
808
- return new BigInteger(addSmall(value, 1), true);
809
- }
810
- return subtractSmall(value, 1, this.sign);
811
- };
812
- SmallInteger.prototype.prev = function () {
813
- var value = this.value;
814
- if (value - 1 > -MAX_INT) return new SmallInteger(value - 1);
815
- return new BigInteger(MAX_INT_ARR, true);
816
- };
817
-
818
- var powersOfTwo = [1];
819
- while (powersOfTwo[powersOfTwo.length - 1] <= BASE) powersOfTwo.push(2 * powersOfTwo[powersOfTwo.length - 1]);
820
- var powers2Length = powersOfTwo.length, highestPower2 = powersOfTwo[powers2Length - 1];
821
-
822
- function shift_isSmall(n) {
823
- return ((typeof n === "number" || typeof n === "string") && +Math.abs(n) <= BASE) ||
824
- (n instanceof BigInteger && n.value.length <= 1);
825
- }
826
-
827
- BigInteger.prototype.shiftLeft = function (n) {
828
- if (!shift_isSmall(n)) {
829
- if (n.isNegative()) return this.shiftRight(n.abs());
830
- return this.times(CACHE[2].pow(n));
831
- }
832
- n = +n;
833
- if (n < 0) return this.shiftRight(-n);
834
- var result = this;
835
- while (n >= powers2Length) {
836
- result = result.multiply(highestPower2);
837
- n -= powers2Length - 1;
838
- }
839
- return result.multiply(powersOfTwo[n]);
840
- };
841
- SmallInteger.prototype.shiftLeft = BigInteger.prototype.shiftLeft;
842
-
843
- BigInteger.prototype.shiftRight = function (n) {
844
- var remQuo;
845
- if (!shift_isSmall(n)) {
846
- if (n.isNegative()) return this.shiftLeft(n.abs());
847
- remQuo = this.divmod(CACHE[2].pow(n));
848
- return remQuo.remainder.isNegative() ? remQuo.quotient.prev() : remQuo.quotient;
849
- }
850
- n = +n;
851
- if (n < 0) return this.shiftLeft(-n);
852
- var result = this;
853
- while (n >= powers2Length) {
854
- if (result.isZero()) return result;
855
- remQuo = divModAny(result, highestPower2);
856
- result = remQuo[1].isNegative() ? remQuo[0].prev() : remQuo[0];
857
- n -= powers2Length - 1;
858
- }
859
- remQuo = divModAny(result, powersOfTwo[n]);
860
- return remQuo[1].isNegative() ? remQuo[0].prev() : remQuo[0];
861
- };
862
- SmallInteger.prototype.shiftRight = BigInteger.prototype.shiftRight;
863
-
864
- function bitwise(x, y, fn) {
865
- y = parseValue(y);
866
- var xSign = x.isNegative(), ySign = y.isNegative();
867
- var xRem = xSign ? x.not() : x,
868
- yRem = ySign ? y.not() : y;
869
- var xBits = [], yBits = [];
870
- var xStop = false, yStop = false;
871
- while (!xStop || !yStop) {
872
- if (xRem.isZero()) { // virtual sign extension for simulating two's complement
873
- xStop = true;
874
- xBits.push(xSign ? 1 : 0);
875
- }
876
- else if (xSign) xBits.push(xRem.isEven() ? 1 : 0); // two's complement for negative numbers
877
- else xBits.push(xRem.isEven() ? 0 : 1);
878
-
879
- if (yRem.isZero()) {
880
- yStop = true;
881
- yBits.push(ySign ? 1 : 0);
882
- }
883
- else if (ySign) yBits.push(yRem.isEven() ? 1 : 0);
884
- else yBits.push(yRem.isEven() ? 0 : 1);
885
-
886
- xRem = xRem.over(2);
887
- yRem = yRem.over(2);
888
- }
889
- var result = [];
890
- for (var i = 0; i < xBits.length; i++) result.push(fn(xBits[i], yBits[i]));
891
- var sum = bigInt(result.pop()).negate().times(bigInt(2).pow(result.length));
892
- while (result.length) {
893
- sum = sum.add(bigInt(result.pop()).times(bigInt(2).pow(result.length)));
894
- }
895
- return sum;
896
- }
897
-
898
- BigInteger.prototype.not = function () {
899
- return this.negate().prev();
900
- };
901
- SmallInteger.prototype.not = BigInteger.prototype.not;
902
-
903
- BigInteger.prototype.and = function (n) {
904
- return bitwise(this, n, function (a, b) { return a & b; });
905
- };
906
- SmallInteger.prototype.and = BigInteger.prototype.and;
907
-
908
- BigInteger.prototype.or = function (n) {
909
- return bitwise(this, n, function (a, b) { return a | b; });
910
- };
911
- SmallInteger.prototype.or = BigInteger.prototype.or;
912
-
913
- BigInteger.prototype.xor = function (n) {
914
- return bitwise(this, n, function (a, b) { return a ^ b; });
915
- };
916
- SmallInteger.prototype.xor = BigInteger.prototype.xor;
917
-
918
- function max(a, b) {
919
- a = parseValue(a);
920
- b = parseValue(b);
921
- return a.greater(b) ? a : b;
922
- }
923
- function min(a,b) {
924
- a = parseValue(a);
925
- b = parseValue(b);
926
- return a.lesser(b) ? a : b;
927
- }
928
- function gcd(a, b) {
929
- a = parseValue(a).abs();
930
- b = parseValue(b).abs();
931
- if (a.equals(b)) return a;
932
- if (a.isZero()) return b;
933
- if (b.isZero()) return a;
934
- if (a.isEven()) {
935
- if (b.isOdd()) {
936
- return gcd(a.divide(2), b);
937
- }
938
- return gcd(a.divide(2), b.divide(2)).multiply(2);
939
- }
940
- if (b.isEven()) {
941
- return gcd(a, b.divide(2));
942
- }
943
- if (a.greater(b)) {
944
- return gcd(a.subtract(b).divide(2), b);
945
- }
946
- return gcd(b.subtract(a).divide(2), a);
947
- }
948
- function lcm(a, b) {
949
- a = parseValue(a).abs();
950
- b = parseValue(b).abs();
951
- return a.multiply(b).divide(gcd(a, b));
952
- }
953
- function randBetween(a, b) {
954
- a = parseValue(a);
955
- b = parseValue(b);
956
- var low = min(a, b), high = max(a, b);
957
- var range = high.subtract(low);
958
- if (range.isSmall) return low.add(Math.round(Math.random() * range));
959
- var length = range.value.length - 1;
960
- var result = [], restricted = true;
961
- for (var i = length; i >= 0; i--) {
962
- var top = restricted ? range.value[i] : BASE;
963
- var digit = truncate(Math.random() * top);
964
- result.unshift(digit);
965
- if (digit < top) restricted = false;
966
- }
967
- result = arrayToSmall(result);
968
- return low.add(new BigInteger(result, false, typeof result === "number"));
969
- }
970
- var parseBase = function (text, base) {
971
- var val = CACHE[0], pow = CACHE[1],
972
- length = text.length;
973
- if (2 <= base && base <= 36) {
974
- if (length <= LOG_MAX_INT / Math.log(base)) {
975
- return new SmallInteger(parseInt(text, base));
976
- }
977
- }
978
- base = parseValue(base);
979
- var digits = [];
980
- var i;
981
- var isNegative = text[0] === "-";
982
- for (i = isNegative ? 1 : 0; i < text.length; i++) {
983
- var c = text[i].toLowerCase(),
984
- charCode = c.charCodeAt(0);
985
- if (48 <= charCode && charCode <= 57) digits.push(parseValue(c));
986
- else if (97 <= charCode && charCode <= 122) digits.push(parseValue(c.charCodeAt(0) - 87));
987
- else if (c === "<") {
988
- var start = i;
989
- do { i++; } while (text[i] !== ">");
990
- digits.push(parseValue(text.slice(start + 1, i)));
991
- }
992
- else throw new Error(c + " is not a valid character");
993
- }
994
- digits.reverse();
995
- for (i = 0; i < digits.length; i++) {
996
- val = val.add(digits[i].times(pow));
997
- pow = pow.times(base);
998
- }
999
- return isNegative ? val.negate() : val;
1000
- };
1001
-
1002
- function stringify(digit) {
1003
- var v = digit.value;
1004
- if (typeof v === "number") v = [v];
1005
- if (v.length === 1 && v[0] <= 36) {
1006
- return "0123456789abcdefghijklmnopqrstuvwxyz".charAt(v[0]);
1007
- }
1008
- return "<" + v + ">";
1009
- }
1010
- function toBase(n, base) {
1011
- base = bigInt(base);
1012
- if (base.isZero()) {
1013
- if (n.isZero()) return "0";
1014
- throw new Error("Cannot convert nonzero numbers to base 0.");
1015
- }
1016
- if (base.equals(-1)) {
1017
- if (n.isZero()) return "0";
1018
- if (n.isNegative()) return new Array(1 - n).join("10");
1019
- return "1" + new Array(+n).join("01");
1020
- }
1021
- var minusSign = "";
1022
- if (n.isNegative() && base.isPositive()) {
1023
- minusSign = "-";
1024
- n = n.abs();
1025
- }
1026
- if (base.equals(1)) {
1027
- if (n.isZero()) return "0";
1028
- return minusSign + new Array(+n + 1).join(1);
1029
- }
1030
- var out = [];
1031
- var left = n, divmod;
1032
- while (left.isNegative() || left.compareAbs(base) >= 0) {
1033
- divmod = left.divmod(base);
1034
- left = divmod.quotient;
1035
- var digit = divmod.remainder;
1036
- if (digit.isNegative()) {
1037
- digit = base.minus(digit).abs();
1038
- left = left.next();
1039
- }
1040
- out.push(stringify(digit));
1041
- }
1042
- out.push(stringify(left));
1043
- return minusSign + out.reverse().join("");
1044
- }
1045
-
1046
- BigInteger.prototype.toString = function (radix) {
1047
- if (radix === undefined) radix = 10;
1048
- if (radix !== 10) return toBase(this, radix);
1049
- var v = this.value, l = v.length, str = String(v[--l]), zeros = "0000000", digit;
1050
- while (--l >= 0) {
1051
- digit = String(v[l]);
1052
- str += zeros.slice(digit.length) + digit;
1053
- }
1054
- var sign = this.sign ? "-" : "";
1055
- return sign + str;
1056
- };
1057
- SmallInteger.prototype.toString = function (radix) {
1058
- if (radix === undefined) radix = 10;
1059
- if (radix != 10) return toBase(this, radix);
1060
- return String(this.value);
1061
- };
1062
-
1063
- BigInteger.prototype.valueOf = function () {
1064
- return +this.toString();
1065
- };
1066
- BigInteger.prototype.toJSNumber = BigInteger.prototype.valueOf;
1067
-
1068
- SmallInteger.prototype.valueOf = function () {
1069
- return this.value;
1070
- };
1071
- SmallInteger.prototype.toJSNumber = SmallInteger.prototype.valueOf;
1072
-
1073
- function parseStringValue(v) {
1074
- if (isPrecise(+v)) {
1075
- var x = +v;
1076
- if (x === truncate(x))
1077
- return new SmallInteger(x);
1078
- throw "Invalid integer: " + v;
1079
- }
1080
- var sign = v[0] === "-";
1081
- if (sign) v = v.slice(1);
1082
- var split = v.split(/e/i);
1083
- if (split.length > 2) throw new Error("Invalid integer: " + text.join("e"));
1084
- if (split.length === 2) {
1085
- var exp = split[1];
1086
- if (exp[0] === "+") exp = exp.slice(1);
1087
- exp = +exp;
1088
- if (exp !== truncate(exp) || !isPrecise(exp)) throw new Error("Invalid integer: " + exp + " is not a valid exponent.");
1089
- var text = split[0];
1090
- var decimalPlace = text.indexOf(".");
1091
- if (decimalPlace >= 0) {
1092
- exp -= text.length - decimalPlace;
1093
- text = text.slice(0, decimalPlace) + text.slice(decimalPlace + 1);
1094
- }
1095
- if (exp < 0) throw new Error("Cannot include negative exponent part for integers");
1096
- text += (new Array(exp + 1)).join("0");
1097
- v = text;
1098
- }
1099
- var isValid = /^([0-9][0-9]*)$/.test(v);
1100
- if (!isValid) throw new Error("Invalid integer: " + v);
1101
- var r = [], max = v.length, l = LOG_BASE, min = max - l;
1102
- while (max > 0) {
1103
- r.push(+v.slice(min, max));
1104
- min -= l;
1105
- if (min < 0) min = 0;
1106
- max -= l;
1107
- }
1108
- trim(r);
1109
- return new BigInteger(r, sign);
1110
- }
1111
-
1112
- function parseNumberValue(v) {
1113
- if (isPrecise(v)) return new SmallInteger(v);
1114
- return parseStringValue(v.toString());
1115
- }
1116
-
1117
- function parseValue(v) {
1118
- if (typeof v === "number") {
1119
- return parseNumberValue(v);
1120
- }
1121
- if (typeof v === "string") {
1122
- return parseStringValue(v);
1123
- }
1124
- return v;
1125
- }
1126
- // Pre-define numbers in range [-999,999]
1127
- var CACHE = function (v, radix) {
1128
- if (typeof v === "undefined") return CACHE[0];
1129
- if (typeof radix !== "undefined") return +radix === 10 ? parseValue(v) : parseBase(v, radix);
1130
- return parseValue(v);
1131
- };
1132
- for (var i = 0; i < 1000; i++) {
1133
- CACHE[i] = new SmallInteger(i);
1134
- if (i > 0) CACHE[-i] = new SmallInteger(-i);
1135
- }
1136
- // Backwards compatibility
1137
- CACHE.one = CACHE[1];
1138
- CACHE.zero = CACHE[0];
1139
- CACHE.minusOne = CACHE[-1];
1140
- CACHE.max = max;
1141
- CACHE.min = min;
1142
- CACHE.gcd = gcd;
1143
- CACHE.lcm = lcm;
1144
- CACHE.isInstance = function (x) { return x instanceof BigInteger || x instanceof SmallInteger; };
1145
- CACHE.randBetween = randBetween;
1146
- return CACHE;
1147
- })();
1148
-
1149
- // Node.js check
1150
- if (typeof module !== "undefined" && module.hasOwnProperty("exports")) {
1151
- module.exports = bigInt;
1152
- }
1
+ /* eslint-disable */
2
+ // copied from https://github.com/peterolson/BigInteger.js/blob/master/BigInteger.js
3
+ // commit: https://github.com/peterolson/BigInteger.js/commit/58375195b3bc12475cb382b165ba251766a3614e#diff-82f314e495f1c3ec2a3bc0602627d16a
4
+ var bigInt = (function (undefined) {
5
+ "use strict";
6
+
7
+ var BASE = 1e7,
8
+ LOG_BASE = 7,
9
+ MAX_INT = 9007199254740992,
10
+ MAX_INT_ARR = smallToArray(MAX_INT),
11
+ LOG_MAX_INT = Math.log(MAX_INT);
12
+
13
+ function BigInteger(value, sign) {
14
+ this.value = value;
15
+ this.sign = sign;
16
+ this.isSmall = false;
17
+ }
18
+
19
+ function SmallInteger(value) {
20
+ this.value = value;
21
+ this.sign = value < 0;
22
+ this.isSmall = true;
23
+ }
24
+
25
+ function isPrecise(n) {
26
+ return -MAX_INT < n && n < MAX_INT;
27
+ }
28
+
29
+ function smallToArray(n) { // For performance reasons doesn't reference BASE, need to change this function if BASE changes
30
+ if (n < 1e7)
31
+ return [n];
32
+ if (n < 1e14)
33
+ return [n % 1e7, Math.floor(n / 1e7)];
34
+ return [n % 1e7, Math.floor(n / 1e7) % 1e7, Math.floor(n / 1e14)];
35
+ }
36
+
37
+ function arrayToSmall(arr) { // If BASE changes this function may need to change
38
+ trim(arr);
39
+ var length = arr.length;
40
+ if (length < 4 && compareAbs(arr, MAX_INT_ARR) < 0) {
41
+ switch (length) {
42
+ case 0: return 0;
43
+ case 1: return arr[0];
44
+ case 2: return arr[0] + arr[1] * BASE;
45
+ default: return arr[0] + (arr[1] + arr[2] * BASE) * BASE;
46
+ }
47
+ }
48
+ return arr;
49
+ }
50
+
51
+ function trim(v) {
52
+ var i = v.length;
53
+ while (v[--i] === 0);
54
+ v.length = i + 1;
55
+ }
56
+
57
+ function createArray(length) { // function shamelessly stolen from Yaffle's library https://github.com/Yaffle/BigInteger
58
+ var x = new Array(length);
59
+ var i = -1;
60
+ while (++i < length) {
61
+ x[i] = 0;
62
+ }
63
+ return x;
64
+ }
65
+
66
+ function truncate(n) {
67
+ if (n > 0) return Math.floor(n);
68
+ return Math.ceil(n);
69
+ }
70
+
71
+ function add(a, b) { // assumes a and b are arrays with a.length >= b.length
72
+ var l_a = a.length,
73
+ l_b = b.length,
74
+ r = new Array(l_a),
75
+ carry = 0,
76
+ base = BASE,
77
+ sum, i;
78
+ for (i = 0; i < l_b; i++) {
79
+ sum = a[i] + b[i] + carry;
80
+ carry = sum >= base ? 1 : 0;
81
+ r[i] = sum - carry * base;
82
+ }
83
+ while (i < l_a) {
84
+ sum = a[i] + carry;
85
+ carry = sum === base ? 1 : 0;
86
+ r[i++] = sum - carry * base;
87
+ }
88
+ if (carry > 0) r.push(carry);
89
+ return r;
90
+ }
91
+
92
+ function addAny(a, b) {
93
+ if (a.length >= b.length) return add(a, b);
94
+ return add(b, a);
95
+ }
96
+
97
+ function addSmall(a, carry) { // assumes a is array, carry is number with 0 <= carry < MAX_INT
98
+ var l = a.length,
99
+ r = new Array(l),
100
+ base = BASE,
101
+ sum, i;
102
+ for (i = 0; i < l; i++) {
103
+ sum = a[i] - base + carry;
104
+ carry = Math.floor(sum / base);
105
+ r[i] = sum - carry * base;
106
+ carry += 1;
107
+ }
108
+ while (carry > 0) {
109
+ r[i++] = carry % base;
110
+ carry = Math.floor(carry / base);
111
+ }
112
+ return r;
113
+ }
114
+
115
+ BigInteger.prototype.add = function (v) {
116
+ var value, n = parseValue(v);
117
+ if (this.sign !== n.sign) {
118
+ return this.subtract(n.negate());
119
+ }
120
+ var a = this.value, b = n.value;
121
+ if (n.isSmall) {
122
+ return new BigInteger(addSmall(a, Math.abs(b)), this.sign);
123
+ }
124
+ return new BigInteger(addAny(a, b), this.sign);
125
+ };
126
+ BigInteger.prototype.plus = BigInteger.prototype.add;
127
+
128
+ SmallInteger.prototype.add = function (v) {
129
+ var n = parseValue(v);
130
+ var a = this.value;
131
+ if (a < 0 !== n.sign) {
132
+ return this.subtract(n.negate());
133
+ }
134
+ var b = n.value;
135
+ if (n.isSmall) {
136
+ if (isPrecise(a + b)) return new SmallInteger(a + b);
137
+ b = smallToArray(Math.abs(b));
138
+ }
139
+ return new BigInteger(addSmall(b, Math.abs(a)), a < 0);
140
+ };
141
+ SmallInteger.prototype.plus = SmallInteger.prototype.add;
142
+
143
+ function subtract(a, b) { // assumes a and b are arrays with a >= b
144
+ var a_l = a.length,
145
+ b_l = b.length,
146
+ r = new Array(a_l),
147
+ borrow = 0,
148
+ base = BASE,
149
+ i, difference;
150
+ for (i = 0; i < b_l; i++) {
151
+ difference = a[i] - borrow - b[i];
152
+ if (difference < 0) {
153
+ difference += base;
154
+ borrow = 1;
155
+ } else borrow = 0;
156
+ r[i] = difference;
157
+ }
158
+ for (i = b_l; i < a_l; i++) {
159
+ difference = a[i] - borrow;
160
+ if (difference < 0) difference += base;
161
+ else {
162
+ r[i++] = difference;
163
+ break;
164
+ }
165
+ r[i] = difference;
166
+ }
167
+ for (; i < a_l; i++) {
168
+ r[i] = a[i];
169
+ }
170
+ trim(r);
171
+ return r;
172
+ }
173
+
174
+ function subtractAny(a, b, sign) {
175
+ var value, isSmall;
176
+ if (compareAbs(a, b) >= 0) {
177
+ value = subtract(a,b);
178
+ } else {
179
+ value = subtract(b, a);
180
+ sign = !sign;
181
+ }
182
+ value = arrayToSmall(value);
183
+ if (typeof value === "number") {
184
+ if (sign) value = -value;
185
+ return new SmallInteger(value);
186
+ }
187
+ return new BigInteger(value, sign);
188
+ }
189
+
190
+ function subtractSmall(a, b, sign) { // assumes a is array, b is number with 0 <= b < MAX_INT
191
+ var l = a.length,
192
+ r = new Array(l),
193
+ carry = -b,
194
+ base = BASE,
195
+ i, difference;
196
+ for (i = 0; i < l; i++) {
197
+ difference = a[i] + carry;
198
+ carry = Math.floor(difference / base);
199
+ difference %= base;
200
+ r[i] = difference < 0 ? difference + base : difference;
201
+ }
202
+ r = arrayToSmall(r);
203
+ if (typeof r === "number") {
204
+ if (sign) r = -r;
205
+ return new SmallInteger(r);
206
+ } return new BigInteger(r, sign);
207
+ }
208
+
209
+ BigInteger.prototype.subtract = function (v) {
210
+ var n = parseValue(v);
211
+ if (this.sign !== n.sign) {
212
+ return this.add(n.negate());
213
+ }
214
+ var a = this.value, b = n.value;
215
+ if (n.isSmall)
216
+ return subtractSmall(a, Math.abs(b), this.sign);
217
+ return subtractAny(a, b, this.sign);
218
+ };
219
+ BigInteger.prototype.minus = BigInteger.prototype.subtract;
220
+
221
+ SmallInteger.prototype.subtract = function (v) {
222
+ var n = parseValue(v);
223
+ var a = this.value;
224
+ if (a < 0 !== n.sign) {
225
+ return this.add(n.negate());
226
+ }
227
+ var b = n.value;
228
+ if (n.isSmall) {
229
+ return new SmallInteger(a - b);
230
+ }
231
+ return subtractSmall(b, Math.abs(a), a >= 0);
232
+ };
233
+ SmallInteger.prototype.minus = SmallInteger.prototype.subtract;
234
+
235
+ BigInteger.prototype.negate = function () {
236
+ return new BigInteger(this.value, !this.sign);
237
+ };
238
+ SmallInteger.prototype.negate = function () {
239
+ var sign = this.sign;
240
+ var small = new SmallInteger(-this.value);
241
+ small.sign = !sign;
242
+ return small;
243
+ };
244
+
245
+ BigInteger.prototype.abs = function () {
246
+ return new BigInteger(this.value, false);
247
+ };
248
+ SmallInteger.prototype.abs = function () {
249
+ return new SmallInteger(Math.abs(this.value));
250
+ };
251
+
252
+ function multiplyLong(a, b) {
253
+ var a_l = a.length,
254
+ b_l = b.length,
255
+ l = a_l + b_l,
256
+ r = createArray(l),
257
+ base = BASE,
258
+ product, carry, i, a_i, b_j;
259
+ for (i = 0; i < a_l; ++i) {
260
+ a_i = a[i];
261
+ for (var j = 0; j < b_l; ++j) {
262
+ b_j = b[j];
263
+ product = a_i * b_j + r[i + j];
264
+ carry = Math.floor(product / base);
265
+ r[i + j] = product - carry * base;
266
+ r[i + j + 1] += carry;
267
+ }
268
+ }
269
+ trim(r);
270
+ return r;
271
+ }
272
+
273
+ function multiplySmall(a, b) { // assumes a is array, b is number with |b| < BASE
274
+ var l = a.length,
275
+ r = new Array(l),
276
+ base = BASE,
277
+ carry = 0,
278
+ product, i;
279
+ for (i = 0; i < l; i++) {
280
+ product = a[i] * b + carry;
281
+ carry = Math.floor(product / base);
282
+ r[i] = product - carry * base;
283
+ }
284
+ while (carry > 0) {
285
+ r[i++] = carry % base;
286
+ carry = Math.floor(carry / base);
287
+ }
288
+ return r;
289
+ }
290
+
291
+ function shiftLeft(x, n) {
292
+ var r = [];
293
+ while (n-- > 0) r.push(0);
294
+ return r.concat(x);
295
+ }
296
+
297
+ function multiplyKaratsuba(x, y) {
298
+ var n = Math.max(x.length, y.length);
299
+
300
+ if (n <= 400) return multiplyLong(x, y);
301
+ n = Math.ceil(n / 2);
302
+
303
+ var b = x.slice(n),
304
+ a = x.slice(0, n),
305
+ d = y.slice(n),
306
+ c = y.slice(0, n);
307
+
308
+ var ac = multiplyKaratsuba(a, c),
309
+ bd = multiplyKaratsuba(b, d),
310
+ abcd = multiplyKaratsuba(addAny(a, b), addAny(c, d));
311
+
312
+ return addAny(addAny(ac, shiftLeft(subtract(subtract(abcd, ac), bd), n)), shiftLeft(bd, 2 * n));
313
+ }
314
+
315
+ BigInteger.prototype.multiply = function (v) {
316
+ var value, n = parseValue(v),
317
+ a = this.value, b = n.value,
318
+ sign = this.sign !== n.sign,
319
+ abs;
320
+ if (n.isSmall) {
321
+ if (b === 0) return CACHE[0];
322
+ if (b === 1) return this;
323
+ if (b === -1) return this.negate();
324
+ abs = Math.abs(b);
325
+ if (abs < BASE) {
326
+ return new BigInteger(multiplySmall(a, abs), sign);
327
+ }
328
+ b = smallToArray(abs);
329
+ }
330
+ if (a.length + b.length > 4000) // Karatsuba is only faster for sufficiently large inputs
331
+ return new BigInteger(multiplyKaratsuba(a, b), sign);
332
+ return new BigInteger(multiplyLong(a, b), sign);
333
+ };
334
+
335
+ BigInteger.prototype.times = BigInteger.prototype.multiply;
336
+
337
+ function multiplySmallAndArray(a, b, sign) { // a >= 0
338
+ if (a < BASE) {
339
+ return new BigInteger(multiplySmall(b, a), sign);
340
+ }
341
+ return new BigInteger(multiplyLong(b, smallToArray(a)), sign);
342
+ }
343
+ SmallInteger.prototype["_multiplyBySmall"] = function (a) {
344
+ if (isPrecise(a.value * this.value)) {
345
+ return new SmallInteger(a.value * this.value);
346
+ }
347
+ return multiplySmallAndArray(Math.abs(a.value), smallToArray(Math.abs(this.value)), this.sign !== a.sign);
348
+ };
349
+ BigInteger.prototype["_multiplyBySmall"] = function (a) {
350
+ if (a.value === 0) return CACHE[0];
351
+ if (a.value === 1) return this;
352
+ if (a.value === -1) return this.negate();
353
+ return multiplySmallAndArray(Math.abs(a.value), this.value, this.sign !== a.sign);
354
+ };
355
+ SmallInteger.prototype.multiply = function (v) {
356
+ return parseValue(v)["_multiplyBySmall"](this);
357
+ };
358
+ SmallInteger.prototype.times = SmallInteger.prototype.multiply;
359
+
360
+ function square(a) {
361
+ var l = a.length,
362
+ r = createArray(l + l),
363
+ base = BASE,
364
+ product, carry, i, a_i, a_j;
365
+ for (i = 0; i < l; i++) {
366
+ a_i = a[i];
367
+ for (var j = 0; j < l; j++) {
368
+ a_j = a[j];
369
+ product = a_i * a_j + r[i + j];
370
+ carry = Math.floor(product / base);
371
+ r[i + j] = product - carry * base;
372
+ r[i + j + 1] += carry;
373
+ }
374
+ }
375
+ trim(r);
376
+ return r;
377
+ }
378
+
379
+ BigInteger.prototype.square = function () {
380
+ return new BigInteger(square(this.value), false);
381
+ };
382
+
383
+ SmallInteger.prototype.square = function () {
384
+ var value = this.value * this.value;
385
+ if (isPrecise(value)) return new SmallInteger(value);
386
+ return new BigInteger(square(smallToArray(Math.abs(this.value))), false);
387
+ };
388
+
389
+ function divMod1(a, b) { // Left over from previous version. Performs faster than divMod2 on smaller input sizes.
390
+ var a_l = a.length,
391
+ b_l = b.length,
392
+ base = BASE,
393
+ result = createArray(b.length),
394
+ divisorMostSignificantDigit = b[b_l - 1],
395
+ // normalization
396
+ lambda = Math.ceil(base / (2 * divisorMostSignificantDigit)),
397
+ remainder = multiplySmall(a, lambda),
398
+ divisor = multiplySmall(b, lambda),
399
+ quotientDigit, shift, carry, borrow, i, l, q;
400
+ if (remainder.length <= a_l) remainder.push(0);
401
+ divisor.push(0);
402
+ divisorMostSignificantDigit = divisor[b_l - 1];
403
+ for (shift = a_l - b_l; shift >= 0; shift--) {
404
+ quotientDigit = base - 1;
405
+ quotientDigit = Math.floor((remainder[shift + b_l] * base + remainder[shift + b_l - 1]) / divisorMostSignificantDigit);
406
+ carry = 0;
407
+ borrow = 0;
408
+ l = divisor.length;
409
+ for (i = 0; i < l; i++) {
410
+ carry += quotientDigit * divisor[i];
411
+ q = Math.floor(carry / base);
412
+ borrow += remainder[shift + i] - (carry - q * base);
413
+ carry = q;
414
+ if (borrow < 0) {
415
+ remainder[shift + i] = borrow + base;
416
+ borrow = -1;
417
+ } else {
418
+ remainder[shift + i] = borrow;
419
+ borrow = 0;
420
+ }
421
+ }
422
+ while (borrow !== 0) {
423
+ quotientDigit -= 1;
424
+ carry = 0;
425
+ for (i = 0; i < l; i++) {
426
+ carry += remainder[shift + i] - base + divisor[i];
427
+ if (carry < 0) {
428
+ remainder[shift + i] = carry + base;
429
+ carry = 0;
430
+ } else {
431
+ remainder[shift + i] = carry;
432
+ carry = 1;
433
+ }
434
+ }
435
+ borrow += carry;
436
+ }
437
+ result[shift] = quotientDigit;
438
+ }
439
+ // denormalization
440
+ remainder = divModSmall(remainder, lambda)[0];
441
+ return [arrayToSmall(result), arrayToSmall(remainder)];
442
+ }
443
+
444
+ function divMod2(a, b) { // Implementation idea shamelessly stolen from Silent Matt's library http://silentmatt.com/biginteger/
445
+ // Performs faster than divMod1 on larger input sizes.
446
+ var a_l = a.length,
447
+ b_l = b.length,
448
+ result = [],
449
+ part = [],
450
+ base = BASE,
451
+ guess, xlen, highx, highy, check;
452
+ while (a_l) {
453
+ part.unshift(a[--a_l]);
454
+ if (compareAbs(part, b) < 0) {
455
+ result.push(0);
456
+ continue;
457
+ }
458
+ xlen = part.length;
459
+ highx = part[xlen - 1] * base + part[xlen - 2];
460
+ highy = b[b_l - 1] * base + b[b_l - 2];
461
+ if (xlen > b_l) {
462
+ highx = (highx + 1) * base;
463
+ }
464
+ guess = Math.ceil(highx / highy);
465
+ do {
466
+ check = multiplySmall(b, guess);
467
+ if (compareAbs(check, part) <= 0) break;
468
+ guess--;
469
+ } while (guess);
470
+ result.push(guess);
471
+ part = subtract(part, check);
472
+ }
473
+ result.reverse();
474
+ return [arrayToSmall(result), arrayToSmall(part)];
475
+ }
476
+
477
+ function divModSmall(value, lambda) {
478
+ var length = value.length,
479
+ quotient = createArray(length),
480
+ base = BASE,
481
+ i, q, remainder, divisor;
482
+ remainder = 0;
483
+ for (i = length - 1; i >= 0; --i) {
484
+ divisor = remainder * base + value[i];
485
+ q = truncate(divisor / lambda);
486
+ remainder = divisor - q * lambda;
487
+ quotient[i] = q | 0;
488
+ }
489
+ return [quotient, remainder | 0];
490
+ }
491
+
492
+ function divModAny(self, v) {
493
+ var value, n = parseValue(v);
494
+ var a = self.value, b = n.value;
495
+ var quotient;
496
+ if (b === 0) throw new Error("Cannot divide by zero");
497
+ if (self.isSmall) {
498
+ if (n.isSmall) {
499
+ return [new SmallInteger(truncate(a / b)), new SmallInteger(a % b)];
500
+ }
501
+ return [CACHE[0], self];
502
+ }
503
+ if (n.isSmall) {
504
+ if (b === 1) return [self, CACHE[0]];
505
+ if (b == -1) return [self.negate(), CACHE[0]];
506
+ var abs = Math.abs(b);
507
+ if (abs < BASE) {
508
+ value = divModSmall(a, abs);
509
+ quotient = arrayToSmall(value[0]);
510
+ var remainder = value[1];
511
+ if (self.sign) remainder = -remainder;
512
+ if (typeof quotient === "number") {
513
+ if (self.sign !== n.sign) quotient = -quotient;
514
+ return [new SmallInteger(quotient), new SmallInteger(remainder)];
515
+ }
516
+ return [new BigInteger(quotient, self.sign !== n.sign), new SmallInteger(remainder)];
517
+ }
518
+ b = smallToArray(abs);
519
+ }
520
+ var comparison = compareAbs(a, b);
521
+ if (comparison === -1) return [CACHE[0], self];
522
+ if (comparison === 0) return [CACHE[self.sign === n.sign ? 1 : -1], CACHE[0]];
523
+
524
+ // divMod1 is faster on smaller input sizes
525
+ if (a.length + b.length <= 200)
526
+ value = divMod1(a, b);
527
+ else value = divMod2(a, b);
528
+
529
+ quotient = value[0];
530
+ var qSign = self.sign !== n.sign,
531
+ mod = value[1],
532
+ mSign = self.sign;
533
+ if (typeof quotient === "number") {
534
+ if (qSign) quotient = -quotient;
535
+ quotient = new SmallInteger(quotient);
536
+ } else quotient = new BigInteger(quotient, qSign);
537
+ if (typeof mod === "number") {
538
+ if (mSign) mod = -mod;
539
+ mod = new SmallInteger(mod);
540
+ } else mod = new BigInteger(mod, mSign);
541
+ return [quotient, mod];
542
+ }
543
+
544
+ BigInteger.prototype.divmod = function (v) {
545
+ var result = divModAny(this, v);
546
+ return {
547
+ quotient: result[0],
548
+ remainder: result[1]
549
+ };
550
+ };
551
+ SmallInteger.prototype.divmod = BigInteger.prototype.divmod;
552
+
553
+ BigInteger.prototype.divide = function (v) {
554
+ return divModAny(this, v)[0];
555
+ };
556
+ SmallInteger.prototype.over = SmallInteger.prototype.divide = BigInteger.prototype.over = BigInteger.prototype.divide;
557
+
558
+ BigInteger.prototype.mod = function (v) {
559
+ return divModAny(this, v)[1];
560
+ };
561
+ SmallInteger.prototype.remainder = SmallInteger.prototype.mod = BigInteger.prototype.remainder = BigInteger.prototype.mod;
562
+
563
+ BigInteger.prototype.pow = function (v) {
564
+ var n = parseValue(v),
565
+ a = this.value,
566
+ b = n.value,
567
+ value, x, y;
568
+ if (b === 0) return CACHE[1];
569
+ if (a === 0) return CACHE[0];
570
+ if (a === 1) return CACHE[1];
571
+ if (a === -1) return n.isEven() ? CACHE[1] : CACHE[-1];
572
+ if (n.sign) {
573
+ return CACHE[0];
574
+ }
575
+ if (!n.isSmall) throw new Error("The exponent " + n.toString() + " is too large.");
576
+ if (this.isSmall) {
577
+ if (isPrecise(value = Math.pow(a, b)))
578
+ return new SmallInteger(truncate(value));
579
+ }
580
+ x = this;
581
+ y = CACHE[1];
582
+ while (true) {
583
+ if (b & 1 === 1) {
584
+ y = y.times(x);
585
+ --b;
586
+ }
587
+ if (b === 0) break;
588
+ b /= 2;
589
+ x = x.square();
590
+ }
591
+ return y;
592
+ };
593
+ SmallInteger.prototype.pow = BigInteger.prototype.pow;
594
+
595
+ BigInteger.prototype.modPow = function (exp, mod) {
596
+ exp = parseValue(exp);
597
+ mod = parseValue(mod);
598
+ if (mod.isZero()) throw new Error("Cannot take modPow with modulus 0");
599
+ var r = CACHE[1],
600
+ base = this.mod(mod);
601
+ if (base.isZero()) return CACHE[0];
602
+ while (exp.isPositive()) {
603
+ if (exp.isOdd()) r = r.multiply(base).mod(mod);
604
+ exp = exp.divide(2);
605
+ base = base.square().mod(mod);
606
+ }
607
+ return r;
608
+ };
609
+ SmallInteger.prototype.modPow = BigInteger.prototype.modPow;
610
+
611
+ function compareAbs(a, b) {
612
+ if (a.length !== b.length) {
613
+ return a.length > b.length ? 1 : -1;
614
+ }
615
+ for (var i = a.length - 1; i >= 0; i--) {
616
+ if (a[i] !== b[i]) return a[i] > b[i] ? 1 : -1;
617
+ }
618
+ return 0;
619
+ }
620
+
621
+ BigInteger.prototype.compareAbs = function (v) {
622
+ var n = parseValue(v),
623
+ a = this.value,
624
+ b = n.value;
625
+ if (n.isSmall) return 1;
626
+ return compareAbs(a, b);
627
+ };
628
+ SmallInteger.prototype.compareAbs = function (v) {
629
+ var n = parseValue(v),
630
+ a = Math.abs(this.value),
631
+ b = n.value;
632
+ if (n.isSmall) {
633
+ b = Math.abs(b);
634
+ return a === b ? 0 : a > b ? 1 : -1;
635
+ }
636
+ return -1;
637
+ };
638
+
639
+ BigInteger.prototype.compare = function (v) {
640
+ var n = parseValue(v),
641
+ a = this.value,
642
+ b = n.value;
643
+ if (this.sign !== n.sign) {
644
+ return n.sign ? 1 : -1;
645
+ }
646
+ if (n.isSmall) {
647
+ return this.sign ? -1 : 1;
648
+ }
649
+ return compareAbs(a, b) * (this.sign ? -1 : 1);
650
+ };
651
+ BigInteger.prototype.compareTo = BigInteger.prototype.compare;
652
+
653
+ SmallInteger.prototype.compare = function (v) {
654
+ var n = parseValue(v),
655
+ a = this.value,
656
+ b = n.value;
657
+ if (n.isSmall) {
658
+ return a == b ? 0 : a > b ? 1 : -1;
659
+ }
660
+ if (a < 0 !== n.sign) {
661
+ return a < 0 ? -1 : 1;
662
+ }
663
+ return a < 0 ? 1 : -1;
664
+ };
665
+ SmallInteger.prototype.compareTo = SmallInteger.prototype.compare;
666
+
667
+ BigInteger.prototype.equals = function (v) {
668
+ return this.compare(v) === 0;
669
+ };
670
+ SmallInteger.prototype.eq = SmallInteger.prototype.equals = BigInteger.prototype.eq = BigInteger.prototype.equals;
671
+
672
+ BigInteger.prototype.notEquals = function (v) {
673
+ return this.compare(v) !== 0;
674
+ };
675
+ SmallInteger.prototype.neq = SmallInteger.prototype.notEquals = BigInteger.prototype.neq = BigInteger.prototype.notEquals;
676
+
677
+ BigInteger.prototype.greater = function (v) {
678
+ return this.compare(v) > 0;
679
+ };
680
+ SmallInteger.prototype.gt = SmallInteger.prototype.greater = BigInteger.prototype.gt = BigInteger.prototype.greater;
681
+
682
+ BigInteger.prototype.lesser = function (v) {
683
+ return this.compare(v) < 0;
684
+ };
685
+ SmallInteger.prototype.lt = SmallInteger.prototype.lesser = BigInteger.prototype.lt = BigInteger.prototype.lesser;
686
+
687
+ BigInteger.prototype.greaterOrEquals = function (v) {
688
+ return this.compare(v) >= 0;
689
+ };
690
+ SmallInteger.prototype.geq = SmallInteger.prototype.greaterOrEquals = BigInteger.prototype.geq = BigInteger.prototype.greaterOrEquals;
691
+
692
+ BigInteger.prototype.lesserOrEquals = function (v) {
693
+ return this.compare(v) <= 0;
694
+ };
695
+ SmallInteger.prototype.leq = SmallInteger.prototype.lesserOrEquals = BigInteger.prototype.leq = BigInteger.prototype.lesserOrEquals;
696
+
697
+ BigInteger.prototype.isEven = function () {
698
+ return (this.value[0] & 1) === 0;
699
+ };
700
+ SmallInteger.prototype.isEven = function () {
701
+ return (this.value & 1) === 0;
702
+ };
703
+
704
+ BigInteger.prototype.isOdd = function () {
705
+ return (this.value[0] & 1) === 1;
706
+ };
707
+ SmallInteger.prototype.isOdd = function () {
708
+ return (this.value & 1) === 1;
709
+ };
710
+
711
+ BigInteger.prototype.isPositive = function () {
712
+ return !this.sign;
713
+ };
714
+ SmallInteger.prototype.isPositive = function () {
715
+ return this.value > 0;
716
+ };
717
+
718
+ BigInteger.prototype.isNegative = function () {
719
+ return this.sign;
720
+ };
721
+ SmallInteger.prototype.isNegative = function () {
722
+ return this.value < 0;
723
+ };
724
+
725
+ BigInteger.prototype.isUnit = function () {
726
+ return false;
727
+ };
728
+ SmallInteger.prototype.isUnit = function () {
729
+ return Math.abs(this.value) === 1;
730
+ };
731
+
732
+ BigInteger.prototype.isZero = function () {
733
+ return false;
734
+ };
735
+ SmallInteger.prototype.isZero = function () {
736
+ return this.value === 0;
737
+ };
738
+ BigInteger.prototype.isDivisibleBy = function (v) {
739
+ var n = parseValue(v);
740
+ var value = n.value;
741
+ if (value === 0) return false;
742
+ if (value === 1) return true;
743
+ if (value === 2) return this.isEven();
744
+ return this.mod(n).equals(CACHE[0]);
745
+ };
746
+ SmallInteger.prototype.isDivisibleBy = BigInteger.prototype.isDivisibleBy;
747
+
748
+ function isBasicPrime(v) {
749
+ var n = v.abs();
750
+ if (n.isUnit()) return false;
751
+ if (n.equals(2) || n.equals(3) || n.equals(5)) return true;
752
+ if (n.isEven() || n.isDivisibleBy(3) || n.isDivisibleBy(5)) return false;
753
+ if (n.lesser(25)) return true;
754
+ // we don't know if it's prime: let the other functions figure it out
755
+ };
756
+
757
+ BigInteger.prototype.isPrime = function () {
758
+ var isPrime = isBasicPrime(this);
759
+ if (isPrime !== undefined) return isPrime;
760
+ var n = this.abs(),
761
+ nPrev = n.prev();
762
+ var a = [2, 3, 5, 7, 11, 13, 17, 19],
763
+ b = nPrev,
764
+ d, t, i, x;
765
+ while (b.isEven()) b = b.divide(2);
766
+ for (i = 0; i < a.length; i++) {
767
+ x = bigInt(a[i]).modPow(b, n);
768
+ if (x.equals(CACHE[1]) || x.equals(nPrev)) continue;
769
+ for (t = true, d = b; t && d.lesser(nPrev) ; d = d.multiply(2)) {
770
+ x = x.square().mod(n);
771
+ if (x.equals(nPrev)) t = false;
772
+ }
773
+ if (t) return false;
774
+ }
775
+ return true;
776
+ };
777
+ SmallInteger.prototype.isPrime = BigInteger.prototype.isPrime;
778
+
779
+ BigInteger.prototype.isProbablePrime = function (iterations) {
780
+ var isPrime = isBasicPrime(this);
781
+ if (isPrime !== undefined) return isPrime;
782
+ var n = this.abs();
783
+ var t = iterations === undefined ? 5 : iterations;
784
+ // use the Fermat primality test
785
+ for (var i = 0; i < t; i++) {
786
+ var a = bigInt.randBetween(2, n.minus(2));
787
+ if (!a.modPow(n.prev(), n).isUnit()) return false; // definitely composite
788
+ }
789
+ return true; // large chance of being prime
790
+ };
791
+ SmallInteger.prototype.isProbablePrime = BigInteger.prototype.isProbablePrime;
792
+
793
+ BigInteger.prototype.next = function () {
794
+ var value = this.value;
795
+ if (this.sign) {
796
+ return subtractSmall(value, 1, this.sign);
797
+ }
798
+ return new BigInteger(addSmall(value, 1), this.sign);
799
+ };
800
+ SmallInteger.prototype.next = function () {
801
+ var value = this.value;
802
+ if (value + 1 < MAX_INT) return new SmallInteger(value + 1);
803
+ return new BigInteger(MAX_INT_ARR, false);
804
+ };
805
+
806
+ BigInteger.prototype.prev = function () {
807
+ var value = this.value;
808
+ if (this.sign) {
809
+ return new BigInteger(addSmall(value, 1), true);
810
+ }
811
+ return subtractSmall(value, 1, this.sign);
812
+ };
813
+ SmallInteger.prototype.prev = function () {
814
+ var value = this.value;
815
+ if (value - 1 > -MAX_INT) return new SmallInteger(value - 1);
816
+ return new BigInteger(MAX_INT_ARR, true);
817
+ };
818
+
819
+ var powersOfTwo = [1];
820
+ while (powersOfTwo[powersOfTwo.length - 1] <= BASE) powersOfTwo.push(2 * powersOfTwo[powersOfTwo.length - 1]);
821
+ var powers2Length = powersOfTwo.length, highestPower2 = powersOfTwo[powers2Length - 1];
822
+
823
+ function shift_isSmall(n) {
824
+ return ((typeof n === "number" || typeof n === "string") && +Math.abs(n) <= BASE) ||
825
+ (n instanceof BigInteger && n.value.length <= 1);
826
+ }
827
+
828
+ BigInteger.prototype.shiftLeft = function (n) {
829
+ if (!shift_isSmall(n)) {
830
+ if (n.isNegative()) return this.shiftRight(n.abs());
831
+ return this.times(CACHE[2].pow(n));
832
+ }
833
+ n = +n;
834
+ if (n < 0) return this.shiftRight(-n);
835
+ var result = this;
836
+ while (n >= powers2Length) {
837
+ result = result.multiply(highestPower2);
838
+ n -= powers2Length - 1;
839
+ }
840
+ return result.multiply(powersOfTwo[n]);
841
+ };
842
+ SmallInteger.prototype.shiftLeft = BigInteger.prototype.shiftLeft;
843
+
844
+ BigInteger.prototype.shiftRight = function (n) {
845
+ var remQuo;
846
+ if (!shift_isSmall(n)) {
847
+ if (n.isNegative()) return this.shiftLeft(n.abs());
848
+ remQuo = this.divmod(CACHE[2].pow(n));
849
+ return remQuo.remainder.isNegative() ? remQuo.quotient.prev() : remQuo.quotient;
850
+ }
851
+ n = +n;
852
+ if (n < 0) return this.shiftLeft(-n);
853
+ var result = this;
854
+ while (n >= powers2Length) {
855
+ if (result.isZero()) return result;
856
+ remQuo = divModAny(result, highestPower2);
857
+ result = remQuo[1].isNegative() ? remQuo[0].prev() : remQuo[0];
858
+ n -= powers2Length - 1;
859
+ }
860
+ remQuo = divModAny(result, powersOfTwo[n]);
861
+ return remQuo[1].isNegative() ? remQuo[0].prev() : remQuo[0];
862
+ };
863
+ SmallInteger.prototype.shiftRight = BigInteger.prototype.shiftRight;
864
+
865
+ function bitwise(x, y, fn) {
866
+ y = parseValue(y);
867
+ var xSign = x.isNegative(), ySign = y.isNegative();
868
+ var xRem = xSign ? x.not() : x,
869
+ yRem = ySign ? y.not() : y;
870
+ var xBits = [], yBits = [];
871
+ var xStop = false, yStop = false;
872
+ while (!xStop || !yStop) {
873
+ if (xRem.isZero()) { // virtual sign extension for simulating two's complement
874
+ xStop = true;
875
+ xBits.push(xSign ? 1 : 0);
876
+ }
877
+ else if (xSign) xBits.push(xRem.isEven() ? 1 : 0); // two's complement for negative numbers
878
+ else xBits.push(xRem.isEven() ? 0 : 1);
879
+
880
+ if (yRem.isZero()) {
881
+ yStop = true;
882
+ yBits.push(ySign ? 1 : 0);
883
+ }
884
+ else if (ySign) yBits.push(yRem.isEven() ? 1 : 0);
885
+ else yBits.push(yRem.isEven() ? 0 : 1);
886
+
887
+ xRem = xRem.over(2);
888
+ yRem = yRem.over(2);
889
+ }
890
+ var result = [];
891
+ for (var i = 0; i < xBits.length; i++) result.push(fn(xBits[i], yBits[i]));
892
+ var sum = bigInt(result.pop()).negate().times(bigInt(2).pow(result.length));
893
+ while (result.length) {
894
+ sum = sum.add(bigInt(result.pop()).times(bigInt(2).pow(result.length)));
895
+ }
896
+ return sum;
897
+ }
898
+
899
+ BigInteger.prototype.not = function () {
900
+ return this.negate().prev();
901
+ };
902
+ SmallInteger.prototype.not = BigInteger.prototype.not;
903
+
904
+ BigInteger.prototype.and = function (n) {
905
+ return bitwise(this, n, function (a, b) { return a & b; });
906
+ };
907
+ SmallInteger.prototype.and = BigInteger.prototype.and;
908
+
909
+ BigInteger.prototype.or = function (n) {
910
+ return bitwise(this, n, function (a, b) { return a | b; });
911
+ };
912
+ SmallInteger.prototype.or = BigInteger.prototype.or;
913
+
914
+ BigInteger.prototype.xor = function (n) {
915
+ return bitwise(this, n, function (a, b) { return a ^ b; });
916
+ };
917
+ SmallInteger.prototype.xor = BigInteger.prototype.xor;
918
+
919
+ function max(a, b) {
920
+ a = parseValue(a);
921
+ b = parseValue(b);
922
+ return a.greater(b) ? a : b;
923
+ }
924
+ function min(a,b) {
925
+ a = parseValue(a);
926
+ b = parseValue(b);
927
+ return a.lesser(b) ? a : b;
928
+ }
929
+ function gcd(a, b) {
930
+ a = parseValue(a).abs();
931
+ b = parseValue(b).abs();
932
+ if (a.equals(b)) return a;
933
+ if (a.isZero()) return b;
934
+ if (b.isZero()) return a;
935
+ if (a.isEven()) {
936
+ if (b.isOdd()) {
937
+ return gcd(a.divide(2), b);
938
+ }
939
+ return gcd(a.divide(2), b.divide(2)).multiply(2);
940
+ }
941
+ if (b.isEven()) {
942
+ return gcd(a, b.divide(2));
943
+ }
944
+ if (a.greater(b)) {
945
+ return gcd(a.subtract(b).divide(2), b);
946
+ }
947
+ return gcd(b.subtract(a).divide(2), a);
948
+ }
949
+ function lcm(a, b) {
950
+ a = parseValue(a).abs();
951
+ b = parseValue(b).abs();
952
+ return a.multiply(b).divide(gcd(a, b));
953
+ }
954
+ function randBetween(a, b) {
955
+ a = parseValue(a);
956
+ b = parseValue(b);
957
+ var low = min(a, b), high = max(a, b);
958
+ var range = high.subtract(low);
959
+ if (range.isSmall) return low.add(Math.round(Math.random() * range));
960
+ var length = range.value.length - 1;
961
+ var result = [], restricted = true;
962
+ for (var i = length; i >= 0; i--) {
963
+ var top = restricted ? range.value[i] : BASE;
964
+ var digit = truncate(Math.random() * top);
965
+ result.unshift(digit);
966
+ if (digit < top) restricted = false;
967
+ }
968
+ result = arrayToSmall(result);
969
+ return low.add(new BigInteger(result, false, typeof result === "number"));
970
+ }
971
+ var parseBase = function (text, base) {
972
+ var val = CACHE[0], pow = CACHE[1],
973
+ length = text.length;
974
+ if (2 <= base && base <= 36) {
975
+ if (length <= LOG_MAX_INT / Math.log(base)) {
976
+ return new SmallInteger(parseInt(text, base));
977
+ }
978
+ }
979
+ base = parseValue(base);
980
+ var digits = [];
981
+ var i;
982
+ var isNegative = text[0] === "-";
983
+ for (i = isNegative ? 1 : 0; i < text.length; i++) {
984
+ var c = text[i].toLowerCase(),
985
+ charCode = c.charCodeAt(0);
986
+ if (48 <= charCode && charCode <= 57) digits.push(parseValue(c));
987
+ else if (97 <= charCode && charCode <= 122) digits.push(parseValue(c.charCodeAt(0) - 87));
988
+ else if (c === "<") {
989
+ var start = i;
990
+ do { i++; } while (text[i] !== ">");
991
+ digits.push(parseValue(text.slice(start + 1, i)));
992
+ }
993
+ else throw new Error(c + " is not a valid character");
994
+ }
995
+ digits.reverse();
996
+ for (i = 0; i < digits.length; i++) {
997
+ val = val.add(digits[i].times(pow));
998
+ pow = pow.times(base);
999
+ }
1000
+ return isNegative ? val.negate() : val;
1001
+ };
1002
+
1003
+ function stringify(digit) {
1004
+ var v = digit.value;
1005
+ if (typeof v === "number") v = [v];
1006
+ if (v.length === 1 && v[0] <= 36) {
1007
+ return "0123456789abcdefghijklmnopqrstuvwxyz".charAt(v[0]);
1008
+ }
1009
+ return "<" + v + ">";
1010
+ }
1011
+ function toBase(n, base) {
1012
+ base = bigInt(base);
1013
+ if (base.isZero()) {
1014
+ if (n.isZero()) return "0";
1015
+ throw new Error("Cannot convert nonzero numbers to base 0.");
1016
+ }
1017
+ if (base.equals(-1)) {
1018
+ if (n.isZero()) return "0";
1019
+ if (n.isNegative()) return new Array(1 - n).join("10");
1020
+ return "1" + new Array(+n).join("01");
1021
+ }
1022
+ var minusSign = "";
1023
+ if (n.isNegative() && base.isPositive()) {
1024
+ minusSign = "-";
1025
+ n = n.abs();
1026
+ }
1027
+ if (base.equals(1)) {
1028
+ if (n.isZero()) return "0";
1029
+ return minusSign + new Array(+n + 1).join(1);
1030
+ }
1031
+ var out = [];
1032
+ var left = n, divmod;
1033
+ while (left.isNegative() || left.compareAbs(base) >= 0) {
1034
+ divmod = left.divmod(base);
1035
+ left = divmod.quotient;
1036
+ var digit = divmod.remainder;
1037
+ if (digit.isNegative()) {
1038
+ digit = base.minus(digit).abs();
1039
+ left = left.next();
1040
+ }
1041
+ out.push(stringify(digit));
1042
+ }
1043
+ out.push(stringify(left));
1044
+ return minusSign + out.reverse().join("");
1045
+ }
1046
+
1047
+ BigInteger.prototype.toString = function (radix) {
1048
+ if (radix === undefined) radix = 10;
1049
+ if (radix !== 10) return toBase(this, radix);
1050
+ var v = this.value, l = v.length, str = String(v[--l]), zeros = "0000000", digit;
1051
+ while (--l >= 0) {
1052
+ digit = String(v[l]);
1053
+ str += zeros.slice(digit.length) + digit;
1054
+ }
1055
+ var sign = this.sign ? "-" : "";
1056
+ return sign + str;
1057
+ };
1058
+ SmallInteger.prototype.toString = function (radix) {
1059
+ if (radix === undefined) radix = 10;
1060
+ if (radix != 10) return toBase(this, radix);
1061
+ return String(this.value);
1062
+ };
1063
+
1064
+ BigInteger.prototype.valueOf = function () {
1065
+ return +this.toString();
1066
+ };
1067
+ BigInteger.prototype.toJSNumber = BigInteger.prototype.valueOf;
1068
+
1069
+ SmallInteger.prototype.valueOf = function () {
1070
+ return this.value;
1071
+ };
1072
+ SmallInteger.prototype.toJSNumber = SmallInteger.prototype.valueOf;
1073
+
1074
+ function parseStringValue(v) {
1075
+ if (isPrecise(+v)) {
1076
+ var x = +v;
1077
+ if (x === truncate(x))
1078
+ return new SmallInteger(x);
1079
+ throw "Invalid integer: " + v;
1080
+ }
1081
+ var sign = v[0] === "-";
1082
+ if (sign) v = v.slice(1);
1083
+ var split = v.split(/e/i);
1084
+ if (split.length > 2) throw new Error("Invalid integer: " + text.join("e"));
1085
+ if (split.length === 2) {
1086
+ var exp = split[1];
1087
+ if (exp[0] === "+") exp = exp.slice(1);
1088
+ exp = +exp;
1089
+ if (exp !== truncate(exp) || !isPrecise(exp)) throw new Error("Invalid integer: " + exp + " is not a valid exponent.");
1090
+ var text = split[0];
1091
+ var decimalPlace = text.indexOf(".");
1092
+ if (decimalPlace >= 0) {
1093
+ exp -= text.length - decimalPlace;
1094
+ text = text.slice(0, decimalPlace) + text.slice(decimalPlace + 1);
1095
+ }
1096
+ if (exp < 0) throw new Error("Cannot include negative exponent part for integers");
1097
+ text += (new Array(exp + 1)).join("0");
1098
+ v = text;
1099
+ }
1100
+ var isValid = /^([0-9][0-9]*)$/.test(v);
1101
+ if (!isValid) throw new Error("Invalid integer: " + v);
1102
+ var r = [], max = v.length, l = LOG_BASE, min = max - l;
1103
+ while (max > 0) {
1104
+ r.push(+v.slice(min, max));
1105
+ min -= l;
1106
+ if (min < 0) min = 0;
1107
+ max -= l;
1108
+ }
1109
+ trim(r);
1110
+ return new BigInteger(r, sign);
1111
+ }
1112
+
1113
+ function parseNumberValue(v) {
1114
+ if (isPrecise(v)) return new SmallInteger(v);
1115
+ return parseStringValue(v.toString());
1116
+ }
1117
+
1118
+ function parseValue(v) {
1119
+ if (typeof v === "number") {
1120
+ return parseNumberValue(v);
1121
+ }
1122
+ if (typeof v === "string") {
1123
+ return parseStringValue(v);
1124
+ }
1125
+ return v;
1126
+ }
1127
+ // Pre-define numbers in range [-999,999]
1128
+ var CACHE = function (v, radix) {
1129
+ if (typeof v === "undefined") return CACHE[0];
1130
+ if (typeof radix !== "undefined") return +radix === 10 ? parseValue(v) : parseBase(v, radix);
1131
+ return parseValue(v);
1132
+ };
1133
+ for (var i = 0; i < 1000; i++) {
1134
+ CACHE[i] = new SmallInteger(i);
1135
+ if (i > 0) CACHE[-i] = new SmallInteger(-i);
1136
+ }
1137
+ // Backwards compatibility
1138
+ CACHE.one = CACHE[1];
1139
+ CACHE.zero = CACHE[0];
1140
+ CACHE.minusOne = CACHE[-1];
1141
+ CACHE.max = max;
1142
+ CACHE.min = min;
1143
+ CACHE.gcd = gcd;
1144
+ CACHE.lcm = lcm;
1145
+ CACHE.isInstance = function (x) { return x instanceof BigInteger || x instanceof SmallInteger; };
1146
+ CACHE.randBetween = randBetween;
1147
+ return CACHE;
1148
+ })();
1149
+
1150
+ // Node.js check
1151
+ if (typeof module !== "undefined" && module.hasOwnProperty("exports")) {
1152
+ module.exports = bigInt;
1153
+ }