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.
- checksums.yaml +4 -4
- data/CONTRIBUTING.md +10 -0
- data/bin/bootstrap +6 -0
- data/common/CONTRIBUTING.md +2 -2
- data/lib/trackler/version.rb +1 -1
- data/tracks/coffeescript/LICENSE +2 -2
- data/tracks/coffeescript/README.md +0 -5
- data/tracks/coldfusion/LICENSE +2 -2
- data/tracks/coldfusion/README.md +0 -5
- data/tracks/crystal/LICENSE +2 -2
- data/tracks/crystal/README.md +0 -5
- data/tracks/dlang/.travis.yml +4 -2
- data/tracks/dlang/LICENSE +2 -2
- data/tracks/dlang/README.md +3 -6
- data/tracks/dlang/bin/test-all-exercises +20 -0
- data/tracks/dlang/bin/test-exercise +16 -0
- data/tracks/dlang/config.json +0 -18
- data/tracks/dlang/exercises/difference-of-squares/difference_of_squares.d +6 -3
- data/tracks/dlang/exercises/difference-of-squares/difference_of_squares_example.d +17 -0
- data/tracks/dlang/exercises/gigasecond/gigasecond.d +7 -3
- data/tracks/dlang/exercises/gigasecond/gigasecond_example.d +15 -0
- data/tracks/dlang/exercises/hello-world/hello_world.d +4 -3
- data/tracks/dlang/exercises/hello-world/hello_world_example.d +9 -0
- data/tracks/dlang/exercises/nucleotide-count/nucleotide_count.d +1 -1
- data/tracks/dlang/exercises/raindrops/raindrops.d +6 -3
- data/tracks/dlang/exercises/raindrops/raindrops_example.d +20 -0
- data/tracks/dlang/exercises/rna-transcription/rna_transcription.d +7 -3
- data/tracks/dlang/exercises/rna-transcription/rna_transcription_example.d +15 -0
- data/tracks/ecmascript/exercises/accumulate/package.json +25 -39
- data/tracks/ecmascript/exercises/acronym/package.json +25 -39
- data/tracks/ecmascript/exercises/all-your-base/package.json +25 -39
- data/tracks/ecmascript/exercises/allergies/package.json +25 -39
- data/tracks/ecmascript/exercises/anagram/package.json +25 -39
- data/tracks/ecmascript/exercises/atbash-cipher/package.json +25 -39
- data/tracks/ecmascript/exercises/beer-song/package.json +25 -39
- data/tracks/ecmascript/exercises/binary/package.json +25 -39
- data/tracks/ecmascript/exercises/binary-search/example.js +3 -1
- data/tracks/ecmascript/exercises/binary-search/package.json +25 -39
- data/tracks/ecmascript/exercises/binary-search-tree/package.json +25 -39
- data/tracks/ecmascript/exercises/bob/bob.js +1 -0
- data/tracks/ecmascript/exercises/bob/package.json +25 -39
- data/tracks/ecmascript/exercises/bracket-push/package.json +25 -39
- data/tracks/ecmascript/exercises/circular-buffer/package.json +25 -39
- data/tracks/ecmascript/exercises/clock/package.json +25 -39
- data/tracks/ecmascript/exercises/connect/example.js +4 -1
- data/tracks/ecmascript/exercises/connect/package.json +25 -39
- data/tracks/ecmascript/exercises/crypto-square/package.json +25 -39
- data/tracks/ecmascript/exercises/custom-set/package.json +25 -39
- data/tracks/ecmascript/exercises/diamond/example.js +3 -4
- data/tracks/ecmascript/exercises/diamond/package.json +25 -39
- data/tracks/ecmascript/exercises/difference-of-squares/package.json +25 -39
- data/tracks/ecmascript/exercises/etl/package.json +25 -39
- data/tracks/ecmascript/exercises/food-chain/package.json +25 -39
- data/tracks/ecmascript/exercises/gigasecond/package.json +25 -39
- data/tracks/ecmascript/exercises/grade-school/package.json +25 -39
- data/tracks/ecmascript/exercises/grains/lib/big-integer.js +1153 -1152
- data/tracks/ecmascript/exercises/grains/package.json +25 -39
- data/tracks/ecmascript/exercises/hamming/package.json +25 -39
- data/tracks/ecmascript/exercises/hello-world/package.json +25 -39
- data/tracks/ecmascript/exercises/hexadecimal/package.json +25 -39
- data/tracks/ecmascript/exercises/isogram/package.json +25 -39
- data/tracks/ecmascript/exercises/kindergarten-garden/package.json +25 -39
- data/tracks/ecmascript/exercises/largest-series-product/example.js +8 -2
- data/tracks/ecmascript/exercises/largest-series-product/package.json +25 -39
- data/tracks/ecmascript/exercises/leap/package.json +25 -39
- data/tracks/ecmascript/exercises/linked-list/package.json +25 -39
- data/tracks/ecmascript/exercises/luhn/package.json +25 -39
- data/tracks/ecmascript/exercises/matrix/package.json +25 -39
- data/tracks/ecmascript/exercises/meetup/example.js +3 -5
- data/tracks/ecmascript/exercises/meetup/meetup.spec.js +2 -5
- data/tracks/ecmascript/exercises/meetup/package.json +25 -39
- data/tracks/ecmascript/exercises/nth-prime/package.json +25 -39
- data/tracks/ecmascript/exercises/ocr-numbers/package.json +25 -39
- data/tracks/ecmascript/exercises/octal/package.json +25 -39
- data/tracks/ecmascript/exercises/package.json +24 -40
- data/tracks/ecmascript/exercises/palindrome-products/package.json +25 -39
- data/tracks/ecmascript/exercises/pangram/package.json +25 -39
- data/tracks/ecmascript/exercises/pascals-triangle/package.json +25 -39
- data/tracks/ecmascript/exercises/perfect-numbers/example.js +5 -7
- data/tracks/ecmascript/exercises/perfect-numbers/package.json +25 -39
- data/tracks/ecmascript/exercises/phone-number/package.json +25 -39
- data/tracks/ecmascript/exercises/pig-latin/package.json +25 -39
- data/tracks/ecmascript/exercises/prime-factors/package.json +25 -39
- data/tracks/ecmascript/exercises/pythagorean-triplet/package.json +25 -39
- data/tracks/ecmascript/exercises/queen-attack/package.json +25 -39
- data/tracks/ecmascript/exercises/queen-attack/queen-attack.spec.js +1 -2
- data/tracks/ecmascript/exercises/raindrops/package.json +25 -39
- data/tracks/ecmascript/exercises/rna-transcription/example.js +1 -2
- data/tracks/ecmascript/exercises/rna-transcription/package.json +25 -39
- data/tracks/ecmascript/exercises/robot-name/package.json +25 -39
- data/tracks/ecmascript/exercises/robot-simulator/package.json +25 -39
- data/tracks/ecmascript/exercises/roman-numerals/package.json +25 -39
- data/tracks/ecmascript/exercises/saddle-points/example.js +1 -1
- data/tracks/ecmascript/exercises/saddle-points/package.json +25 -39
- data/tracks/ecmascript/exercises/say/package.json +25 -39
- data/tracks/ecmascript/exercises/scrabble-score/package.json +25 -39
- data/tracks/ecmascript/exercises/secret-handshake/package.json +25 -39
- data/tracks/ecmascript/exercises/series/package.json +25 -39
- data/tracks/ecmascript/exercises/sieve/example.js +1 -1
- data/tracks/ecmascript/exercises/sieve/package.json +25 -39
- data/tracks/ecmascript/exercises/simple-cipher/package.json +25 -39
- data/tracks/ecmascript/exercises/space-age/package.json +25 -39
- data/tracks/ecmascript/exercises/strain/package.json +25 -39
- data/tracks/ecmascript/exercises/sum-of-multiples/package.json +25 -39
- data/tracks/ecmascript/exercises/triangle/package.json +25 -39
- data/tracks/ecmascript/exercises/trinary/package.json +25 -39
- data/tracks/ecmascript/exercises/two-bucket/package.json +25 -39
- data/tracks/ecmascript/exercises/word-count/package.json +25 -39
- data/tracks/ecmascript/exercises/wordy/package.json +25 -39
- data/tracks/ecmascript/package.json +25 -39
- data/tracks/factor/config.json +0 -6
- data/tracks/go/exercises/variable-length-quantity/.meta/gen.go +0 -8
- data/tracks/go/exercises/variable-length-quantity/cases_test.go +0 -9
- data/tracks/go/exercises/variable-length-quantity/example.go +5 -5
- data/tracks/go/exercises/variable-length-quantity/variable_length_quantity_test.go +2 -4
- data/tracks/groovy/LICENSE +2 -2
- data/tracks/groovy/README.md +0 -5
- data/tracks/haxe/LICENSE +2 -2
- data/tracks/haxe/README.md +0 -5
- data/tracks/nasm/LICENSE +21 -0
- data/tracks/nasm/README.md +1 -6
- data/tracks/nim/LICENSE +21 -0
- data/tracks/nim/README.md +0 -5
- data/tracks/plsql/LICENSE +2 -2
- data/tracks/plsql/README.md +0 -5
- data/tracks/plsql/config.json +52 -12
- data/tracks/pony/LICENSE +2 -2
- data/tracks/pony/README.md +0 -5
- data/tracks/powershell/LICENSE +21 -0
- data/tracks/powershell/README.md +0 -5
- data/tracks/prolog/LICENSE +2 -2
- data/tracks/prolog/README.md +1 -6
- data/tracks/purescript/LICENSE +2 -2
- data/tracks/purescript/README.md +0 -5
- data/tracks/ruby/exercises/meetup/.meta/solutions/meetup.rb +1 -0
- data/tracks/ruby/exercises/meetup/meetup_test.rb +0 -1
- data/tracks/sml/LICENSE +21 -0
- data/tracks/sml/README.md +0 -5
- data/tracks/tcl/LICENSE +2 -2
- data/tracks/tcl/README.md +1 -6
- data/tracks/teco/LICENSE +2 -2
- data/tracks/teco/README.md +0 -5
- data/tracks/vbnet/LICENSE +21 -0
- data/tracks/vbnet/README.md +0 -5
- data/tracks/vimscript/config.json +5 -0
- data/tracks/vimscript/exercises/scrabble-score/example.vim +21 -0
- data/tracks/vimscript/exercises/scrabble-score/scrabble_score.vader +62 -0
- data/tracks/vimscript/exercises/scrabble-score/scrabble_score.vim +16 -0
- metadata +13 -2
@@ -1,1152 +1,1153 @@
|
|
1
|
-
|
2
|
-
//
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
this.
|
15
|
-
this.
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
this.
|
21
|
-
this.
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
case
|
43
|
-
case
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
v
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
var
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
return Math.
|
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
|
-
return add(
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
carry
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
var
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
b
|
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
|
-
difference
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
var
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
var
|
240
|
-
small
|
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
|
-
r[i + j
|
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
|
-
if (b ===
|
322
|
-
if (b ===
|
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
|
-
if (a.value ===
|
351
|
-
if (a.value ===
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
r[i + j
|
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
|
-
quotientDigit =
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
carry
|
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
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
var
|
494
|
-
var
|
495
|
-
|
496
|
-
if (
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
if (b
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
if (
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
if (comparison ===
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
quotient =
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
mod =
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
value,
|
567
|
-
|
568
|
-
if (
|
569
|
-
if (a ===
|
570
|
-
if (a ===
|
571
|
-
if (n.
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
if (
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
b
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
exp =
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
var
|
740
|
-
|
741
|
-
if (value ===
|
742
|
-
if (value ===
|
743
|
-
return this.
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
if (n.
|
751
|
-
if (n.
|
752
|
-
if (n.
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
|
775
|
-
|
776
|
-
|
777
|
-
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
var
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
795
|
-
|
796
|
-
|
797
|
-
|
798
|
-
|
799
|
-
|
800
|
-
|
801
|
-
|
802
|
-
return new
|
803
|
-
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
|
808
|
-
|
809
|
-
|
810
|
-
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
|
815
|
-
return new
|
816
|
-
|
817
|
-
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
826
|
-
|
827
|
-
|
828
|
-
|
829
|
-
|
830
|
-
return this.
|
831
|
-
|
832
|
-
|
833
|
-
|
834
|
-
|
835
|
-
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
var
|
868
|
-
|
869
|
-
|
870
|
-
var
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
else xBits.push(xRem.isEven() ?
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
882
|
-
|
883
|
-
|
884
|
-
else yBits.push(yRem.isEven() ?
|
885
|
-
|
886
|
-
|
887
|
-
|
888
|
-
|
889
|
-
|
890
|
-
|
891
|
-
var
|
892
|
-
|
893
|
-
|
894
|
-
|
895
|
-
|
896
|
-
|
897
|
-
|
898
|
-
|
899
|
-
|
900
|
-
|
901
|
-
|
902
|
-
|
903
|
-
|
904
|
-
|
905
|
-
|
906
|
-
|
907
|
-
|
908
|
-
|
909
|
-
|
910
|
-
|
911
|
-
|
912
|
-
|
913
|
-
|
914
|
-
|
915
|
-
|
916
|
-
|
917
|
-
|
918
|
-
|
919
|
-
|
920
|
-
|
921
|
-
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
if (a.
|
933
|
-
if (
|
934
|
-
if (
|
935
|
-
|
936
|
-
|
937
|
-
|
938
|
-
|
939
|
-
|
940
|
-
|
941
|
-
|
942
|
-
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
952
|
-
|
953
|
-
|
954
|
-
|
955
|
-
|
956
|
-
|
957
|
-
var
|
958
|
-
|
959
|
-
|
960
|
-
var
|
961
|
-
|
962
|
-
|
963
|
-
var
|
964
|
-
|
965
|
-
|
966
|
-
|
967
|
-
|
968
|
-
|
969
|
-
|
970
|
-
|
971
|
-
|
972
|
-
|
973
|
-
|
974
|
-
|
975
|
-
|
976
|
-
|
977
|
-
|
978
|
-
|
979
|
-
|
980
|
-
var
|
981
|
-
var
|
982
|
-
|
983
|
-
|
984
|
-
|
985
|
-
|
986
|
-
|
987
|
-
else if (c
|
988
|
-
|
989
|
-
|
990
|
-
|
991
|
-
|
992
|
-
|
993
|
-
|
994
|
-
|
995
|
-
|
996
|
-
|
997
|
-
|
998
|
-
|
999
|
-
|
1000
|
-
|
1001
|
-
|
1002
|
-
|
1003
|
-
|
1004
|
-
|
1005
|
-
if (v
|
1006
|
-
|
1007
|
-
|
1008
|
-
|
1009
|
-
|
1010
|
-
|
1011
|
-
|
1012
|
-
|
1013
|
-
|
1014
|
-
|
1015
|
-
|
1016
|
-
|
1017
|
-
|
1018
|
-
if (n.
|
1019
|
-
|
1020
|
-
|
1021
|
-
|
1022
|
-
|
1023
|
-
|
1024
|
-
|
1025
|
-
|
1026
|
-
|
1027
|
-
|
1028
|
-
|
1029
|
-
|
1030
|
-
|
1031
|
-
var
|
1032
|
-
|
1033
|
-
|
1034
|
-
|
1035
|
-
|
1036
|
-
|
1037
|
-
|
1038
|
-
|
1039
|
-
|
1040
|
-
|
1041
|
-
|
1042
|
-
|
1043
|
-
|
1044
|
-
|
1045
|
-
|
1046
|
-
|
1047
|
-
|
1048
|
-
if (radix
|
1049
|
-
|
1050
|
-
|
1051
|
-
|
1052
|
-
|
1053
|
-
|
1054
|
-
|
1055
|
-
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
1059
|
-
if (radix
|
1060
|
-
return
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1064
|
-
|
1065
|
-
|
1066
|
-
|
1067
|
-
|
1068
|
-
|
1069
|
-
|
1070
|
-
|
1071
|
-
|
1072
|
-
|
1073
|
-
|
1074
|
-
|
1075
|
-
|
1076
|
-
|
1077
|
-
|
1078
|
-
|
1079
|
-
|
1080
|
-
|
1081
|
-
|
1082
|
-
|
1083
|
-
|
1084
|
-
if (split.length
|
1085
|
-
|
1086
|
-
|
1087
|
-
exp
|
1088
|
-
|
1089
|
-
|
1090
|
-
var
|
1091
|
-
|
1092
|
-
|
1093
|
-
|
1094
|
-
|
1095
|
-
|
1096
|
-
|
1097
|
-
|
1098
|
-
|
1099
|
-
|
1100
|
-
|
1101
|
-
|
1102
|
-
|
1103
|
-
|
1104
|
-
min
|
1105
|
-
|
1106
|
-
|
1107
|
-
|
1108
|
-
|
1109
|
-
|
1110
|
-
|
1111
|
-
|
1112
|
-
|
1113
|
-
|
1114
|
-
return
|
1115
|
-
|
1116
|
-
|
1117
|
-
|
1118
|
-
|
1119
|
-
|
1120
|
-
|
1121
|
-
|
1122
|
-
|
1123
|
-
|
1124
|
-
|
1125
|
-
|
1126
|
-
|
1127
|
-
|
1128
|
-
|
1129
|
-
if (typeof
|
1130
|
-
return parseValue(v);
|
1131
|
-
|
1132
|
-
|
1133
|
-
|
1134
|
-
|
1135
|
-
|
1136
|
-
|
1137
|
-
|
1138
|
-
CACHE.
|
1139
|
-
CACHE.
|
1140
|
-
CACHE.
|
1141
|
-
CACHE.
|
1142
|
-
CACHE.
|
1143
|
-
CACHE.
|
1144
|
-
CACHE.
|
1145
|
-
CACHE.
|
1146
|
-
|
1147
|
-
|
1148
|
-
|
1149
|
-
|
1150
|
-
|
1151
|
-
|
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
|
+
}
|