nserror-schemenumber-js-rails 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/SchemeNumber-Rails.rb +6 -0
- data/lib/schemenumber-js-rails/engine.rb +6 -0
- data/lib/schemenumber-js-rails/version.rb +5 -0
- data/schemenumber-js-rails.gemspec +27 -0
- data/vendor/assets/javascripts/biginteger.js +1621 -0
- data/vendor/assets/javascripts/schemeNumber.js +6558 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 866b8e7c841383d7ac98587e28802c73814580ae
|
4
|
+
data.tar.gz: 158391a3c214200c878f308ff97b6b191e410d36
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 57411fa242b64ea89a30c175447300072b38cda8bf95fb5a163dea8242a0f9315a381c3e57cf78ca968c6badd122d7b114834f498242ebffeddd2a61cf336191
|
7
|
+
data.tar.gz: 5a2415541be99f3b449ca0598c28d7326d1a9635261d7b970f4c9e72d340e307692068496f58d8ce7ce24a8dac919f298dd5012fcd91cdeb2189e9d9dcaddad2
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# largely copying the example from the
|
2
|
+
# less-rails-fontawesome gem
|
3
|
+
|
4
|
+
$:.push File.expand_path("../lib", __FILE__)
|
5
|
+
|
6
|
+
require 'schemenumber-js-rails/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |s|
|
9
|
+
s.name = 'nserror-schemenumber-js-rails'
|
10
|
+
s.version = SchemeNumber::Rails::VERSION
|
11
|
+
s.authors = ['Chris Miller']
|
12
|
+
s.email = ['lordsauronthegreat@gmail.com']
|
13
|
+
s.homepage = 'https://github.com/NSError/schemenumber-js-rails'
|
14
|
+
s.summary = %q{SchemeNumber.JS packaged for your Rails 3.1+ Asset Pipeline}
|
15
|
+
s.description = %q{SchemeNumber.JS packaged for the Rails 3.1+ Asset Pipeline, both the Coffee, JS, and min.JS versions.}
|
16
|
+
|
17
|
+
s.files = %w{
|
18
|
+
schemenumber-js-rails.gemspec
|
19
|
+
vendor/assets/javascripts/biginteger.js
|
20
|
+
vendor/assets/javascripts/schemeNumber.js
|
21
|
+
lib/SchemeNumber-Rails.rb
|
22
|
+
lib/schemenumber-js-rails/engine.rb
|
23
|
+
lib/schemenumber-js-rails/version.rb }
|
24
|
+
s.require_paths = ['lib']
|
25
|
+
|
26
|
+
s.add_runtime_dependency 'railties', '>= 3.1.1'
|
27
|
+
end
|
@@ -0,0 +1,1621 @@
|
|
1
|
+
/*
|
2
|
+
JavaScript BigInteger library version 0.9
|
3
|
+
http://silentmatt.com/biginteger/
|
4
|
+
|
5
|
+
Copyright (c) 2009 Matthew Crumley <email@matthewcrumley.com>
|
6
|
+
Copyright (c) 2010,2011 by John Tobey <jtobey@john-edwin-tobey.org>
|
7
|
+
Licensed under the MIT license.
|
8
|
+
|
9
|
+
Support for arbitrary internal representation base was added by
|
10
|
+
Vitaly Magerya.
|
11
|
+
*/
|
12
|
+
|
13
|
+
/*
|
14
|
+
File: biginteger.js
|
15
|
+
|
16
|
+
Exports:
|
17
|
+
|
18
|
+
<BigInteger>
|
19
|
+
*/
|
20
|
+
|
21
|
+
/*
|
22
|
+
Class: BigInteger
|
23
|
+
An arbitrarily-large integer.
|
24
|
+
|
25
|
+
<BigInteger> objects should be considered immutable. None of the "built-in"
|
26
|
+
methods modify *this* or their arguments. All properties should be
|
27
|
+
considered private.
|
28
|
+
|
29
|
+
All the methods of <BigInteger> instances can be called "statically". The
|
30
|
+
static versions are convenient if you don't already have a <BigInteger>
|
31
|
+
object.
|
32
|
+
|
33
|
+
As an example, these calls are equivalent.
|
34
|
+
|
35
|
+
> BigInteger(4).multiply(5); // returns BigInteger(20);
|
36
|
+
> BigInteger.multiply(4, 5); // returns BigInteger(20);
|
37
|
+
|
38
|
+
> var a = 42;
|
39
|
+
> var a = BigInteger.toJSValue("0b101010"); // Not completely useless...
|
40
|
+
*/
|
41
|
+
|
42
|
+
// IE doesn't support Array.prototype.map
|
43
|
+
if (!Array.prototype.map) {
|
44
|
+
Array.prototype.map = function(fun /*, thisp*/) {
|
45
|
+
var len = this.length >>> 0;
|
46
|
+
if (typeof fun !== "function") {
|
47
|
+
throw new TypeError();
|
48
|
+
}
|
49
|
+
|
50
|
+
var res = new Array(len);
|
51
|
+
var thisp = arguments[1];
|
52
|
+
for (var i = 0; i < len; i++) {
|
53
|
+
if (i in this) {
|
54
|
+
res[i] = fun.call(thisp, this[i], i, this);
|
55
|
+
}
|
56
|
+
}
|
57
|
+
|
58
|
+
return res;
|
59
|
+
};
|
60
|
+
}
|
61
|
+
|
62
|
+
/*
|
63
|
+
Constructor: BigInteger()
|
64
|
+
Convert a value to a <BigInteger>.
|
65
|
+
|
66
|
+
Although <BigInteger()> is the constructor for <BigInteger> objects, it is
|
67
|
+
best not to call it as a constructor. If *n* is a <BigInteger> object, it is
|
68
|
+
simply returned as-is. Otherwise, <BigInteger()> is equivalent to <parse>
|
69
|
+
without a radix argument.
|
70
|
+
|
71
|
+
> var n0 = BigInteger(); // Same as <BigInteger.ZERO>
|
72
|
+
> var n1 = BigInteger("123"); // Create a new <BigInteger> with value 123
|
73
|
+
> var n2 = BigInteger(123); // Create a new <BigInteger> with value 123
|
74
|
+
> var n3 = BigInteger(n2); // Return n2, unchanged
|
75
|
+
|
76
|
+
The constructor form only takes an array and a sign. *n* must be an
|
77
|
+
array of numbers in little-endian order, where each digit is between 0
|
78
|
+
and BigInteger.base. The second parameter sets the sign: -1 for
|
79
|
+
negative, +1 for positive, or 0 for zero. The array is *not copied and
|
80
|
+
may be modified*. If the array contains only zeros, the sign parameter
|
81
|
+
is ignored and is forced to zero.
|
82
|
+
|
83
|
+
> new BigInteger([5], -1): create a new BigInteger with value -5
|
84
|
+
|
85
|
+
Parameters:
|
86
|
+
|
87
|
+
n - Value to convert to a <BigInteger>.
|
88
|
+
|
89
|
+
Returns:
|
90
|
+
|
91
|
+
A <BigInteger> value.
|
92
|
+
|
93
|
+
See Also:
|
94
|
+
|
95
|
+
<parse>, <BigInteger>
|
96
|
+
*/
|
97
|
+
function BigInteger(n, s) {
|
98
|
+
if (!(this instanceof BigInteger)) {
|
99
|
+
if (n instanceof BigInteger) {
|
100
|
+
return n;
|
101
|
+
}
|
102
|
+
else if (typeof n === "undefined") {
|
103
|
+
return BigInteger.ZERO;
|
104
|
+
}
|
105
|
+
return BigInteger.parse(n);
|
106
|
+
}
|
107
|
+
|
108
|
+
n = n || []; // Provide the nullary constructor for subclasses.
|
109
|
+
while (n.length && !n[n.length - 1]) {
|
110
|
+
--n.length;
|
111
|
+
}
|
112
|
+
this._d = n;
|
113
|
+
this._s = n.length ? (s || 1) : 0;
|
114
|
+
}
|
115
|
+
|
116
|
+
// Base-10 speedup hacks in parse, toString, exp10 and log functions
|
117
|
+
// require base to be a power of 10. 10^7 is the largest such power
|
118
|
+
// that won't cause a precision loss when digits are multiplied.
|
119
|
+
BigInteger.base = 10000000;
|
120
|
+
BigInteger.base_log10 = 7;
|
121
|
+
|
122
|
+
BigInteger.init = function() {
|
123
|
+
|
124
|
+
// Constant: ZERO
|
125
|
+
// <BigInteger> 0.
|
126
|
+
BigInteger.ZERO = new BigInteger([], 0);
|
127
|
+
|
128
|
+
// Constant: ONE
|
129
|
+
// <BigInteger> 1.
|
130
|
+
BigInteger.ONE = new BigInteger([1], 1);
|
131
|
+
|
132
|
+
// Constant: M_ONE
|
133
|
+
// <BigInteger> -1.
|
134
|
+
BigInteger.M_ONE = new BigInteger(BigInteger.ONE._d, -1);
|
135
|
+
|
136
|
+
// Constant: _0
|
137
|
+
// Shortcut for <ZERO>.
|
138
|
+
BigInteger._0 = BigInteger.ZERO;
|
139
|
+
|
140
|
+
// Constant: _1
|
141
|
+
// Shortcut for <ONE>.
|
142
|
+
BigInteger._1 = BigInteger.ONE;
|
143
|
+
|
144
|
+
/*
|
145
|
+
Constant: small
|
146
|
+
Array of <BigIntegers> from 0 to 36.
|
147
|
+
|
148
|
+
These are used internally for parsing, but useful when you need a "small"
|
149
|
+
<BigInteger>.
|
150
|
+
|
151
|
+
See Also:
|
152
|
+
|
153
|
+
<ZERO>, <ONE>, <_0>, <_1>
|
154
|
+
*/
|
155
|
+
BigInteger.small = [
|
156
|
+
BigInteger.ZERO,
|
157
|
+
BigInteger.ONE,
|
158
|
+
/* Assuming BigInteger.base > 36 */
|
159
|
+
new BigInteger( [2], 1),
|
160
|
+
new BigInteger( [3], 1),
|
161
|
+
new BigInteger( [4], 1),
|
162
|
+
new BigInteger( [5], 1),
|
163
|
+
new BigInteger( [6], 1),
|
164
|
+
new BigInteger( [7], 1),
|
165
|
+
new BigInteger( [8], 1),
|
166
|
+
new BigInteger( [9], 1),
|
167
|
+
new BigInteger([10], 1),
|
168
|
+
new BigInteger([11], 1),
|
169
|
+
new BigInteger([12], 1),
|
170
|
+
new BigInteger([13], 1),
|
171
|
+
new BigInteger([14], 1),
|
172
|
+
new BigInteger([15], 1),
|
173
|
+
new BigInteger([16], 1),
|
174
|
+
new BigInteger([17], 1),
|
175
|
+
new BigInteger([18], 1),
|
176
|
+
new BigInteger([19], 1),
|
177
|
+
new BigInteger([20], 1),
|
178
|
+
new BigInteger([21], 1),
|
179
|
+
new BigInteger([22], 1),
|
180
|
+
new BigInteger([23], 1),
|
181
|
+
new BigInteger([24], 1),
|
182
|
+
new BigInteger([25], 1),
|
183
|
+
new BigInteger([26], 1),
|
184
|
+
new BigInteger([27], 1),
|
185
|
+
new BigInteger([28], 1),
|
186
|
+
new BigInteger([29], 1),
|
187
|
+
new BigInteger([30], 1),
|
188
|
+
new BigInteger([31], 1),
|
189
|
+
new BigInteger([32], 1),
|
190
|
+
new BigInteger([33], 1),
|
191
|
+
new BigInteger([34], 1),
|
192
|
+
new BigInteger([35], 1),
|
193
|
+
new BigInteger([36], 1)
|
194
|
+
];
|
195
|
+
}
|
196
|
+
BigInteger.init();
|
197
|
+
|
198
|
+
// Used for parsing/radix conversion
|
199
|
+
BigInteger.digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
|
200
|
+
|
201
|
+
/*
|
202
|
+
Method: toString
|
203
|
+
Convert a <BigInteger> to a string.
|
204
|
+
|
205
|
+
When *base* is greater than 10, letters are upper case.
|
206
|
+
|
207
|
+
Parameters:
|
208
|
+
|
209
|
+
base - Optional base to represent the number in (default is base 10).
|
210
|
+
Must be between 2 and 36 inclusive, or an Error will be thrown.
|
211
|
+
|
212
|
+
Returns:
|
213
|
+
|
214
|
+
The string representation of the <BigInteger>.
|
215
|
+
*/
|
216
|
+
BigInteger.prototype.toString = function(base) {
|
217
|
+
base = +base || 10;
|
218
|
+
if (base < 2 || base > 36) {
|
219
|
+
throw new Error("illegal radix " + base + ".");
|
220
|
+
}
|
221
|
+
if (this._s === 0) {
|
222
|
+
return "0";
|
223
|
+
}
|
224
|
+
if (base === 10) {
|
225
|
+
var str = this._s < 0 ? "-" : "";
|
226
|
+
str += this._d[this._d.length - 1].toString();
|
227
|
+
for (var i = this._d.length - 2; i >= 0; i--) {
|
228
|
+
var group = this._d[i].toString();
|
229
|
+
while (group.length < BigInteger.base_log10) group = '0' + group;
|
230
|
+
str += group;
|
231
|
+
}
|
232
|
+
return str;
|
233
|
+
}
|
234
|
+
else {
|
235
|
+
var numerals = BigInteger.digits;
|
236
|
+
base = BigInteger.small[base];
|
237
|
+
var sign = this._s;
|
238
|
+
|
239
|
+
var n = this.abs();
|
240
|
+
var digits = [];
|
241
|
+
var digit;
|
242
|
+
|
243
|
+
while (n._s !== 0) {
|
244
|
+
var divmod = n.divRem(base);
|
245
|
+
n = divmod[0];
|
246
|
+
digit = divmod[1];
|
247
|
+
// TODO: This could be changed to unshift instead of reversing at the end.
|
248
|
+
// Benchmark both to compare speeds.
|
249
|
+
digits.push(numerals[digit.valueOf()]);
|
250
|
+
}
|
251
|
+
return (sign < 0 ? "-" : "") + digits.reverse().join("");
|
252
|
+
}
|
253
|
+
};
|
254
|
+
|
255
|
+
// Verify strings for parsing
|
256
|
+
BigInteger.radixRegex = [
|
257
|
+
/^$/,
|
258
|
+
/^$/,
|
259
|
+
/^[01]*$/,
|
260
|
+
/^[012]*$/,
|
261
|
+
/^[0-3]*$/,
|
262
|
+
/^[0-4]*$/,
|
263
|
+
/^[0-5]*$/,
|
264
|
+
/^[0-6]*$/,
|
265
|
+
/^[0-7]*$/,
|
266
|
+
/^[0-8]*$/,
|
267
|
+
/^[0-9]*$/,
|
268
|
+
/^[0-9aA]*$/,
|
269
|
+
/^[0-9abAB]*$/,
|
270
|
+
/^[0-9abcABC]*$/,
|
271
|
+
/^[0-9a-dA-D]*$/,
|
272
|
+
/^[0-9a-eA-E]*$/,
|
273
|
+
/^[0-9a-fA-F]*$/,
|
274
|
+
/^[0-9a-gA-G]*$/,
|
275
|
+
/^[0-9a-hA-H]*$/,
|
276
|
+
/^[0-9a-iA-I]*$/,
|
277
|
+
/^[0-9a-jA-J]*$/,
|
278
|
+
/^[0-9a-kA-K]*$/,
|
279
|
+
/^[0-9a-lA-L]*$/,
|
280
|
+
/^[0-9a-mA-M]*$/,
|
281
|
+
/^[0-9a-nA-N]*$/,
|
282
|
+
/^[0-9a-oA-O]*$/,
|
283
|
+
/^[0-9a-pA-P]*$/,
|
284
|
+
/^[0-9a-qA-Q]*$/,
|
285
|
+
/^[0-9a-rA-R]*$/,
|
286
|
+
/^[0-9a-sA-S]*$/,
|
287
|
+
/^[0-9a-tA-T]*$/,
|
288
|
+
/^[0-9a-uA-U]*$/,
|
289
|
+
/^[0-9a-vA-V]*$/,
|
290
|
+
/^[0-9a-wA-W]*$/,
|
291
|
+
/^[0-9a-xA-X]*$/,
|
292
|
+
/^[0-9a-yA-Y]*$/,
|
293
|
+
/^[0-9a-zA-Z]*$/
|
294
|
+
];
|
295
|
+
|
296
|
+
/*
|
297
|
+
Function: parse
|
298
|
+
Parse a string into a <BigInteger>.
|
299
|
+
|
300
|
+
*base* is optional but, if provided, must be from 2 to 36 inclusive. If
|
301
|
+
*base* is not provided, it will be guessed based on the leading characters
|
302
|
+
of *s* as follows:
|
303
|
+
|
304
|
+
- "0x" or "0X": *base* = 16
|
305
|
+
- "0c" or "0C": *base* = 8
|
306
|
+
- "0b" or "0B": *base* = 2
|
307
|
+
- else: *base* = 10
|
308
|
+
|
309
|
+
If no base is provided, or *base* is 10, the number can be in exponential
|
310
|
+
form. For example, these are all valid:
|
311
|
+
|
312
|
+
> BigInteger.parse("1e9"); // Same as "1000000000"
|
313
|
+
> BigInteger.parse("1.234*10^3"); // Same as 1234
|
314
|
+
> BigInteger.parse("56789 * 10 ** -2"); // Same as 567
|
315
|
+
|
316
|
+
If any characters fall outside the range defined by the radix, an exception
|
317
|
+
will be thrown.
|
318
|
+
|
319
|
+
Parameters:
|
320
|
+
|
321
|
+
s - The string to parse.
|
322
|
+
base - Optional radix (default is to guess based on *s*).
|
323
|
+
|
324
|
+
Returns:
|
325
|
+
|
326
|
+
a <BigInteger> instance.
|
327
|
+
*/
|
328
|
+
BigInteger.parse = function(s, base) {
|
329
|
+
// Expands a number in exponential form to decimal form.
|
330
|
+
// expandExponential("-13.441*10^5") === "1344100";
|
331
|
+
// expandExponential("1.12300e-1") === "0.112300";
|
332
|
+
// expandExponential(1000000000000000000000000000000) === "1000000000000000000000000000000";
|
333
|
+
function expandExponential(str) {
|
334
|
+
str = str.replace(/\s*[*xX]\s*10\s*(\^|\*\*)\s*/, "e");
|
335
|
+
|
336
|
+
return str.replace(/^([+\-])?(\d+)\.?(\d*)[eE]([+\-]?\d+)$/, function(x, s, n, f, c) {
|
337
|
+
c = +c;
|
338
|
+
var l = c < 0;
|
339
|
+
var i = n.length + c;
|
340
|
+
x = (l ? n : f).length;
|
341
|
+
c = ((c = Math.abs(c)) >= x ? c - x + l : 0);
|
342
|
+
var z = (new Array(c + 1)).join("0");
|
343
|
+
var r = n + f;
|
344
|
+
return (s || "") + (l ? r = z + r : r += z).substr(0, i += l ? z.length : 0) + (i < r.length ? "." + r.substr(i) : "");
|
345
|
+
});
|
346
|
+
}
|
347
|
+
|
348
|
+
s = s.toString();
|
349
|
+
if (typeof base === "undefined" || +base === 10) {
|
350
|
+
s = expandExponential(s);
|
351
|
+
}
|
352
|
+
|
353
|
+
var parts = /^([+\-]?)(0[xXcCbB])?([0-9A-Za-z]*)(?:\.\d*)?$/.exec(s);
|
354
|
+
if (parts) {
|
355
|
+
var sign = parts[1] || "+";
|
356
|
+
var baseSection = parts[2] || "";
|
357
|
+
var digits = parts[3] || "";
|
358
|
+
|
359
|
+
if (typeof base === "undefined") {
|
360
|
+
// Guess base
|
361
|
+
if (baseSection === "0x" || baseSection === "0X") { // Hex
|
362
|
+
base = 16;
|
363
|
+
}
|
364
|
+
else if (baseSection === "0c" || baseSection === "0C") { // Octal
|
365
|
+
base = 8;
|
366
|
+
}
|
367
|
+
else if (baseSection === "0b" || baseSection === "0B") { // Binary
|
368
|
+
base = 2;
|
369
|
+
}
|
370
|
+
else {
|
371
|
+
base = 10;
|
372
|
+
}
|
373
|
+
}
|
374
|
+
else if (base < 2 || base > 36) {
|
375
|
+
throw new Error("Illegal radix " + base + ".");
|
376
|
+
}
|
377
|
+
|
378
|
+
base = +base;
|
379
|
+
|
380
|
+
// Check for digits outside the range
|
381
|
+
if (!(BigInteger.radixRegex[base].test(digits))) {
|
382
|
+
throw new Error("Bad digit for radix " + base);
|
383
|
+
}
|
384
|
+
|
385
|
+
// Strip leading zeros, and convert to array
|
386
|
+
digits = digits.replace(/^0+/, "").split("");
|
387
|
+
if (digits.length === 0) {
|
388
|
+
return BigInteger.ZERO;
|
389
|
+
}
|
390
|
+
|
391
|
+
// Get the sign (we know it's not zero)
|
392
|
+
sign = (sign === "-") ? -1 : 1;
|
393
|
+
|
394
|
+
// Optimize 10
|
395
|
+
if (base == 10) {
|
396
|
+
var d = [];
|
397
|
+
while (digits.length >= BigInteger.base_log10) {
|
398
|
+
d.push(parseInt(digits.splice(-BigInteger.base_log10).join(''), 10));
|
399
|
+
}
|
400
|
+
d.push(parseInt(digits.join(''), 10));
|
401
|
+
return new BigInteger(d, sign);
|
402
|
+
}
|
403
|
+
|
404
|
+
// Optimize base
|
405
|
+
if (base === BigInteger.base) {
|
406
|
+
return new BigInteger(digits.map(Number).reverse(), sign);
|
407
|
+
}
|
408
|
+
|
409
|
+
// Do the conversion
|
410
|
+
var d = BigInteger.ZERO;
|
411
|
+
base = BigInteger.small[base];
|
412
|
+
var small = BigInteger.small;
|
413
|
+
for (var i = 0; i < digits.length; i++) {
|
414
|
+
d = d.multiply(base).add(small[parseInt(digits[i], 36)]);
|
415
|
+
}
|
416
|
+
return new BigInteger(d._d, sign);
|
417
|
+
}
|
418
|
+
else {
|
419
|
+
throw new Error("Invalid BigInteger format: " + s);
|
420
|
+
}
|
421
|
+
};
|
422
|
+
|
423
|
+
/*
|
424
|
+
Function: add
|
425
|
+
Add two <BigIntegers>.
|
426
|
+
|
427
|
+
Parameters:
|
428
|
+
|
429
|
+
n - The number to add to *this*. Will be converted to a <BigInteger>.
|
430
|
+
|
431
|
+
Returns:
|
432
|
+
|
433
|
+
The numbers added together.
|
434
|
+
|
435
|
+
See Also:
|
436
|
+
|
437
|
+
<subtract>, <multiply>, <quotient>, <next>
|
438
|
+
*/
|
439
|
+
BigInteger.prototype.add = function(n) {
|
440
|
+
if (this._s === 0) {
|
441
|
+
return BigInteger(n);
|
442
|
+
}
|
443
|
+
|
444
|
+
n = BigInteger(n);
|
445
|
+
if (n._s === 0) {
|
446
|
+
return this;
|
447
|
+
}
|
448
|
+
if (this._s !== n._s) {
|
449
|
+
n = n.negate();
|
450
|
+
return this.subtract(n);
|
451
|
+
}
|
452
|
+
|
453
|
+
var a = this._d;
|
454
|
+
var b = n._d;
|
455
|
+
var al = a.length;
|
456
|
+
var bl = b.length;
|
457
|
+
var sum = new Array(Math.max(al, bl) + 1);
|
458
|
+
var size = Math.min(al, bl);
|
459
|
+
var carry = 0;
|
460
|
+
var digit;
|
461
|
+
|
462
|
+
for (var i = 0; i < size; i++) {
|
463
|
+
digit = a[i] + b[i] + carry;
|
464
|
+
sum[i] = digit % BigInteger.base;
|
465
|
+
carry = (digit / BigInteger.base) | 0;
|
466
|
+
}
|
467
|
+
if (bl > al) {
|
468
|
+
a = b;
|
469
|
+
al = bl;
|
470
|
+
}
|
471
|
+
for (i = size; carry && i < al; i++) {
|
472
|
+
digit = a[i] + carry;
|
473
|
+
sum[i] = digit % BigInteger.base;
|
474
|
+
carry = (digit / BigInteger.base) | 0;
|
475
|
+
}
|
476
|
+
if (carry) {
|
477
|
+
sum[i] = carry;
|
478
|
+
}
|
479
|
+
|
480
|
+
for ( ; i < al; i++) {
|
481
|
+
sum[i] = a[i];
|
482
|
+
}
|
483
|
+
|
484
|
+
return new BigInteger(sum, this._s);
|
485
|
+
};
|
486
|
+
|
487
|
+
/*
|
488
|
+
Function: negate
|
489
|
+
Get the additive inverse of a <BigInteger>.
|
490
|
+
|
491
|
+
Returns:
|
492
|
+
|
493
|
+
A <BigInteger> with the same magnatude, but with the opposite sign.
|
494
|
+
|
495
|
+
See Also:
|
496
|
+
|
497
|
+
<abs>
|
498
|
+
*/
|
499
|
+
BigInteger.prototype.negate = function() {
|
500
|
+
return new BigInteger(this._d, -this._s);
|
501
|
+
};
|
502
|
+
|
503
|
+
/*
|
504
|
+
Function: abs
|
505
|
+
Get the absolute value of a <BigInteger>.
|
506
|
+
|
507
|
+
Returns:
|
508
|
+
|
509
|
+
A <BigInteger> with the same magnatude, but always positive (or zero).
|
510
|
+
|
511
|
+
See Also:
|
512
|
+
|
513
|
+
<negate>
|
514
|
+
*/
|
515
|
+
BigInteger.prototype.abs = function() {
|
516
|
+
return (this._s < 0) ? this.negate() : this;
|
517
|
+
};
|
518
|
+
|
519
|
+
/*
|
520
|
+
Function: subtract
|
521
|
+
Subtract two <BigIntegers>.
|
522
|
+
|
523
|
+
Parameters:
|
524
|
+
|
525
|
+
n - The number to subtract from *this*. Will be converted to a <BigInteger>.
|
526
|
+
|
527
|
+
Returns:
|
528
|
+
|
529
|
+
The *n* subtracted from *this*.
|
530
|
+
|
531
|
+
See Also:
|
532
|
+
|
533
|
+
<add>, <multiply>, <quotient>, <prev>
|
534
|
+
*/
|
535
|
+
BigInteger.prototype.subtract = function(n) {
|
536
|
+
if (this._s === 0) {
|
537
|
+
return BigInteger(n).negate();
|
538
|
+
}
|
539
|
+
|
540
|
+
n = BigInteger(n);
|
541
|
+
if (n._s === 0) {
|
542
|
+
return this;
|
543
|
+
}
|
544
|
+
if (this._s !== n._s) {
|
545
|
+
n = n.negate();
|
546
|
+
return this.add(n);
|
547
|
+
}
|
548
|
+
|
549
|
+
var m = this;
|
550
|
+
var t;
|
551
|
+
// negative - negative => -|a| - -|b| => -|a| + |b| => |b| - |a|
|
552
|
+
if (this._s < 0) {
|
553
|
+
t = m;
|
554
|
+
m = new BigInteger(n._d, 1);
|
555
|
+
n = new BigInteger(t._d, 1);
|
556
|
+
}
|
557
|
+
|
558
|
+
// Both are positive => a - b
|
559
|
+
var sign = m.compareAbs(n);
|
560
|
+
if (sign === 0) {
|
561
|
+
return BigInteger.ZERO;
|
562
|
+
}
|
563
|
+
else if (sign < 0) {
|
564
|
+
// swap m and n
|
565
|
+
t = n;
|
566
|
+
n = m;
|
567
|
+
m = t;
|
568
|
+
}
|
569
|
+
|
570
|
+
// a > b
|
571
|
+
var a = m._d;
|
572
|
+
var b = n._d;
|
573
|
+
var al = a.length;
|
574
|
+
var bl = b.length;
|
575
|
+
var diff = new Array(al); // al >= bl since a > b
|
576
|
+
var borrow = 0;
|
577
|
+
var i;
|
578
|
+
var digit;
|
579
|
+
|
580
|
+
for (i = 0; i < bl; i++) {
|
581
|
+
digit = a[i] - borrow - b[i];
|
582
|
+
if (digit < 0) {
|
583
|
+
digit += BigInteger.base;
|
584
|
+
borrow = 1;
|
585
|
+
}
|
586
|
+
else {
|
587
|
+
borrow = 0;
|
588
|
+
}
|
589
|
+
diff[i] = digit;
|
590
|
+
}
|
591
|
+
for (i = bl; i < al; i++) {
|
592
|
+
digit = a[i] - borrow;
|
593
|
+
if (digit < 0) {
|
594
|
+
digit += BigInteger.base;
|
595
|
+
}
|
596
|
+
else {
|
597
|
+
diff[i++] = digit;
|
598
|
+
break;
|
599
|
+
}
|
600
|
+
diff[i] = digit;
|
601
|
+
}
|
602
|
+
for ( ; i < al; i++) {
|
603
|
+
diff[i] = a[i];
|
604
|
+
}
|
605
|
+
|
606
|
+
return new BigInteger(diff, sign);
|
607
|
+
};
|
608
|
+
|
609
|
+
(function() {
|
610
|
+
function addOne(n, sign) {
|
611
|
+
var a = n._d;
|
612
|
+
var sum = a.slice();
|
613
|
+
var carry = true;
|
614
|
+
var i = 0;
|
615
|
+
|
616
|
+
while (true) {
|
617
|
+
var digit = (a[i] || 0) + 1;
|
618
|
+
sum[i] = digit % BigInteger.base;
|
619
|
+
if (digit <= BigInteger.base - 1) {
|
620
|
+
break;
|
621
|
+
}
|
622
|
+
++i;
|
623
|
+
}
|
624
|
+
|
625
|
+
return new BigInteger(sum, sign);
|
626
|
+
}
|
627
|
+
|
628
|
+
function subtractOne(n, sign) {
|
629
|
+
var a = n._d;
|
630
|
+
var sum = a.slice();
|
631
|
+
var borrow = true;
|
632
|
+
var i = 0;
|
633
|
+
|
634
|
+
while (true) {
|
635
|
+
var digit = (a[i] || 0) - 1;
|
636
|
+
if (digit < 0) {
|
637
|
+
sum[i] = digit + BigInteger.base;
|
638
|
+
}
|
639
|
+
else {
|
640
|
+
sum[i] = digit;
|
641
|
+
break;
|
642
|
+
}
|
643
|
+
++i;
|
644
|
+
}
|
645
|
+
|
646
|
+
return new BigInteger(sum, sign);
|
647
|
+
}
|
648
|
+
|
649
|
+
/*
|
650
|
+
Function: next
|
651
|
+
Get the next <BigInteger> (add one).
|
652
|
+
|
653
|
+
Returns:
|
654
|
+
|
655
|
+
*this* + 1.
|
656
|
+
|
657
|
+
See Also:
|
658
|
+
|
659
|
+
<add>, <prev>
|
660
|
+
*/
|
661
|
+
BigInteger.prototype.next = function() {
|
662
|
+
switch (this._s) {
|
663
|
+
case 0:
|
664
|
+
return BigInteger.ONE;
|
665
|
+
case -1:
|
666
|
+
return subtractOne(this, -1);
|
667
|
+
// case 1:
|
668
|
+
default:
|
669
|
+
return addOne(this, 1);
|
670
|
+
}
|
671
|
+
};
|
672
|
+
|
673
|
+
/*
|
674
|
+
Function: prev
|
675
|
+
Get the previous <BigInteger> (subtract one).
|
676
|
+
|
677
|
+
Returns:
|
678
|
+
|
679
|
+
*this* - 1.
|
680
|
+
|
681
|
+
See Also:
|
682
|
+
|
683
|
+
<next>, <subtract>
|
684
|
+
*/
|
685
|
+
BigInteger.prototype.prev = function() {
|
686
|
+
switch (this._s) {
|
687
|
+
case 0:
|
688
|
+
return BigInteger.M_ONE;
|
689
|
+
case -1:
|
690
|
+
return addOne(this, -1);
|
691
|
+
// case 1:
|
692
|
+
default:
|
693
|
+
return subtractOne(this, 1);
|
694
|
+
}
|
695
|
+
};
|
696
|
+
})();
|
697
|
+
|
698
|
+
/*
|
699
|
+
Function: compareAbs
|
700
|
+
Compare the absolute value of two <BigIntegers>.
|
701
|
+
|
702
|
+
Calling <compareAbs> is faster than calling <abs> twice, then <compare>.
|
703
|
+
|
704
|
+
Parameters:
|
705
|
+
|
706
|
+
n - The number to compare to *this*. Will be converted to a <BigInteger>.
|
707
|
+
|
708
|
+
Returns:
|
709
|
+
|
710
|
+
-1, 0, or +1 if *|this|* is less than, equal to, or greater than *|n|*.
|
711
|
+
|
712
|
+
See Also:
|
713
|
+
|
714
|
+
<compare>, <abs>
|
715
|
+
*/
|
716
|
+
BigInteger.prototype.compareAbs = function(n) {
|
717
|
+
if (this === n) {
|
718
|
+
return 0;
|
719
|
+
}
|
720
|
+
|
721
|
+
if (!(n instanceof BigInteger)) {
|
722
|
+
if (!isFinite(n)) {
|
723
|
+
return(isNaN(n) ? n : -1);
|
724
|
+
}
|
725
|
+
n = BigInteger(n);
|
726
|
+
}
|
727
|
+
|
728
|
+
if (this._s === 0) {
|
729
|
+
return (n._s !== 0) ? -1 : 0;
|
730
|
+
}
|
731
|
+
if (n._s === 0) {
|
732
|
+
return 1;
|
733
|
+
}
|
734
|
+
|
735
|
+
var l = this._d.length;
|
736
|
+
var nl = n._d.length;
|
737
|
+
if (l < nl) {
|
738
|
+
return -1;
|
739
|
+
}
|
740
|
+
else if (l > nl) {
|
741
|
+
return 1;
|
742
|
+
}
|
743
|
+
|
744
|
+
var a = this._d;
|
745
|
+
var b = n._d;
|
746
|
+
for (var i = l-1; i >= 0; i--) {
|
747
|
+
if (a[i] !== b[i]) {
|
748
|
+
return a[i] < b[i] ? -1 : 1;
|
749
|
+
}
|
750
|
+
}
|
751
|
+
|
752
|
+
return 0;
|
753
|
+
};
|
754
|
+
|
755
|
+
/*
|
756
|
+
Function: compare
|
757
|
+
Compare two <BigIntegers>.
|
758
|
+
|
759
|
+
Parameters:
|
760
|
+
|
761
|
+
n - The number to compare to *this*. Will be converted to a <BigInteger>.
|
762
|
+
|
763
|
+
Returns:
|
764
|
+
|
765
|
+
-1, 0, or +1 if *this* is less than, equal to, or greater than *n*.
|
766
|
+
|
767
|
+
See Also:
|
768
|
+
|
769
|
+
<compareAbs>, <isPositive>, <isNegative>, <isUnit>
|
770
|
+
*/
|
771
|
+
BigInteger.prototype.compare = function(n) {
|
772
|
+
if (this === n) {
|
773
|
+
return 0;
|
774
|
+
}
|
775
|
+
|
776
|
+
n = BigInteger(n);
|
777
|
+
|
778
|
+
if (this._s === 0) {
|
779
|
+
return -n._s;
|
780
|
+
}
|
781
|
+
|
782
|
+
if (this._s === n._s) { // both positive or both negative
|
783
|
+
var cmp = this.compareAbs(n);
|
784
|
+
return cmp * this._s;
|
785
|
+
}
|
786
|
+
else {
|
787
|
+
return this._s;
|
788
|
+
}
|
789
|
+
};
|
790
|
+
|
791
|
+
/*
|
792
|
+
Function: isUnit
|
793
|
+
Return true iff *this* is either 1 or -1.
|
794
|
+
|
795
|
+
Returns:
|
796
|
+
|
797
|
+
true if *this* compares equal to <BigInteger.ONE> or <BigInteger.M_ONE>.
|
798
|
+
|
799
|
+
See Also:
|
800
|
+
|
801
|
+
<isZero>, <isNegative>, <isPositive>, <compareAbs>, <compare>,
|
802
|
+
<BigInteger.ONE>, <BigInteger.M_ONE>
|
803
|
+
*/
|
804
|
+
BigInteger.prototype.isUnit = function() {
|
805
|
+
return this === BigInteger.ONE ||
|
806
|
+
this === BigInteger.M_ONE ||
|
807
|
+
(this._d.length === 1 && this._d[0] === 1);
|
808
|
+
};
|
809
|
+
|
810
|
+
/*
|
811
|
+
Function: multiply
|
812
|
+
Multiply two <BigIntegers>.
|
813
|
+
|
814
|
+
Parameters:
|
815
|
+
|
816
|
+
n - The number to multiply *this* by. Will be converted to a
|
817
|
+
<BigInteger>.
|
818
|
+
|
819
|
+
Returns:
|
820
|
+
|
821
|
+
The numbers multiplied together.
|
822
|
+
|
823
|
+
See Also:
|
824
|
+
|
825
|
+
<add>, <subtract>, <quotient>, <square>
|
826
|
+
*/
|
827
|
+
BigInteger.prototype.multiply = function(n) {
|
828
|
+
// TODO: Consider adding Karatsuba multiplication for large numbers
|
829
|
+
if (this._s === 0) {
|
830
|
+
return BigInteger.ZERO;
|
831
|
+
}
|
832
|
+
|
833
|
+
n = BigInteger(n);
|
834
|
+
if (n._s === 0) {
|
835
|
+
return BigInteger.ZERO;
|
836
|
+
}
|
837
|
+
if (this.isUnit()) {
|
838
|
+
if (this._s < 0) {
|
839
|
+
return n.negate();
|
840
|
+
}
|
841
|
+
return n;
|
842
|
+
}
|
843
|
+
if (n.isUnit()) {
|
844
|
+
if (n._s < 0) {
|
845
|
+
return this.negate();
|
846
|
+
}
|
847
|
+
return this;
|
848
|
+
}
|
849
|
+
if (this === n) {
|
850
|
+
return this.square();
|
851
|
+
}
|
852
|
+
|
853
|
+
var r = (this._d.length >= n._d.length);
|
854
|
+
var a = (r ? this : n)._d; // a will be longer than b
|
855
|
+
var b = (r ? n : this)._d;
|
856
|
+
var al = a.length;
|
857
|
+
var bl = b.length;
|
858
|
+
|
859
|
+
var pl = al + bl;
|
860
|
+
var partial = new Array(pl);
|
861
|
+
var i;
|
862
|
+
for (i = 0; i < pl; i++) {
|
863
|
+
partial[i] = 0;
|
864
|
+
}
|
865
|
+
|
866
|
+
for (i = 0; i < bl; i++) {
|
867
|
+
var carry = 0;
|
868
|
+
var bi = b[i];
|
869
|
+
var jlimit = al + i;
|
870
|
+
var digit;
|
871
|
+
for (var j = i; j < jlimit; j++) {
|
872
|
+
digit = partial[j] + bi * a[j - i] + carry;
|
873
|
+
carry = (digit / BigInteger.base) | 0;
|
874
|
+
partial[j] = (digit % BigInteger.base) | 0;
|
875
|
+
}
|
876
|
+
if (carry) {
|
877
|
+
digit = partial[j] + carry;
|
878
|
+
carry = (digit / BigInteger.base) | 0;
|
879
|
+
partial[j] = digit % BigInteger.base;
|
880
|
+
}
|
881
|
+
}
|
882
|
+
return new BigInteger(partial, this._s * n._s);
|
883
|
+
};
|
884
|
+
|
885
|
+
// Multiply a BigInteger by a single-digit native number
|
886
|
+
// Assumes that this and n are >= 0
|
887
|
+
// This is not really intended to be used outside the library itself
|
888
|
+
BigInteger.prototype.multiplySingleDigit = function(n) {
|
889
|
+
if (n === 0 || this._s === 0) {
|
890
|
+
return BigInteger.ZERO;
|
891
|
+
}
|
892
|
+
if (n === 1) {
|
893
|
+
return this;
|
894
|
+
}
|
895
|
+
|
896
|
+
var digit;
|
897
|
+
if (this._d.length === 1) {
|
898
|
+
digit = this._d[0] * n;
|
899
|
+
if (digit >= BigInteger.base) {
|
900
|
+
return new BigInteger([(digit % BigInteger.base)|0,
|
901
|
+
(digit / BigInteger.base)|0], 1);
|
902
|
+
}
|
903
|
+
return new BigInteger([digit], 1);
|
904
|
+
}
|
905
|
+
|
906
|
+
if (n === 2) {
|
907
|
+
return this.add(this);
|
908
|
+
}
|
909
|
+
if (this.isUnit()) {
|
910
|
+
return new BigInteger([n], 1);
|
911
|
+
}
|
912
|
+
|
913
|
+
var a = this._d;
|
914
|
+
var al = a.length;
|
915
|
+
|
916
|
+
var pl = al + 1;
|
917
|
+
var partial = new Array(pl);
|
918
|
+
for (var i = 0; i < pl; i++) {
|
919
|
+
partial[i] = 0;
|
920
|
+
}
|
921
|
+
|
922
|
+
var carry = 0;
|
923
|
+
for (var j = 0; j < al; j++) {
|
924
|
+
digit = n * a[j] + carry;
|
925
|
+
carry = (digit / BigInteger.base) | 0;
|
926
|
+
partial[j] = (digit % BigInteger.base) | 0;
|
927
|
+
}
|
928
|
+
if (carry) {
|
929
|
+
digit = carry;
|
930
|
+
carry = (digit / BigInteger.base) | 0;
|
931
|
+
partial[j] = digit % BigInteger.base;
|
932
|
+
}
|
933
|
+
|
934
|
+
return new BigInteger(partial, 1);
|
935
|
+
};
|
936
|
+
|
937
|
+
/*
|
938
|
+
Function: square
|
939
|
+
Multiply a <BigInteger> by itself.
|
940
|
+
|
941
|
+
This is slightly faster than regular multiplication, since it removes the
|
942
|
+
duplicated multiplcations.
|
943
|
+
|
944
|
+
Returns:
|
945
|
+
|
946
|
+
> this.multiply(this)
|
947
|
+
|
948
|
+
See Also:
|
949
|
+
<multiply>
|
950
|
+
*/
|
951
|
+
BigInteger.prototype.square = function() {
|
952
|
+
// Normally, squaring a 10-digit number would take 100 multiplications.
|
953
|
+
// Of these 10 are unique diagonals, of the remaining 90 (100-10), 45 are repeated.
|
954
|
+
// This procedure saves (N*(N-1))/2 multiplications, (e.g., 45 of 100 multiplies).
|
955
|
+
// Based on code by Gary Darby, Intellitech Systems Inc., www.DelphiForFun.org
|
956
|
+
|
957
|
+
if (this._s === 0) {
|
958
|
+
return BigInteger.ZERO;
|
959
|
+
}
|
960
|
+
if (this.isUnit()) {
|
961
|
+
return BigInteger.ONE;
|
962
|
+
}
|
963
|
+
|
964
|
+
var digits = this._d;
|
965
|
+
var length = digits.length;
|
966
|
+
var imult1 = new Array(length + length + 1);
|
967
|
+
var product, carry, k;
|
968
|
+
var i;
|
969
|
+
|
970
|
+
// Calculate diagonal
|
971
|
+
for (i = 0; i < length; i++) {
|
972
|
+
k = i * 2;
|
973
|
+
product = digits[i] * digits[i];
|
974
|
+
carry = (product / BigInteger.base) | 0;
|
975
|
+
imult1[k] = product % BigInteger.base;
|
976
|
+
imult1[k + 1] = carry;
|
977
|
+
}
|
978
|
+
|
979
|
+
// Calculate repeating part
|
980
|
+
for (i = 0; i < length; i++) {
|
981
|
+
carry = 0;
|
982
|
+
k = i * 2 + 1;
|
983
|
+
for (var j = i + 1; j < length; j++, k++) {
|
984
|
+
product = digits[j] * digits[i] * 2 + imult1[k] + carry;
|
985
|
+
carry = (product / BigInteger.base) | 0;
|
986
|
+
imult1[k] = product % BigInteger.base;
|
987
|
+
}
|
988
|
+
k = length + i;
|
989
|
+
var digit = carry + imult1[k];
|
990
|
+
carry = (digit / BigInteger.base) | 0;
|
991
|
+
imult1[k] = digit % BigInteger.base;
|
992
|
+
imult1[k + 1] += carry;
|
993
|
+
}
|
994
|
+
|
995
|
+
return new BigInteger(imult1, 1);
|
996
|
+
};
|
997
|
+
|
998
|
+
/*
|
999
|
+
Function: quotient
|
1000
|
+
Divide two <BigIntegers> and truncate towards zero.
|
1001
|
+
|
1002
|
+
<quotient> throws an exception if *n* is zero.
|
1003
|
+
|
1004
|
+
Parameters:
|
1005
|
+
|
1006
|
+
n - The number to divide *this* by. Will be converted to a <BigInteger>.
|
1007
|
+
|
1008
|
+
Returns:
|
1009
|
+
|
1010
|
+
The *this* / *n*, truncated to an integer.
|
1011
|
+
|
1012
|
+
See Also:
|
1013
|
+
|
1014
|
+
<add>, <subtract>, <multiply>, <divRem>, <remainder>
|
1015
|
+
*/
|
1016
|
+
BigInteger.prototype.quotient = function(n) {
|
1017
|
+
return this.divRem(n)[0];
|
1018
|
+
};
|
1019
|
+
|
1020
|
+
/*
|
1021
|
+
Function: divide
|
1022
|
+
Deprecated synonym for <quotient>.
|
1023
|
+
*/
|
1024
|
+
BigInteger.prototype.divide = BigInteger.prototype.quotient;
|
1025
|
+
|
1026
|
+
/*
|
1027
|
+
Function: remainder
|
1028
|
+
Calculate the remainder of two <BigIntegers>.
|
1029
|
+
|
1030
|
+
<remainder> throws an exception if *n* is zero.
|
1031
|
+
|
1032
|
+
Parameters:
|
1033
|
+
|
1034
|
+
n - The remainder after *this* is divided *this* by *n*. Will be
|
1035
|
+
converted to a <BigInteger>.
|
1036
|
+
|
1037
|
+
Returns:
|
1038
|
+
|
1039
|
+
*this* % *n*.
|
1040
|
+
|
1041
|
+
See Also:
|
1042
|
+
|
1043
|
+
<divRem>, <quotient>
|
1044
|
+
*/
|
1045
|
+
BigInteger.prototype.remainder = function(n) {
|
1046
|
+
return this.divRem(n)[1];
|
1047
|
+
};
|
1048
|
+
|
1049
|
+
/*
|
1050
|
+
Function: divRem
|
1051
|
+
Calculate the integer quotient and remainder of two <BigIntegers>.
|
1052
|
+
|
1053
|
+
<divRem> throws an exception if *n* is zero.
|
1054
|
+
|
1055
|
+
Parameters:
|
1056
|
+
|
1057
|
+
n - The number to divide *this* by. Will be converted to a <BigInteger>.
|
1058
|
+
|
1059
|
+
Returns:
|
1060
|
+
|
1061
|
+
A two-element array containing the quotient and the remainder.
|
1062
|
+
|
1063
|
+
> a.divRem(b)
|
1064
|
+
|
1065
|
+
is exactly equivalent to
|
1066
|
+
|
1067
|
+
> [a.quotient(b), a.remainder(b)]
|
1068
|
+
|
1069
|
+
except it is faster, because they are calculated at the same time.
|
1070
|
+
|
1071
|
+
See Also:
|
1072
|
+
|
1073
|
+
<quotient>, <remainder>
|
1074
|
+
*/
|
1075
|
+
BigInteger.prototype.divRem = function(n) {
|
1076
|
+
n = BigInteger(n);
|
1077
|
+
if (n._s === 0) {
|
1078
|
+
throw new Error("Divide by zero");
|
1079
|
+
}
|
1080
|
+
if (this._s === 0) {
|
1081
|
+
return [BigInteger.ZERO, BigInteger.ZERO];
|
1082
|
+
}
|
1083
|
+
if (n._d.length === 1) {
|
1084
|
+
return this.divRemSmall(n._s * n._d[0]);
|
1085
|
+
}
|
1086
|
+
|
1087
|
+
// Test for easy cases -- |n1| <= |n2|
|
1088
|
+
switch (this.compareAbs(n)) {
|
1089
|
+
case 0: // n1 == n2
|
1090
|
+
return [this._s === n._s ? BigInteger.ONE : BigInteger.M_ONE, BigInteger.ZERO];
|
1091
|
+
case -1: // |n1| < |n2|
|
1092
|
+
return [BigInteger.ZERO, this];
|
1093
|
+
}
|
1094
|
+
|
1095
|
+
var sign = this._s * n._s;
|
1096
|
+
var a = n.abs();
|
1097
|
+
var b_digits = this._d.slice();
|
1098
|
+
var digits = n._d.length;
|
1099
|
+
var max = b_digits.length;
|
1100
|
+
var quot = [];
|
1101
|
+
var guess;
|
1102
|
+
|
1103
|
+
var part = new BigInteger([], 1);
|
1104
|
+
part._s = 1;
|
1105
|
+
|
1106
|
+
while (b_digits.length) {
|
1107
|
+
part._d.unshift(b_digits.pop());
|
1108
|
+
part = new BigInteger(part._d, 1);
|
1109
|
+
|
1110
|
+
if (part.compareAbs(n) < 0) {
|
1111
|
+
quot.push(0);
|
1112
|
+
continue;
|
1113
|
+
}
|
1114
|
+
if (part._s === 0) {
|
1115
|
+
guess = 0;
|
1116
|
+
}
|
1117
|
+
else {
|
1118
|
+
var xlen = part._d.length, ylen = a._d.length;
|
1119
|
+
var highx = part._d[xlen-1]*BigInteger.base + part._d[xlen-2];
|
1120
|
+
var highy = a._d[ylen-1]*BigInteger.base + a._d[ylen-2];
|
1121
|
+
if (part._d.length > a._d.length) {
|
1122
|
+
// The length of part._d can either match a._d length,
|
1123
|
+
// or exceed it by one.
|
1124
|
+
highx = (highx+1)*BigInteger.base;
|
1125
|
+
}
|
1126
|
+
guess = Math.ceil(highx/highy);
|
1127
|
+
}
|
1128
|
+
do {
|
1129
|
+
var check = a.multiplySingleDigit(guess);
|
1130
|
+
if (check.compareAbs(part) <= 0) {
|
1131
|
+
break;
|
1132
|
+
}
|
1133
|
+
guess--;
|
1134
|
+
} while (guess);
|
1135
|
+
|
1136
|
+
quot.push(guess);
|
1137
|
+
if (!guess) {
|
1138
|
+
continue;
|
1139
|
+
}
|
1140
|
+
var diff = part.subtract(check);
|
1141
|
+
part._d = diff._d.slice();
|
1142
|
+
}
|
1143
|
+
|
1144
|
+
return [new BigInteger(quot.reverse(), sign),
|
1145
|
+
new BigInteger(part._d, this._s)];
|
1146
|
+
};
|
1147
|
+
|
1148
|
+
// Throws an exception if n is outside of (-BigInteger.base, -1] or
|
1149
|
+
// [1, BigInteger.base). It's not necessary to call this, since the
|
1150
|
+
// other division functions will call it if they are able to.
|
1151
|
+
BigInteger.prototype.divRemSmall = function(n) {
|
1152
|
+
var r;
|
1153
|
+
n = +n;
|
1154
|
+
if (n === 0) {
|
1155
|
+
throw new Error("Divide by zero");
|
1156
|
+
}
|
1157
|
+
|
1158
|
+
var n_s = n < 0 ? -1 : 1;
|
1159
|
+
var sign = this._s * n_s;
|
1160
|
+
n = Math.abs(n);
|
1161
|
+
|
1162
|
+
if (n < 1 || n >= BigInteger.base) {
|
1163
|
+
throw new Error("Argument out of range");
|
1164
|
+
}
|
1165
|
+
|
1166
|
+
if (this._s === 0) {
|
1167
|
+
return [BigInteger.ZERO, BigInteger.ZERO];
|
1168
|
+
}
|
1169
|
+
|
1170
|
+
if (n === 1 || n === -1) {
|
1171
|
+
return [(sign === 1) ? this.abs() : new BigInteger(this._d, sign), BigInteger.ZERO];
|
1172
|
+
}
|
1173
|
+
|
1174
|
+
// 2 <= n < BigInteger.base
|
1175
|
+
|
1176
|
+
// divide a single digit by a single digit
|
1177
|
+
if (this._d.length === 1) {
|
1178
|
+
var q = new BigInteger([(this._d[0] / n) | 0], 1);
|
1179
|
+
r = new BigInteger([(this._d[0] % n) | 0], 1);
|
1180
|
+
if (sign < 0) {
|
1181
|
+
q = q.negate();
|
1182
|
+
}
|
1183
|
+
if (this._s < 0) {
|
1184
|
+
r = r.negate();
|
1185
|
+
}
|
1186
|
+
return [q, r];
|
1187
|
+
}
|
1188
|
+
|
1189
|
+
var digits = this._d.slice();
|
1190
|
+
var quot = new Array(digits.length);
|
1191
|
+
var part = 0;
|
1192
|
+
var diff = 0;
|
1193
|
+
var i = 0;
|
1194
|
+
var guess;
|
1195
|
+
|
1196
|
+
while (digits.length) {
|
1197
|
+
part = part * BigInteger.base + digits[digits.length - 1];
|
1198
|
+
if (part < n) {
|
1199
|
+
quot[i++] = 0;
|
1200
|
+
digits.pop();
|
1201
|
+
diff = BigInteger.base * diff + part;
|
1202
|
+
continue;
|
1203
|
+
}
|
1204
|
+
if (part === 0) {
|
1205
|
+
guess = 0;
|
1206
|
+
}
|
1207
|
+
else {
|
1208
|
+
guess = (part / n) | 0;
|
1209
|
+
}
|
1210
|
+
|
1211
|
+
var check = n * guess;
|
1212
|
+
diff = part - check;
|
1213
|
+
quot[i++] = guess;
|
1214
|
+
if (!guess) {
|
1215
|
+
digits.pop();
|
1216
|
+
continue;
|
1217
|
+
}
|
1218
|
+
|
1219
|
+
digits.pop();
|
1220
|
+
part = diff;
|
1221
|
+
}
|
1222
|
+
|
1223
|
+
r = new BigInteger([diff], 1);
|
1224
|
+
if (this._s < 0) {
|
1225
|
+
r = r.negate();
|
1226
|
+
}
|
1227
|
+
return [new BigInteger(quot.reverse(), sign), r];
|
1228
|
+
};
|
1229
|
+
|
1230
|
+
/*
|
1231
|
+
Function: isEven
|
1232
|
+
Return true iff *this* is divisible by two.
|
1233
|
+
|
1234
|
+
Note that <BigInteger.ZERO> is even.
|
1235
|
+
|
1236
|
+
Returns:
|
1237
|
+
|
1238
|
+
true if *this* is even, false otherwise.
|
1239
|
+
|
1240
|
+
See Also:
|
1241
|
+
|
1242
|
+
<isOdd>
|
1243
|
+
*/
|
1244
|
+
BigInteger.prototype.isEven = function() {
|
1245
|
+
var digits = this._d;
|
1246
|
+
return this._s === 0 || digits.length === 0 || (digits[0] % 2) === 0;
|
1247
|
+
};
|
1248
|
+
|
1249
|
+
/*
|
1250
|
+
Function: isOdd
|
1251
|
+
Return true iff *this* is not divisible by two.
|
1252
|
+
|
1253
|
+
Returns:
|
1254
|
+
|
1255
|
+
true if *this* is odd, false otherwise.
|
1256
|
+
|
1257
|
+
See Also:
|
1258
|
+
|
1259
|
+
<isEven>
|
1260
|
+
*/
|
1261
|
+
BigInteger.prototype.isOdd = function() {
|
1262
|
+
return !this.isEven();
|
1263
|
+
};
|
1264
|
+
|
1265
|
+
/*
|
1266
|
+
Function: sign
|
1267
|
+
Get the sign of a <BigInteger>.
|
1268
|
+
|
1269
|
+
Returns:
|
1270
|
+
|
1271
|
+
* -1 if *this* < 0
|
1272
|
+
* 0 if *this* == 0
|
1273
|
+
* +1 if *this* > 0
|
1274
|
+
|
1275
|
+
See Also:
|
1276
|
+
|
1277
|
+
<isZero>, <isPositive>, <isNegative>, <compare>, <BigInteger.ZERO>
|
1278
|
+
*/
|
1279
|
+
BigInteger.prototype.sign = function() {
|
1280
|
+
return this._s;
|
1281
|
+
};
|
1282
|
+
|
1283
|
+
/*
|
1284
|
+
Function: isPositive
|
1285
|
+
Return true iff *this* > 0.
|
1286
|
+
|
1287
|
+
Returns:
|
1288
|
+
|
1289
|
+
true if *this*.compare(<BigInteger.ZERO>) == 1.
|
1290
|
+
|
1291
|
+
See Also:
|
1292
|
+
|
1293
|
+
<sign>, <isZero>, <isNegative>, <isUnit>, <compare>, <BigInteger.ZERO>
|
1294
|
+
*/
|
1295
|
+
BigInteger.prototype.isPositive = function() {
|
1296
|
+
return this._s > 0;
|
1297
|
+
};
|
1298
|
+
|
1299
|
+
/*
|
1300
|
+
Function: isNegative
|
1301
|
+
Return true iff *this* < 0.
|
1302
|
+
|
1303
|
+
Returns:
|
1304
|
+
|
1305
|
+
true if *this*.compare(<BigInteger.ZERO>) == -1.
|
1306
|
+
|
1307
|
+
See Also:
|
1308
|
+
|
1309
|
+
<sign>, <isPositive>, <isZero>, <isUnit>, <compare>, <BigInteger.ZERO>
|
1310
|
+
*/
|
1311
|
+
BigInteger.prototype.isNegative = function() {
|
1312
|
+
return this._s < 0;
|
1313
|
+
};
|
1314
|
+
|
1315
|
+
/*
|
1316
|
+
Function: isZero
|
1317
|
+
Return true iff *this* == 0.
|
1318
|
+
|
1319
|
+
Returns:
|
1320
|
+
|
1321
|
+
true if *this*.compare(<BigInteger.ZERO>) == 0.
|
1322
|
+
|
1323
|
+
See Also:
|
1324
|
+
|
1325
|
+
<sign>, <isPositive>, <isNegative>, <isUnit>, <BigInteger.ZERO>
|
1326
|
+
*/
|
1327
|
+
BigInteger.prototype.isZero = function() {
|
1328
|
+
return this._s === 0;
|
1329
|
+
};
|
1330
|
+
|
1331
|
+
/*
|
1332
|
+
Function: exp10
|
1333
|
+
Multiply a <BigInteger> by a power of 10.
|
1334
|
+
|
1335
|
+
This is equivalent to, but faster than
|
1336
|
+
|
1337
|
+
> if (n >= 0) {
|
1338
|
+
> return this.multiply(BigInteger("1e" + n));
|
1339
|
+
> }
|
1340
|
+
> else { // n <= 0
|
1341
|
+
> return this.quotient(BigInteger("1e" + -n));
|
1342
|
+
> }
|
1343
|
+
|
1344
|
+
Parameters:
|
1345
|
+
|
1346
|
+
n - The power of 10 to multiply *this* by. *n* is converted to a
|
1347
|
+
javascipt number and must be no greater than <BigInteger.MAX_EXP>
|
1348
|
+
(0x7FFFFFFF), or an exception will be thrown.
|
1349
|
+
|
1350
|
+
Returns:
|
1351
|
+
|
1352
|
+
*this* * (10 ** *n*), truncated to an integer if necessary.
|
1353
|
+
|
1354
|
+
See Also:
|
1355
|
+
|
1356
|
+
<pow>, <multiply>
|
1357
|
+
*/
|
1358
|
+
BigInteger.prototype.exp10 = function(n) {
|
1359
|
+
n = +n;
|
1360
|
+
if (n === 0) {
|
1361
|
+
return this;
|
1362
|
+
}
|
1363
|
+
if (Math.abs(n) > Number(BigInteger.MAX_EXP)) {
|
1364
|
+
throw new Error("exponent too large in BigInteger.exp10");
|
1365
|
+
}
|
1366
|
+
if (n > 0) {
|
1367
|
+
var k = new BigInteger(this._d.slice(), this._s);
|
1368
|
+
|
1369
|
+
for (; n >= BigInteger.base_log10; n -= BigInteger.base_log10) {
|
1370
|
+
k._d.unshift(0);
|
1371
|
+
}
|
1372
|
+
if (n == 0)
|
1373
|
+
return k;
|
1374
|
+
k._s = 1;
|
1375
|
+
k = k.multiplySingleDigit(Math.pow(10, n));
|
1376
|
+
return (this._s < 0 ? k.negate() : k);
|
1377
|
+
} else if (-n >= this._d.length*BigInteger.base_log10) {
|
1378
|
+
return BigInteger.ZERO;
|
1379
|
+
} else {
|
1380
|
+
var k = new BigInteger(this._d.slice(), this._s);
|
1381
|
+
|
1382
|
+
for (n = -n; n >= BigInteger.base_log10; n -= BigInteger.base_log10) {
|
1383
|
+
k._d.shift();
|
1384
|
+
}
|
1385
|
+
return (n == 0) ? k : k.divRemSmall(Math.pow(10, n))[0];
|
1386
|
+
}
|
1387
|
+
};
|
1388
|
+
|
1389
|
+
/*
|
1390
|
+
Function: pow
|
1391
|
+
Raise a <BigInteger> to a power.
|
1392
|
+
|
1393
|
+
In this implementation, 0**0 is 1.
|
1394
|
+
|
1395
|
+
Parameters:
|
1396
|
+
|
1397
|
+
n - The exponent to raise *this* by. *n* must be no greater than
|
1398
|
+
<BigInteger.MAX_EXP> (0x7FFFFFFF), or an exception will be thrown.
|
1399
|
+
|
1400
|
+
Returns:
|
1401
|
+
|
1402
|
+
*this* raised to the *nth* power.
|
1403
|
+
|
1404
|
+
See Also:
|
1405
|
+
|
1406
|
+
<modPow>
|
1407
|
+
*/
|
1408
|
+
BigInteger.prototype.pow = function(n) {
|
1409
|
+
if (this.isUnit()) {
|
1410
|
+
if (this._s > 0) {
|
1411
|
+
return this;
|
1412
|
+
}
|
1413
|
+
else {
|
1414
|
+
return BigInteger(n).isOdd() ? this : this.negate();
|
1415
|
+
}
|
1416
|
+
}
|
1417
|
+
|
1418
|
+
n = BigInteger(n);
|
1419
|
+
if (n._s === 0) {
|
1420
|
+
return BigInteger.ONE;
|
1421
|
+
}
|
1422
|
+
else if (n._s < 0) {
|
1423
|
+
if (this._s === 0) {
|
1424
|
+
throw new Error("Divide by zero");
|
1425
|
+
}
|
1426
|
+
else {
|
1427
|
+
return BigInteger.ZERO;
|
1428
|
+
}
|
1429
|
+
}
|
1430
|
+
if (this._s === 0) {
|
1431
|
+
return BigInteger.ZERO;
|
1432
|
+
}
|
1433
|
+
if (n.isUnit()) {
|
1434
|
+
return this;
|
1435
|
+
}
|
1436
|
+
|
1437
|
+
if (n.compareAbs(BigInteger.MAX_EXP) > 0) {
|
1438
|
+
throw new Error("exponent too large in BigInteger.pow");
|
1439
|
+
}
|
1440
|
+
var x = this;
|
1441
|
+
var aux = BigInteger.ONE;
|
1442
|
+
var two = BigInteger.small[2];
|
1443
|
+
|
1444
|
+
while (n.isPositive()) {
|
1445
|
+
if (n.isOdd()) {
|
1446
|
+
aux = aux.multiply(x);
|
1447
|
+
if (n.isUnit()) {
|
1448
|
+
return aux;
|
1449
|
+
}
|
1450
|
+
}
|
1451
|
+
x = x.square();
|
1452
|
+
n = n.quotient(two);
|
1453
|
+
}
|
1454
|
+
|
1455
|
+
return aux;
|
1456
|
+
};
|
1457
|
+
|
1458
|
+
/*
|
1459
|
+
Function: modPow
|
1460
|
+
Raise a <BigInteger> to a power (mod m).
|
1461
|
+
|
1462
|
+
Because it is reduced by a modulus, <modPow> is not limited by
|
1463
|
+
<BigInteger.MAX_EXP> like <pow>.
|
1464
|
+
|
1465
|
+
Parameters:
|
1466
|
+
|
1467
|
+
exponent - The exponent to raise *this* by. Must be positive.
|
1468
|
+
modulus - The modulus.
|
1469
|
+
|
1470
|
+
Returns:
|
1471
|
+
|
1472
|
+
*this* ^ *exponent* (mod *modulus*).
|
1473
|
+
|
1474
|
+
See Also:
|
1475
|
+
|
1476
|
+
<pow>, <mod>
|
1477
|
+
*/
|
1478
|
+
BigInteger.prototype.modPow = function(exponent, modulus) {
|
1479
|
+
var result = BigInteger.ONE;
|
1480
|
+
var base = this;
|
1481
|
+
|
1482
|
+
while (exponent.isPositive()) {
|
1483
|
+
if (exponent.isOdd()) {
|
1484
|
+
result = result.multiply(base).remainder(modulus);
|
1485
|
+
}
|
1486
|
+
|
1487
|
+
exponent = exponent.quotient(BigInteger.small[2]);
|
1488
|
+
if (exponent.isPositive()) {
|
1489
|
+
base = base.square().remainder(modulus);
|
1490
|
+
}
|
1491
|
+
}
|
1492
|
+
|
1493
|
+
return result;
|
1494
|
+
};
|
1495
|
+
|
1496
|
+
/*
|
1497
|
+
Function: log
|
1498
|
+
Get the natural logarithm of a <BigInteger> as a native JavaScript number.
|
1499
|
+
|
1500
|
+
This is equivalent to
|
1501
|
+
|
1502
|
+
> Math.log(this.toJSValue())
|
1503
|
+
|
1504
|
+
but handles values outside of the native number range.
|
1505
|
+
|
1506
|
+
Returns:
|
1507
|
+
|
1508
|
+
log( *this* )
|
1509
|
+
|
1510
|
+
See Also:
|
1511
|
+
|
1512
|
+
<toJSValue>
|
1513
|
+
*/
|
1514
|
+
BigInteger.prototype.log = function() {
|
1515
|
+
switch (this._s) {
|
1516
|
+
case 0: return -Infinity;
|
1517
|
+
case -1: return NaN;
|
1518
|
+
default: // Fall through.
|
1519
|
+
}
|
1520
|
+
|
1521
|
+
var l = this._d.length;
|
1522
|
+
|
1523
|
+
if (l*BigInteger.base_log10 < 30) {
|
1524
|
+
return Math.log(this.valueOf());
|
1525
|
+
}
|
1526
|
+
|
1527
|
+
var N = Math.ceil(30/BigInteger.base_log10);
|
1528
|
+
var firstNdigits = this._d.slice(l - N);
|
1529
|
+
return Math.log((new BigInteger(firstNdigits, 1)).valueOf()) + (l - N) * Math.log(BigInteger.base);
|
1530
|
+
};
|
1531
|
+
|
1532
|
+
/*
|
1533
|
+
Function: valueOf
|
1534
|
+
Convert a <BigInteger> to a native JavaScript integer.
|
1535
|
+
|
1536
|
+
This is called automatically by JavaScipt to convert a <BigInteger> to a
|
1537
|
+
native value.
|
1538
|
+
|
1539
|
+
Returns:
|
1540
|
+
|
1541
|
+
> parseInt(this.toString(), 10)
|
1542
|
+
|
1543
|
+
See Also:
|
1544
|
+
|
1545
|
+
<toString>, <toJSValue>
|
1546
|
+
*/
|
1547
|
+
BigInteger.prototype.valueOf = function() {
|
1548
|
+
return parseInt(this.toString(), 10);
|
1549
|
+
};
|
1550
|
+
|
1551
|
+
/*
|
1552
|
+
Function: toJSValue
|
1553
|
+
Convert a <BigInteger> to a native JavaScript integer.
|
1554
|
+
|
1555
|
+
This is the same as valueOf, but more explicitly named.
|
1556
|
+
|
1557
|
+
Returns:
|
1558
|
+
|
1559
|
+
> parseInt(this.toString(), 10)
|
1560
|
+
|
1561
|
+
See Also:
|
1562
|
+
|
1563
|
+
<toString>, <valueOf>
|
1564
|
+
*/
|
1565
|
+
BigInteger.prototype.toJSValue = function() {
|
1566
|
+
return parseInt(this.toString(), 10);
|
1567
|
+
};
|
1568
|
+
|
1569
|
+
// Constant: MAX_EXP
|
1570
|
+
// The largest exponent allowed in <pow> and <exp10> (0x7FFFFFFF or 2147483647).
|
1571
|
+
BigInteger.MAX_EXP = BigInteger(0x7FFFFFFF);
|
1572
|
+
|
1573
|
+
(function() {
|
1574
|
+
function makeUnary(fn) {
|
1575
|
+
return function(a) {
|
1576
|
+
return fn.call(BigInteger(a));
|
1577
|
+
};
|
1578
|
+
}
|
1579
|
+
|
1580
|
+
function makeBinary(fn) {
|
1581
|
+
return function(a, b) {
|
1582
|
+
return fn.call(BigInteger(a), BigInteger(b));
|
1583
|
+
};
|
1584
|
+
}
|
1585
|
+
|
1586
|
+
function makeTrinary(fn) {
|
1587
|
+
return function(a, b, c) {
|
1588
|
+
return fn.call(BigInteger(a), BigInteger(b), BigInteger(c));
|
1589
|
+
};
|
1590
|
+
}
|
1591
|
+
|
1592
|
+
(function() {
|
1593
|
+
var i, fn;
|
1594
|
+
var unary = "toJSValue,isEven,isOdd,sign,isZero,isNegative,abs,isUnit,square,negate,isPositive,toString,next,prev,log".split(",");
|
1595
|
+
var binary = "compare,remainder,divRem,subtract,add,quotient,divide,multiply,pow,compareAbs".split(",");
|
1596
|
+
var trinary = ["modPow"];
|
1597
|
+
|
1598
|
+
for (i = 0; i < unary.length; i++) {
|
1599
|
+
fn = unary[i];
|
1600
|
+
BigInteger[fn] = makeUnary(BigInteger.prototype[fn]);
|
1601
|
+
}
|
1602
|
+
|
1603
|
+
for (i = 0; i < binary.length; i++) {
|
1604
|
+
fn = binary[i];
|
1605
|
+
BigInteger[fn] = makeBinary(BigInteger.prototype[fn]);
|
1606
|
+
}
|
1607
|
+
|
1608
|
+
for (i = 0; i < trinary.length; i++) {
|
1609
|
+
fn = trinary[i];
|
1610
|
+
BigInteger[fn] = makeTrinary(BigInteger.prototype[fn]);
|
1611
|
+
}
|
1612
|
+
|
1613
|
+
BigInteger.exp10 = function(x, n) {
|
1614
|
+
return BigInteger(x).exp10(n);
|
1615
|
+
};
|
1616
|
+
})();
|
1617
|
+
})();
|
1618
|
+
|
1619
|
+
if (typeof exports !== 'undefined') {
|
1620
|
+
exports.BigInteger = BigInteger;
|
1621
|
+
}
|