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