BigInteger.js 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  1. var bigInt = (function (undefined) {
  2. "use strict";
  3. var BASE = 1e7,
  4. LOG_BASE = 7,
  5. MAX_INT = 9007199254740992,
  6. MAX_INT_ARR = smallToArray(MAX_INT),
  7. DEFAULT_ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyz";
  8. var supportsNativeBigInt = typeof BigInt === "function";
  9. function Integer(v, radix, alphabet, caseSensitive) {
  10. if (typeof v === "undefined") return Integer[0];
  11. if (typeof radix !== "undefined") return +radix === 10 && !alphabet ? parseValue(v) : parseBase(v, radix, alphabet, caseSensitive);
  12. return parseValue(v);
  13. }
  14. function BigInteger(value, sign) {
  15. this.value = value;
  16. this.sign = sign;
  17. this.isSmall = false;
  18. }
  19. BigInteger.prototype = Object.create(Integer.prototype);
  20. function SmallInteger(value) {
  21. this.value = value;
  22. this.sign = value < 0;
  23. this.isSmall = true;
  24. }
  25. SmallInteger.prototype = Object.create(Integer.prototype);
  26. function NativeBigInt(value) {
  27. this.value = value;
  28. }
  29. NativeBigInt.prototype = Object.create(Integer.prototype);
  30. function isPrecise(n) {
  31. return -MAX_INT < n && n < MAX_INT;
  32. }
  33. function smallToArray(n) { // For performance reasons doesn't reference BASE, need to change this function if BASE changes
  34. if (n < 1e7)
  35. return [n];
  36. if (n < 1e14)
  37. return [n % 1e7, Math.floor(n / 1e7)];
  38. return [n % 1e7, Math.floor(n / 1e7) % 1e7, Math.floor(n / 1e14)];
  39. }
  40. function arrayToSmall(arr) { // If BASE changes this function may need to change
  41. trim(arr);
  42. var length = arr.length;
  43. if (length < 4 && compareAbs(arr, MAX_INT_ARR) < 0) {
  44. switch (length) {
  45. case 0: return 0;
  46. case 1: return arr[0];
  47. case 2: return arr[0] + arr[1] * BASE;
  48. default: return arr[0] + (arr[1] + arr[2] * BASE) * BASE;
  49. }
  50. }
  51. return arr;
  52. }
  53. function trim(v) {
  54. var i = v.length;
  55. while (v[--i] === 0);
  56. v.length = i + 1;
  57. }
  58. function createArray(length) { // function shamelessly stolen from Yaffle's library https://github.com/Yaffle/BigInteger
  59. var x = new Array(length);
  60. var i = -1;
  61. while (++i < length) {
  62. x[i] = 0;
  63. }
  64. return x;
  65. }
  66. function truncate(n) {
  67. if (n > 0) return Math.floor(n);
  68. return Math.ceil(n);
  69. }
  70. function add(a, b) { // assumes a and b are arrays with a.length >= b.length
  71. var l_a = a.length,
  72. l_b = b.length,
  73. r = new Array(l_a),
  74. carry = 0,
  75. base = BASE,
  76. sum, i;
  77. for (i = 0; i < l_b; i++) {
  78. sum = a[i] + b[i] + carry;
  79. carry = sum >= base ? 1 : 0;
  80. r[i] = sum - carry * base;
  81. }
  82. while (i < l_a) {
  83. sum = a[i] + carry;
  84. carry = sum === base ? 1 : 0;
  85. r[i++] = sum - carry * base;
  86. }
  87. if (carry > 0) r.push(carry);
  88. return r;
  89. }
  90. function addAny(a, b) {
  91. if (a.length >= b.length) return add(a, b);
  92. return add(b, a);
  93. }
  94. function addSmall(a, carry) { // assumes a is array, carry is number with 0 <= carry < MAX_INT
  95. var l = a.length,
  96. r = new Array(l),
  97. base = BASE,
  98. sum, i;
  99. for (i = 0; i < l; i++) {
  100. sum = a[i] - base + carry;
  101. carry = Math.floor(sum / base);
  102. r[i] = sum - carry * base;
  103. carry += 1;
  104. }
  105. while (carry > 0) {
  106. r[i++] = carry % base;
  107. carry = Math.floor(carry / base);
  108. }
  109. return r;
  110. }
  111. BigInteger.prototype.add = function (v) {
  112. var n = parseValue(v);
  113. if (this.sign !== n.sign) {
  114. return this.subtract(n.negate());
  115. }
  116. var a = this.value, b = n.value;
  117. if (n.isSmall) {
  118. return new BigInteger(addSmall(a, Math.abs(b)), this.sign);
  119. }
  120. return new BigInteger(addAny(a, b), this.sign);
  121. };
  122. BigInteger.prototype.plus = BigInteger.prototype.add;
  123. SmallInteger.prototype.add = function (v) {
  124. var n = parseValue(v);
  125. var a = this.value;
  126. if (a < 0 !== n.sign) {
  127. return this.subtract(n.negate());
  128. }
  129. var b = n.value;
  130. if (n.isSmall) {
  131. if (isPrecise(a + b)) return new SmallInteger(a + b);
  132. b = smallToArray(Math.abs(b));
  133. }
  134. return new BigInteger(addSmall(b, Math.abs(a)), a < 0);
  135. };
  136. SmallInteger.prototype.plus = SmallInteger.prototype.add;
  137. NativeBigInt.prototype.add = function (v) {
  138. return new NativeBigInt(this.value + parseValue(v).value);
  139. }
  140. NativeBigInt.prototype.plus = NativeBigInt.prototype.add;
  141. function subtract(a, b) { // assumes a and b are arrays with a >= b
  142. var a_l = a.length,
  143. b_l = b.length,
  144. r = new Array(a_l),
  145. borrow = 0,
  146. base = BASE,
  147. i, difference;
  148. for (i = 0; i < b_l; i++) {
  149. difference = a[i] - borrow - b[i];
  150. if (difference < 0) {
  151. difference += base;
  152. borrow = 1;
  153. } else borrow = 0;
  154. r[i] = difference;
  155. }
  156. for (i = b_l; i < a_l; i++) {
  157. difference = a[i] - borrow;
  158. if (difference < 0) difference += base;
  159. else {
  160. r[i++] = difference;
  161. break;
  162. }
  163. r[i] = difference;
  164. }
  165. for (; i < a_l; i++) {
  166. r[i] = a[i];
  167. }
  168. trim(r);
  169. return r;
  170. }
  171. function subtractAny(a, b, sign) {
  172. var value;
  173. if (compareAbs(a, b) >= 0) {
  174. value = subtract(a, b);
  175. } else {
  176. value = subtract(b, a);
  177. sign = !sign;
  178. }
  179. value = arrayToSmall(value);
  180. if (typeof value === "number") {
  181. if (sign) value = -value;
  182. return new SmallInteger(value);
  183. }
  184. return new BigInteger(value, sign);
  185. }
  186. function subtractSmall(a, b, sign) { // assumes a is array, b is number with 0 <= b < MAX_INT
  187. var l = a.length,
  188. r = new Array(l),
  189. carry = -b,
  190. base = BASE,
  191. i, difference;
  192. for (i = 0; i < l; i++) {
  193. difference = a[i] + carry;
  194. carry = Math.floor(difference / base);
  195. difference %= base;
  196. r[i] = difference < 0 ? difference + base : difference;
  197. }
  198. r = arrayToSmall(r);
  199. if (typeof r === "number") {
  200. if (sign) r = -r;
  201. return new SmallInteger(r);
  202. } return new BigInteger(r, sign);
  203. }
  204. BigInteger.prototype.subtract = function (v) {
  205. var n = parseValue(v);
  206. if (this.sign !== n.sign) {
  207. return this.add(n.negate());
  208. }
  209. var a = this.value, b = n.value;
  210. if (n.isSmall)
  211. return subtractSmall(a, Math.abs(b), this.sign);
  212. return subtractAny(a, b, this.sign);
  213. };
  214. BigInteger.prototype.minus = BigInteger.prototype.subtract;
  215. SmallInteger.prototype.subtract = function (v) {
  216. var n = parseValue(v);
  217. var a = this.value;
  218. if (a < 0 !== n.sign) {
  219. return this.add(n.negate());
  220. }
  221. var b = n.value;
  222. if (n.isSmall) {
  223. return new SmallInteger(a - b);
  224. }
  225. return subtractSmall(b, Math.abs(a), a >= 0);
  226. };
  227. SmallInteger.prototype.minus = SmallInteger.prototype.subtract;
  228. NativeBigInt.prototype.subtract = function (v) {
  229. return new NativeBigInt(this.value - parseValue(v).value);
  230. }
  231. NativeBigInt.prototype.minus = NativeBigInt.prototype.subtract;
  232. BigInteger.prototype.negate = function () {
  233. return new BigInteger(this.value, !this.sign);
  234. };
  235. SmallInteger.prototype.negate = function () {
  236. var sign = this.sign;
  237. var small = new SmallInteger(-this.value);
  238. small.sign = !sign;
  239. return small;
  240. };
  241. NativeBigInt.prototype.negate = function () {
  242. return new NativeBigInt(-this.value);
  243. }
  244. BigInteger.prototype.abs = function () {
  245. return new BigInteger(this.value, false);
  246. };
  247. SmallInteger.prototype.abs = function () {
  248. return new SmallInteger(Math.abs(this.value));
  249. };
  250. NativeBigInt.prototype.abs = function () {
  251. return new NativeBigInt(this.value >= 0 ? this.value : -this.value);
  252. }
  253. function multiplyLong(a, b) {
  254. var a_l = a.length,
  255. b_l = b.length,
  256. l = a_l + b_l,
  257. r = createArray(l),
  258. base = BASE,
  259. product, carry, i, a_i, b_j;
  260. for (i = 0; i < a_l; ++i) {
  261. a_i = a[i];
  262. for (var j = 0; j < b_l; ++j) {
  263. b_j = b[j];
  264. product = a_i * b_j + r[i + j];
  265. carry = Math.floor(product / base);
  266. r[i + j] = product - carry * base;
  267. r[i + j + 1] += carry;
  268. }
  269. }
  270. trim(r);
  271. return r;
  272. }
  273. function multiplySmall(a, b) { // assumes a is array, b is number with |b| < BASE
  274. var l = a.length,
  275. r = new Array(l),
  276. base = BASE,
  277. carry = 0,
  278. product, i;
  279. for (i = 0; i < l; i++) {
  280. product = a[i] * b + carry;
  281. carry = Math.floor(product / base);
  282. r[i] = product - carry * base;
  283. }
  284. while (carry > 0) {
  285. r[i++] = carry % base;
  286. carry = Math.floor(carry / base);
  287. }
  288. return r;
  289. }
  290. function shiftLeft(x, n) {
  291. var r = [];
  292. while (n-- > 0) r.push(0);
  293. return r.concat(x);
  294. }
  295. function multiplyKaratsuba(x, y) {
  296. var n = Math.max(x.length, y.length);
  297. if (n <= 30) return multiplyLong(x, y);
  298. n = Math.ceil(n / 2);
  299. var b = x.slice(n),
  300. a = x.slice(0, n),
  301. d = y.slice(n),
  302. c = y.slice(0, n);
  303. var ac = multiplyKaratsuba(a, c),
  304. bd = multiplyKaratsuba(b, d),
  305. abcd = multiplyKaratsuba(addAny(a, b), addAny(c, d));
  306. var product = addAny(addAny(ac, shiftLeft(subtract(subtract(abcd, ac), bd), n)), shiftLeft(bd, 2 * n));
  307. trim(product);
  308. return product;
  309. }
  310. // The following function is derived from a surface fit of a graph plotting the performance difference
  311. // between long multiplication and karatsuba multiplication versus the lengths of the two arrays.
  312. function useKaratsuba(l1, l2) {
  313. return -0.012 * l1 - 0.012 * l2 + 0.000015 * l1 * l2 > 0;
  314. }
  315. BigInteger.prototype.multiply = function (v) {
  316. var n = parseValue(v),
  317. a = this.value, b = n.value,
  318. sign = this.sign !== n.sign,
  319. abs;
  320. if (n.isSmall) {
  321. if (b === 0) return Integer[0];
  322. if (b === 1) return this;
  323. if (b === -1) return this.negate();
  324. abs = Math.abs(b);
  325. if (abs < BASE) {
  326. return new BigInteger(multiplySmall(a, abs), sign);
  327. }
  328. b = smallToArray(abs);
  329. }
  330. if (useKaratsuba(a.length, b.length)) // Karatsuba is only faster for certain array sizes
  331. return new BigInteger(multiplyKaratsuba(a, b), sign);
  332. return new BigInteger(multiplyLong(a, b), sign);
  333. };
  334. BigInteger.prototype.times = BigInteger.prototype.multiply;
  335. function multiplySmallAndArray(a, b, sign) { // a >= 0
  336. if (a < BASE) {
  337. return new BigInteger(multiplySmall(b, a), sign);
  338. }
  339. return new BigInteger(multiplyLong(b, smallToArray(a)), sign);
  340. }
  341. SmallInteger.prototype._multiplyBySmall = function (a) {
  342. if (isPrecise(a.value * this.value)) {
  343. return new SmallInteger(a.value * this.value);
  344. }
  345. return multiplySmallAndArray(Math.abs(a.value), smallToArray(Math.abs(this.value)), this.sign !== a.sign);
  346. };
  347. BigInteger.prototype._multiplyBySmall = function (a) {
  348. if (a.value === 0) return Integer[0];
  349. if (a.value === 1) return this;
  350. if (a.value === -1) return this.negate();
  351. return multiplySmallAndArray(Math.abs(a.value), this.value, this.sign !== a.sign);
  352. };
  353. SmallInteger.prototype.multiply = function (v) {
  354. return parseValue(v)._multiplyBySmall(this);
  355. };
  356. SmallInteger.prototype.times = SmallInteger.prototype.multiply;
  357. NativeBigInt.prototype.multiply = function (v) {
  358. return new NativeBigInt(this.value * parseValue(v).value);
  359. }
  360. NativeBigInt.prototype.times = NativeBigInt.prototype.multiply;
  361. function square(a) {
  362. //console.assert(2 * BASE * BASE < MAX_INT);
  363. var l = a.length,
  364. r = createArray(l + l),
  365. base = BASE,
  366. product, carry, i, a_i, a_j;
  367. for (i = 0; i < l; i++) {
  368. a_i = a[i];
  369. carry = 0 - a_i * a_i;
  370. for (var j = i; j < l; j++) {
  371. a_j = a[j];
  372. product = 2 * (a_i * a_j) + r[i + j] + carry;
  373. carry = Math.floor(product / base);
  374. r[i + j] = product - carry * base;
  375. }
  376. r[i + l] = carry;
  377. }
  378. trim(r);
  379. return r;
  380. }
  381. BigInteger.prototype.square = function () {
  382. return new BigInteger(square(this.value), false);
  383. };
  384. SmallInteger.prototype.square = function () {
  385. var value = this.value * this.value;
  386. if (isPrecise(value)) return new SmallInteger(value);
  387. return new BigInteger(square(smallToArray(Math.abs(this.value))), false);
  388. };
  389. NativeBigInt.prototype.square = function (v) {
  390. return new NativeBigInt(this.value * this.value);
  391. }
  392. function divMod1(a, b) { // Left over from previous version. Performs faster than divMod2 on smaller input sizes.
  393. var a_l = a.length,
  394. b_l = b.length,
  395. base = BASE,
  396. result = createArray(b.length),
  397. divisorMostSignificantDigit = b[b_l - 1],
  398. // normalization
  399. lambda = Math.ceil(base / (2 * divisorMostSignificantDigit)),
  400. remainder = multiplySmall(a, lambda),
  401. divisor = multiplySmall(b, lambda),
  402. quotientDigit, shift, carry, borrow, i, l, q;
  403. if (remainder.length <= a_l) remainder.push(0);
  404. divisor.push(0);
  405. divisorMostSignificantDigit = divisor[b_l - 1];
  406. for (shift = a_l - b_l; shift >= 0; shift--) {
  407. quotientDigit = base - 1;
  408. if (remainder[shift + b_l] !== divisorMostSignificantDigit) {
  409. quotientDigit = Math.floor((remainder[shift + b_l] * base + remainder[shift + b_l - 1]) / divisorMostSignificantDigit);
  410. }
  411. // quotientDigit <= base - 1
  412. carry = 0;
  413. borrow = 0;
  414. l = divisor.length;
  415. for (i = 0; i < l; i++) {
  416. carry += quotientDigit * divisor[i];
  417. q = Math.floor(carry / base);
  418. borrow += remainder[shift + i] - (carry - q * base);
  419. carry = q;
  420. if (borrow < 0) {
  421. remainder[shift + i] = borrow + base;
  422. borrow = -1;
  423. } else {
  424. remainder[shift + i] = borrow;
  425. borrow = 0;
  426. }
  427. }
  428. while (borrow !== 0) {
  429. quotientDigit -= 1;
  430. carry = 0;
  431. for (i = 0; i < l; i++) {
  432. carry += remainder[shift + i] - base + divisor[i];
  433. if (carry < 0) {
  434. remainder[shift + i] = carry + base;
  435. carry = 0;
  436. } else {
  437. remainder[shift + i] = carry;
  438. carry = 1;
  439. }
  440. }
  441. borrow += carry;
  442. }
  443. result[shift] = quotientDigit;
  444. }
  445. // denormalization
  446. remainder = divModSmall(remainder, lambda)[0];
  447. return [arrayToSmall(result), arrayToSmall(remainder)];
  448. }
  449. function divMod2(a, b) { // Implementation idea shamelessly stolen from Silent Matt's library http://silentmatt.com/biginteger/
  450. // Performs faster than divMod1 on larger input sizes.
  451. var a_l = a.length,
  452. b_l = b.length,
  453. result = [],
  454. part = [],
  455. base = BASE,
  456. guess, xlen, highx, highy, check;
  457. while (a_l) {
  458. part.unshift(a[--a_l]);
  459. trim(part);
  460. if (compareAbs(part, b) < 0) {
  461. result.push(0);
  462. continue;
  463. }
  464. xlen = part.length;
  465. highx = part[xlen - 1] * base + part[xlen - 2];
  466. highy = b[b_l - 1] * base + b[b_l - 2];
  467. if (xlen > b_l) {
  468. highx = (highx + 1) * base;
  469. }
  470. guess = Math.ceil(highx / highy);
  471. do {
  472. check = multiplySmall(b, guess);
  473. if (compareAbs(check, part) <= 0) break;
  474. guess--;
  475. } while (guess);
  476. result.push(guess);
  477. part = subtract(part, check);
  478. }
  479. result.reverse();
  480. return [arrayToSmall(result), arrayToSmall(part)];
  481. }
  482. function divModSmall(value, lambda) {
  483. var length = value.length,
  484. quotient = createArray(length),
  485. base = BASE,
  486. i, q, remainder, divisor;
  487. remainder = 0;
  488. for (i = length - 1; i >= 0; --i) {
  489. divisor = remainder * base + value[i];
  490. q = truncate(divisor / lambda);
  491. remainder = divisor - q * lambda;
  492. quotient[i] = q | 0;
  493. }
  494. return [quotient, remainder | 0];
  495. }
  496. function divModAny(self, v) {
  497. var value, n = parseValue(v);
  498. if (supportsNativeBigInt) {
  499. return [new NativeBigInt(self.value / n.value), new NativeBigInt(self.value % n.value)];
  500. }
  501. var a = self.value, b = n.value;
  502. var quotient;
  503. if (b === 0) throw new Error("Cannot divide by zero");
  504. if (self.isSmall) {
  505. if (n.isSmall) {
  506. return [new SmallInteger(truncate(a / b)), new SmallInteger(a % b)];
  507. }
  508. return [Integer[0], self];
  509. }
  510. if (n.isSmall) {
  511. if (b === 1) return [self, Integer[0]];
  512. if (b == -1) return [self.negate(), Integer[0]];
  513. var abs = Math.abs(b);
  514. if (abs < BASE) {
  515. value = divModSmall(a, abs);
  516. quotient = arrayToSmall(value[0]);
  517. var remainder = value[1];
  518. if (self.sign) remainder = -remainder;
  519. if (typeof quotient === "number") {
  520. if (self.sign !== n.sign) quotient = -quotient;
  521. return [new SmallInteger(quotient), new SmallInteger(remainder)];
  522. }
  523. return [new BigInteger(quotient, self.sign !== n.sign), new SmallInteger(remainder)];
  524. }
  525. b = smallToArray(abs);
  526. }
  527. var comparison = compareAbs(a, b);
  528. if (comparison === -1) return [Integer[0], self];
  529. if (comparison === 0) return [Integer[self.sign === n.sign ? 1 : -1], Integer[0]];
  530. // divMod1 is faster on smaller input sizes
  531. if (a.length + b.length <= 200)
  532. value = divMod1(a, b);
  533. else value = divMod2(a, b);
  534. quotient = value[0];
  535. var qSign = self.sign !== n.sign,
  536. mod = value[1],
  537. mSign = self.sign;
  538. if (typeof quotient === "number") {
  539. if (qSign) quotient = -quotient;
  540. quotient = new SmallInteger(quotient);
  541. } else quotient = new BigInteger(quotient, qSign);
  542. if (typeof mod === "number") {
  543. if (mSign) mod = -mod;
  544. mod = new SmallInteger(mod);
  545. } else mod = new BigInteger(mod, mSign);
  546. return [quotient, mod];
  547. }
  548. BigInteger.prototype.divmod = function (v) {
  549. var result = divModAny(this, v);
  550. return {
  551. quotient: result[0],
  552. remainder: result[1]
  553. };
  554. };
  555. NativeBigInt.prototype.divmod = SmallInteger.prototype.divmod = BigInteger.prototype.divmod;
  556. BigInteger.prototype.divide = function (v) {
  557. return divModAny(this, v)[0];
  558. };
  559. NativeBigInt.prototype.over = NativeBigInt.prototype.divide = function (v) {
  560. return new NativeBigInt(this.value / parseValue(v).value);
  561. };
  562. SmallInteger.prototype.over = SmallInteger.prototype.divide = BigInteger.prototype.over = BigInteger.prototype.divide;
  563. BigInteger.prototype.mod = function (v) {
  564. return divModAny(this, v)[1];
  565. };
  566. NativeBigInt.prototype.mod = NativeBigInt.prototype.remainder = function (v) {
  567. return new NativeBigInt(this.value % parseValue(v).value);
  568. };
  569. SmallInteger.prototype.remainder = SmallInteger.prototype.mod = BigInteger.prototype.remainder = BigInteger.prototype.mod;
  570. BigInteger.prototype.pow = function (v) {
  571. var n = parseValue(v),
  572. a = this.value,
  573. b = n.value,
  574. value, x, y;
  575. if (b === 0) return Integer[1];
  576. if (a === 0) return Integer[0];
  577. if (a === 1) return Integer[1];
  578. if (a === -1) return n.isEven() ? Integer[1] : Integer[-1];
  579. if (n.sign) {
  580. return Integer[0];
  581. }
  582. if (!n.isSmall) throw new Error("The exponent " + n.toString() + " is too large.");
  583. if (this.isSmall) {
  584. if (isPrecise(value = Math.pow(a, b)))
  585. return new SmallInteger(truncate(value));
  586. }
  587. x = this;
  588. y = Integer[1];
  589. while (true) {
  590. if (b & 1 === 1) {
  591. y = y.times(x);
  592. --b;
  593. }
  594. if (b === 0) break;
  595. b /= 2;
  596. x = x.square();
  597. }
  598. return y;
  599. };
  600. SmallInteger.prototype.pow = BigInteger.prototype.pow;
  601. NativeBigInt.prototype.pow = function (v) {
  602. var n = parseValue(v);
  603. var a = this.value, b = n.value;
  604. var _0 = BigInt(0), _1 = BigInt(1), _2 = BigInt(2);
  605. if (b === _0) return Integer[1];
  606. if (a === _0) return Integer[0];
  607. if (a === _1) return Integer[1];
  608. if (a === BigInt(-1)) return n.isEven() ? Integer[1] : Integer[-1];
  609. if (n.isNegative()) return new NativeBigInt(_0);
  610. var x = this;
  611. var y = Integer[1];
  612. while (true) {
  613. if ((b & _1) === _1) {
  614. y = y.times(x);
  615. --b;
  616. }
  617. if (b === _0) break;
  618. b /= _2;
  619. x = x.square();
  620. }
  621. return y;
  622. }
  623. BigInteger.prototype.modPow = function (exp, mod) {
  624. exp = parseValue(exp);
  625. mod = parseValue(mod);
  626. if (mod.isZero()) throw new Error("Cannot take modPow with modulus 0");
  627. var r = Integer[1],
  628. base = this.mod(mod);
  629. if (exp.isNegative()) {
  630. exp = exp.multiply(Integer[-1]);
  631. base = base.modInv(mod);
  632. }
  633. while (exp.isPositive()) {
  634. if (base.isZero()) return Integer[0];
  635. if (exp.isOdd()) r = r.multiply(base).mod(mod);
  636. exp = exp.divide(2);
  637. base = base.square().mod(mod);
  638. }
  639. return r;
  640. };
  641. NativeBigInt.prototype.modPow = SmallInteger.prototype.modPow = BigInteger.prototype.modPow;
  642. function compareAbs(a, b) {
  643. if (a.length !== b.length) {
  644. return a.length > b.length ? 1 : -1;
  645. }
  646. for (var i = a.length - 1; i >= 0; i--) {
  647. if (a[i] !== b[i]) return a[i] > b[i] ? 1 : -1;
  648. }
  649. return 0;
  650. }
  651. BigInteger.prototype.compareAbs = function (v) {
  652. var n = parseValue(v),
  653. a = this.value,
  654. b = n.value;
  655. if (n.isSmall) return 1;
  656. return compareAbs(a, b);
  657. };
  658. SmallInteger.prototype.compareAbs = function (v) {
  659. var n = parseValue(v),
  660. a = Math.abs(this.value),
  661. b = n.value;
  662. if (n.isSmall) {
  663. b = Math.abs(b);
  664. return a === b ? 0 : a > b ? 1 : -1;
  665. }
  666. return -1;
  667. };
  668. NativeBigInt.prototype.compareAbs = function (v) {
  669. var a = this.value;
  670. var b = parseValue(v).value;
  671. a = a >= 0 ? a : -a;
  672. b = b >= 0 ? b : -b;
  673. return a === b ? 0 : a > b ? 1 : -1;
  674. }
  675. BigInteger.prototype.compare = function (v) {
  676. // See discussion about comparison with Infinity:
  677. // https://github.com/peterolson/BigInteger.js/issues/61
  678. if (v === Infinity) {
  679. return -1;
  680. }
  681. if (v === -Infinity) {
  682. return 1;
  683. }
  684. var n = parseValue(v),
  685. a = this.value,
  686. b = n.value;
  687. if (this.sign !== n.sign) {
  688. return n.sign ? 1 : -1;
  689. }
  690. if (n.isSmall) {
  691. return this.sign ? -1 : 1;
  692. }
  693. return compareAbs(a, b) * (this.sign ? -1 : 1);
  694. };
  695. BigInteger.prototype.compareTo = BigInteger.prototype.compare;
  696. SmallInteger.prototype.compare = function (v) {
  697. if (v === Infinity) {
  698. return -1;
  699. }
  700. if (v === -Infinity) {
  701. return 1;
  702. }
  703. var n = parseValue(v),
  704. a = this.value,
  705. b = n.value;
  706. if (n.isSmall) {
  707. return a == b ? 0 : a > b ? 1 : -1;
  708. }
  709. if (a < 0 !== n.sign) {
  710. return a < 0 ? -1 : 1;
  711. }
  712. return a < 0 ? 1 : -1;
  713. };
  714. SmallInteger.prototype.compareTo = SmallInteger.prototype.compare;
  715. NativeBigInt.prototype.compare = function (v) {
  716. if (v === Infinity) {
  717. return -1;
  718. }
  719. if (v === -Infinity) {
  720. return 1;
  721. }
  722. var a = this.value;
  723. var b = parseValue(v).value;
  724. return a === b ? 0 : a > b ? 1 : -1;
  725. }
  726. NativeBigInt.prototype.compareTo = NativeBigInt.prototype.compare;
  727. BigInteger.prototype.equals = function (v) {
  728. return this.compare(v) === 0;
  729. };
  730. NativeBigInt.prototype.eq = NativeBigInt.prototype.equals = SmallInteger.prototype.eq = SmallInteger.prototype.equals = BigInteger.prototype.eq = BigInteger.prototype.equals;
  731. BigInteger.prototype.notEquals = function (v) {
  732. return this.compare(v) !== 0;
  733. };
  734. NativeBigInt.prototype.neq = NativeBigInt.prototype.notEquals = SmallInteger.prototype.neq = SmallInteger.prototype.notEquals = BigInteger.prototype.neq = BigInteger.prototype.notEquals;
  735. BigInteger.prototype.greater = function (v) {
  736. return this.compare(v) > 0;
  737. };
  738. NativeBigInt.prototype.gt = NativeBigInt.prototype.greater = SmallInteger.prototype.gt = SmallInteger.prototype.greater = BigInteger.prototype.gt = BigInteger.prototype.greater;
  739. BigInteger.prototype.lesser = function (v) {
  740. return this.compare(v) < 0;
  741. };
  742. NativeBigInt.prototype.lt = NativeBigInt.prototype.lesser = SmallInteger.prototype.lt = SmallInteger.prototype.lesser = BigInteger.prototype.lt = BigInteger.prototype.lesser;
  743. BigInteger.prototype.greaterOrEquals = function (v) {
  744. return this.compare(v) >= 0;
  745. };
  746. NativeBigInt.prototype.geq = NativeBigInt.prototype.greaterOrEquals = SmallInteger.prototype.geq = SmallInteger.prototype.greaterOrEquals = BigInteger.prototype.geq = BigInteger.prototype.greaterOrEquals;
  747. BigInteger.prototype.lesserOrEquals = function (v) {
  748. return this.compare(v) <= 0;
  749. };
  750. NativeBigInt.prototype.leq = NativeBigInt.prototype.lesserOrEquals = SmallInteger.prototype.leq = SmallInteger.prototype.lesserOrEquals = BigInteger.prototype.leq = BigInteger.prototype.lesserOrEquals;
  751. BigInteger.prototype.isEven = function () {
  752. return (this.value[0] & 1) === 0;
  753. };
  754. SmallInteger.prototype.isEven = function () {
  755. return (this.value & 1) === 0;
  756. };
  757. NativeBigInt.prototype.isEven = function () {
  758. return (this.value & BigInt(1)) === BigInt(0);
  759. }
  760. BigInteger.prototype.isOdd = function () {
  761. return (this.value[0] & 1) === 1;
  762. };
  763. SmallInteger.prototype.isOdd = function () {
  764. return (this.value & 1) === 1;
  765. };
  766. NativeBigInt.prototype.isOdd = function () {
  767. return (this.value & BigInt(1)) === BigInt(1);
  768. }
  769. BigInteger.prototype.isPositive = function () {
  770. return !this.sign;
  771. };
  772. SmallInteger.prototype.isPositive = function () {
  773. return this.value > 0;
  774. };
  775. NativeBigInt.prototype.isPositive = SmallInteger.prototype.isPositive;
  776. BigInteger.prototype.isNegative = function () {
  777. return this.sign;
  778. };
  779. SmallInteger.prototype.isNegative = function () {
  780. return this.value < 0;
  781. };
  782. NativeBigInt.prototype.isNegative = SmallInteger.prototype.isNegative;
  783. BigInteger.prototype.isUnit = function () {
  784. return false;
  785. };
  786. SmallInteger.prototype.isUnit = function () {
  787. return Math.abs(this.value) === 1;
  788. };
  789. NativeBigInt.prototype.isUnit = function () {
  790. return this.abs().value === BigInt(1);
  791. }
  792. BigInteger.prototype.isZero = function () {
  793. return false;
  794. };
  795. SmallInteger.prototype.isZero = function () {
  796. return this.value === 0;
  797. };
  798. NativeBigInt.prototype.isZero = function () {
  799. return this.value === BigInt(0);
  800. }
  801. BigInteger.prototype.isDivisibleBy = function (v) {
  802. var n = parseValue(v);
  803. if (n.isZero()) return false;
  804. if (n.isUnit()) return true;
  805. if (n.compareAbs(2) === 0) return this.isEven();
  806. return this.mod(n).isZero();
  807. };
  808. NativeBigInt.prototype.isDivisibleBy = SmallInteger.prototype.isDivisibleBy = BigInteger.prototype.isDivisibleBy;
  809. function isBasicPrime(v) {
  810. var n = v.abs();
  811. if (n.isUnit()) return false;
  812. if (n.equals(2) || n.equals(3) || n.equals(5)) return true;
  813. if (n.isEven() || n.isDivisibleBy(3) || n.isDivisibleBy(5)) return false;
  814. if (n.lesser(49)) return true;
  815. // we don't know if it's prime: let the other functions figure it out
  816. }
  817. function millerRabinTest(n, a) {
  818. var nPrev = n.prev(),
  819. b = nPrev,
  820. r = 0,
  821. d, t, i, x;
  822. while (b.isEven()) b = b.divide(2), r++;
  823. next: for (i = 0; i < a.length; i++) {
  824. if (n.lesser(a[i])) continue;
  825. x = bigInt(a[i]).modPow(b, n);
  826. if (x.isUnit() || x.equals(nPrev)) continue;
  827. for (d = r - 1; d != 0; d--) {
  828. x = x.square().mod(n);
  829. if (x.isUnit()) return false;
  830. if (x.equals(nPrev)) continue next;
  831. }
  832. return false;
  833. }
  834. return true;
  835. }
  836. // Set "strict" to true to force GRH-supported lower bound of 2*log(N)^2
  837. BigInteger.prototype.isPrime = function (strict) {
  838. var isPrime = isBasicPrime(this);
  839. if (isPrime !== undefined) return isPrime;
  840. var n = this.abs();
  841. var bits = n.bitLength();
  842. if (bits <= 64)
  843. return millerRabinTest(n, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]);
  844. var logN = Math.log(2) * bits.toJSNumber();
  845. var t = Math.ceil((strict === true) ? (2 * Math.pow(logN, 2)) : logN);
  846. for (var a = [], i = 0; i < t; i++) {
  847. a.push(bigInt(i + 2));
  848. }
  849. return millerRabinTest(n, a);
  850. };
  851. NativeBigInt.prototype.isPrime = SmallInteger.prototype.isPrime = BigInteger.prototype.isPrime;
  852. BigInteger.prototype.isProbablePrime = function (iterations, rng) {
  853. var isPrime = isBasicPrime(this);
  854. if (isPrime !== undefined) return isPrime;
  855. var n = this.abs();
  856. var t = iterations === undefined ? 5 : iterations;
  857. for (var a = [], i = 0; i < t; i++) {
  858. a.push(bigInt.randBetween(2, n.minus(2), rng));
  859. }
  860. return millerRabinTest(n, a);
  861. };
  862. NativeBigInt.prototype.isProbablePrime = SmallInteger.prototype.isProbablePrime = BigInteger.prototype.isProbablePrime;
  863. BigInteger.prototype.modInv = function (n) {
  864. var t = bigInt.zero, newT = bigInt.one, r = parseValue(n), newR = this.abs(), q, lastT, lastR;
  865. while (!newR.isZero()) {
  866. q = r.divide(newR);
  867. lastT = t;
  868. lastR = r;
  869. t = newT;
  870. r = newR;
  871. newT = lastT.subtract(q.multiply(newT));
  872. newR = lastR.subtract(q.multiply(newR));
  873. }
  874. if (!r.isUnit()) throw new Error(this.toString() + " and " + n.toString() + " are not co-prime");
  875. if (t.compare(0) === -1) {
  876. t = t.add(n);
  877. }
  878. if (this.isNegative()) {
  879. return t.negate();
  880. }
  881. return t;
  882. };
  883. NativeBigInt.prototype.modInv = SmallInteger.prototype.modInv = BigInteger.prototype.modInv;
  884. BigInteger.prototype.next = function () {
  885. var value = this.value;
  886. if (this.sign) {
  887. return subtractSmall(value, 1, this.sign);
  888. }
  889. return new BigInteger(addSmall(value, 1), this.sign);
  890. };
  891. SmallInteger.prototype.next = function () {
  892. var value = this.value;
  893. if (value + 1 < MAX_INT) return new SmallInteger(value + 1);
  894. return new BigInteger(MAX_INT_ARR, false);
  895. };
  896. NativeBigInt.prototype.next = function () {
  897. return new NativeBigInt(this.value + BigInt(1));
  898. }
  899. BigInteger.prototype.prev = function () {
  900. var value = this.value;
  901. if (this.sign) {
  902. return new BigInteger(addSmall(value, 1), true);
  903. }
  904. return subtractSmall(value, 1, this.sign);
  905. };
  906. SmallInteger.prototype.prev = function () {
  907. var value = this.value;
  908. if (value - 1 > -MAX_INT) return new SmallInteger(value - 1);
  909. return new BigInteger(MAX_INT_ARR, true);
  910. };
  911. NativeBigInt.prototype.prev = function () {
  912. return new NativeBigInt(this.value - BigInt(1));
  913. }
  914. var powersOfTwo = [1];
  915. while (2 * powersOfTwo[powersOfTwo.length - 1] <= BASE) powersOfTwo.push(2 * powersOfTwo[powersOfTwo.length - 1]);
  916. var powers2Length = powersOfTwo.length, highestPower2 = powersOfTwo[powers2Length - 1];
  917. function shift_isSmall(n) {
  918. return Math.abs(n) <= BASE;
  919. }
  920. BigInteger.prototype.shiftLeft = function (v) {
  921. var n = parseValue(v).toJSNumber();
  922. if (!shift_isSmall(n)) {
  923. throw new Error(String(n) + " is too large for shifting.");
  924. }
  925. if (n < 0) return this.shiftRight(-n);
  926. var result = this;
  927. if (result.isZero()) return result;
  928. while (n >= powers2Length) {
  929. result = result.multiply(highestPower2);
  930. n -= powers2Length - 1;
  931. }
  932. return result.multiply(powersOfTwo[n]);
  933. };
  934. NativeBigInt.prototype.shiftLeft = SmallInteger.prototype.shiftLeft = BigInteger.prototype.shiftLeft;
  935. BigInteger.prototype.shiftRight = function (v) {
  936. var remQuo;
  937. var n = parseValue(v).toJSNumber();
  938. if (!shift_isSmall(n)) {
  939. throw new Error(String(n) + " is too large for shifting.");
  940. }
  941. if (n < 0) return this.shiftLeft(-n);
  942. var result = this;
  943. while (n >= powers2Length) {
  944. if (result.isZero() || (result.isNegative() && result.isUnit())) return result;
  945. remQuo = divModAny(result, highestPower2);
  946. result = remQuo[1].isNegative() ? remQuo[0].prev() : remQuo[0];
  947. n -= powers2Length - 1;
  948. }
  949. remQuo = divModAny(result, powersOfTwo[n]);
  950. return remQuo[1].isNegative() ? remQuo[0].prev() : remQuo[0];
  951. };
  952. NativeBigInt.prototype.shiftRight = SmallInteger.prototype.shiftRight = BigInteger.prototype.shiftRight;
  953. function bitwise(x, y, fn) {
  954. y = parseValue(y);
  955. var xSign = x.isNegative(), ySign = y.isNegative();
  956. var xRem = xSign ? x.not() : x,
  957. yRem = ySign ? y.not() : y;
  958. var xDigit = 0, yDigit = 0;
  959. var xDivMod = null, yDivMod = null;
  960. var result = [];
  961. while (!xRem.isZero() || !yRem.isZero()) {
  962. xDivMod = divModAny(xRem, highestPower2);
  963. xDigit = xDivMod[1].toJSNumber();
  964. if (xSign) {
  965. xDigit = highestPower2 - 1 - xDigit; // two's complement for negative numbers
  966. }
  967. yDivMod = divModAny(yRem, highestPower2);
  968. yDigit = yDivMod[1].toJSNumber();
  969. if (ySign) {
  970. yDigit = highestPower2 - 1 - yDigit; // two's complement for negative numbers
  971. }
  972. xRem = xDivMod[0];
  973. yRem = yDivMod[0];
  974. result.push(fn(xDigit, yDigit));
  975. }
  976. var sum = fn(xSign ? 1 : 0, ySign ? 1 : 0) !== 0 ? bigInt(-1) : bigInt(0);
  977. for (var i = result.length - 1; i >= 0; i -= 1) {
  978. sum = sum.multiply(highestPower2).add(bigInt(result[i]));
  979. }
  980. return sum;
  981. }
  982. BigInteger.prototype.not = function () {
  983. return this.negate().prev();
  984. };
  985. NativeBigInt.prototype.not = SmallInteger.prototype.not = BigInteger.prototype.not;
  986. BigInteger.prototype.and = function (n) {
  987. return bitwise(this, n, function (a, b) { return a & b; });
  988. };
  989. NativeBigInt.prototype.and = SmallInteger.prototype.and = BigInteger.prototype.and;
  990. BigInteger.prototype.or = function (n) {
  991. return bitwise(this, n, function (a, b) { return a | b; });
  992. };
  993. NativeBigInt.prototype.or = SmallInteger.prototype.or = BigInteger.prototype.or;
  994. BigInteger.prototype.xor = function (n) {
  995. return bitwise(this, n, function (a, b) { return a ^ b; });
  996. };
  997. NativeBigInt.prototype.xor = SmallInteger.prototype.xor = BigInteger.prototype.xor;
  998. var LOBMASK_I = 1 << 30, LOBMASK_BI = (BASE & -BASE) * (BASE & -BASE) | LOBMASK_I;
  999. function roughLOB(n) { // get lowestOneBit (rough)
  1000. // SmallInteger: return Min(lowestOneBit(n), 1 << 30)
  1001. // BigInteger: return Min(lowestOneBit(n), 1 << 14) [BASE=1e7]
  1002. var v = n.value,
  1003. x = typeof v === "number" ? v | LOBMASK_I :
  1004. typeof v === "bigint" ? v | BigInt(LOBMASK_I) :
  1005. v[0] + v[1] * BASE | LOBMASK_BI;
  1006. return x & -x;
  1007. }
  1008. function integerLogarithm(value, base) {
  1009. if (base.compareTo(value) <= 0) {
  1010. var tmp = integerLogarithm(value, base.square(base));
  1011. var p = tmp.p;
  1012. var e = tmp.e;
  1013. var t = p.multiply(base);
  1014. return t.compareTo(value) <= 0 ? { p: t, e: e * 2 + 1 } : { p: p, e: e * 2 };
  1015. }
  1016. return { p: bigInt(1), e: 0 };
  1017. }
  1018. BigInteger.prototype.bitLength = function () {
  1019. var n = this;
  1020. if (n.compareTo(bigInt(0)) < 0) {
  1021. n = n.negate().subtract(bigInt(1));
  1022. }
  1023. if (n.compareTo(bigInt(0)) === 0) {
  1024. return bigInt(0);
  1025. }
  1026. return bigInt(integerLogarithm(n, bigInt(2)).e).add(bigInt(1));
  1027. }
  1028. NativeBigInt.prototype.bitLength = SmallInteger.prototype.bitLength = BigInteger.prototype.bitLength;
  1029. function max(a, b) {
  1030. a = parseValue(a);
  1031. b = parseValue(b);
  1032. return a.greater(b) ? a : b;
  1033. }
  1034. function min(a, b) {
  1035. a = parseValue(a);
  1036. b = parseValue(b);
  1037. return a.lesser(b) ? a : b;
  1038. }
  1039. function gcd(a, b) {
  1040. a = parseValue(a).abs();
  1041. b = parseValue(b).abs();
  1042. if (a.equals(b)) return a;
  1043. if (a.isZero()) return b;
  1044. if (b.isZero()) return a;
  1045. var c = Integer[1], d, t;
  1046. while (a.isEven() && b.isEven()) {
  1047. d = min(roughLOB(a), roughLOB(b));
  1048. a = a.divide(d);
  1049. b = b.divide(d);
  1050. c = c.multiply(d);
  1051. }
  1052. while (a.isEven()) {
  1053. a = a.divide(roughLOB(a));
  1054. }
  1055. do {
  1056. while (b.isEven()) {
  1057. b = b.divide(roughLOB(b));
  1058. }
  1059. if (a.greater(b)) {
  1060. t = b; b = a; a = t;
  1061. }
  1062. b = b.subtract(a);
  1063. } while (!b.isZero());
  1064. return c.isUnit() ? a : a.multiply(c);
  1065. }
  1066. function lcm(a, b) {
  1067. a = parseValue(a).abs();
  1068. b = parseValue(b).abs();
  1069. return a.divide(gcd(a, b)).multiply(b);
  1070. }
  1071. function randBetween(a, b, rng) {
  1072. a = parseValue(a);
  1073. b = parseValue(b);
  1074. var usedRNG = rng || Math.random;
  1075. var low = min(a, b), high = max(a, b);
  1076. var range = high.subtract(low).add(1);
  1077. if (range.isSmall) return low.add(Math.floor(usedRNG() * range));
  1078. var digits = toBase(range, BASE).value;
  1079. var result = [], restricted = true;
  1080. for (var i = 0; i < digits.length; i++) {
  1081. var top = restricted ? digits[i] + (i + 1 < digits.length ? digits[i + 1] / BASE : 0) : BASE;
  1082. var digit = truncate(usedRNG() * top);
  1083. result.push(digit);
  1084. if (digit < digits[i]) restricted = false;
  1085. }
  1086. return low.add(Integer.fromArray(result, BASE, false));
  1087. }
  1088. var parseBase = function (text, base, alphabet, caseSensitive) {
  1089. alphabet = alphabet || DEFAULT_ALPHABET;
  1090. text = String(text);
  1091. if (!caseSensitive) {
  1092. text = text.toLowerCase();
  1093. alphabet = alphabet.toLowerCase();
  1094. }
  1095. var length = text.length;
  1096. var i;
  1097. var absBase = Math.abs(base);
  1098. var alphabetValues = {};
  1099. for (i = 0; i < alphabet.length; i++) {
  1100. alphabetValues[alphabet[i]] = i;
  1101. }
  1102. for (i = 0; i < length; i++) {
  1103. var c = text[i];
  1104. if (c === "-") continue;
  1105. if (c in alphabetValues) {
  1106. if (alphabetValues[c] >= absBase) {
  1107. if (c === "1" && absBase === 1) continue;
  1108. throw new Error(c + " is not a valid digit in base " + base + ".");
  1109. }
  1110. }
  1111. }
  1112. base = parseValue(base);
  1113. var digits = [];
  1114. var isNegative = text[0] === "-";
  1115. for (i = isNegative ? 1 : 0; i < text.length; i++) {
  1116. var c = text[i];
  1117. if (c in alphabetValues) digits.push(parseValue(alphabetValues[c]));
  1118. else if (c === "<") {
  1119. var start = i;
  1120. do { i++; } while (text[i] !== ">" && i < text.length);
  1121. digits.push(parseValue(text.slice(start + 1, i)));
  1122. }
  1123. else throw new Error(c + " is not a valid character");
  1124. }
  1125. return parseBaseFromArray(digits, base, isNegative);
  1126. };
  1127. function parseBaseFromArray(digits, base, isNegative) {
  1128. var val = Integer[0], pow = Integer[1], i;
  1129. for (i = digits.length - 1; i >= 0; i--) {
  1130. val = val.add(digits[i].times(pow));
  1131. pow = pow.times(base);
  1132. }
  1133. return isNegative ? val.negate() : val;
  1134. }
  1135. function stringify(digit, alphabet) {
  1136. alphabet = alphabet || DEFAULT_ALPHABET;
  1137. if (digit < alphabet.length) {
  1138. return alphabet[digit];
  1139. }
  1140. return "<" + digit + ">";
  1141. }
  1142. function toBase(n, base) {
  1143. base = bigInt(base);
  1144. if (base.isZero()) {
  1145. if (n.isZero()) return { value: [0], isNegative: false };
  1146. throw new Error("Cannot convert nonzero numbers to base 0.");
  1147. }
  1148. if (base.equals(-1)) {
  1149. if (n.isZero()) return { value: [0], isNegative: false };
  1150. if (n.isNegative())
  1151. return {
  1152. value: [].concat.apply([], Array.apply(null, Array(-n.toJSNumber()))
  1153. .map(Array.prototype.valueOf, [1, 0])
  1154. ),
  1155. isNegative: false
  1156. };
  1157. var arr = Array.apply(null, Array(n.toJSNumber() - 1))
  1158. .map(Array.prototype.valueOf, [0, 1]);
  1159. arr.unshift([1]);
  1160. return {
  1161. value: [].concat.apply([], arr),
  1162. isNegative: false
  1163. };
  1164. }
  1165. var neg = false;
  1166. if (n.isNegative() && base.isPositive()) {
  1167. neg = true;
  1168. n = n.abs();
  1169. }
  1170. if (base.isUnit()) {
  1171. if (n.isZero()) return { value: [0], isNegative: false };
  1172. return {
  1173. value: Array.apply(null, Array(n.toJSNumber()))
  1174. .map(Number.prototype.valueOf, 1),
  1175. isNegative: neg
  1176. };
  1177. }
  1178. var out = [];
  1179. var left = n, divmod;
  1180. while (left.isNegative() || left.compareAbs(base) >= 0) {
  1181. divmod = left.divmod(base);
  1182. left = divmod.quotient;
  1183. var digit = divmod.remainder;
  1184. if (digit.isNegative()) {
  1185. digit = base.minus(digit).abs();
  1186. left = left.next();
  1187. }
  1188. out.push(digit.toJSNumber());
  1189. }
  1190. out.push(left.toJSNumber());
  1191. return { value: out.reverse(), isNegative: neg };
  1192. }
  1193. function toBaseString(n, base, alphabet) {
  1194. var arr = toBase(n, base);
  1195. return (arr.isNegative ? "-" : "") + arr.value.map(function (x) {
  1196. return stringify(x, alphabet);
  1197. }).join('');
  1198. }
  1199. BigInteger.prototype.toArray = function (radix) {
  1200. return toBase(this, radix);
  1201. };
  1202. SmallInteger.prototype.toArray = function (radix) {
  1203. return toBase(this, radix);
  1204. };
  1205. NativeBigInt.prototype.toArray = function (radix) {
  1206. return toBase(this, radix);
  1207. };
  1208. BigInteger.prototype.toString = function (radix, alphabet) {
  1209. if (radix === undefined) radix = 10;
  1210. if (radix !== 10) return toBaseString(this, radix, alphabet);
  1211. var v = this.value, l = v.length, str = String(v[--l]), zeros = "0000000", digit;
  1212. while (--l >= 0) {
  1213. digit = String(v[l]);
  1214. str += zeros.slice(digit.length) + digit;
  1215. }
  1216. var sign = this.sign ? "-" : "";
  1217. return sign + str;
  1218. };
  1219. SmallInteger.prototype.toString = function (radix, alphabet) {
  1220. if (radix === undefined) radix = 10;
  1221. if (radix != 10) return toBaseString(this, radix, alphabet);
  1222. return String(this.value);
  1223. };
  1224. NativeBigInt.prototype.toString = SmallInteger.prototype.toString;
  1225. NativeBigInt.prototype.toJSON = BigInteger.prototype.toJSON = SmallInteger.prototype.toJSON = function () { return this.toString(); }
  1226. BigInteger.prototype.valueOf = function () {
  1227. return parseInt(this.toString(), 10);
  1228. };
  1229. BigInteger.prototype.toJSNumber = BigInteger.prototype.valueOf;
  1230. SmallInteger.prototype.valueOf = function () {
  1231. return this.value;
  1232. };
  1233. SmallInteger.prototype.toJSNumber = SmallInteger.prototype.valueOf;
  1234. NativeBigInt.prototype.valueOf = NativeBigInt.prototype.toJSNumber = function () {
  1235. return parseInt(this.toString(), 10);
  1236. }
  1237. function parseStringValue(v) {
  1238. if (isPrecise(+v)) {
  1239. var x = +v;
  1240. if (x === truncate(x))
  1241. return supportsNativeBigInt ? new NativeBigInt(BigInt(x)) : new SmallInteger(x);
  1242. throw new Error("Invalid integer: " + v);
  1243. }
  1244. var sign = v[0] === "-";
  1245. if (sign) v = v.slice(1);
  1246. var split = v.split(/e/i);
  1247. if (split.length > 2) throw new Error("Invalid integer: " + split.join("e"));
  1248. if (split.length === 2) {
  1249. var exp = split[1];
  1250. if (exp[0] === "+") exp = exp.slice(1);
  1251. exp = +exp;
  1252. if (exp !== truncate(exp) || !isPrecise(exp)) throw new Error("Invalid integer: " + exp + " is not a valid exponent.");
  1253. var text = split[0];
  1254. var decimalPlace = text.indexOf(".");
  1255. if (decimalPlace >= 0) {
  1256. exp -= text.length - decimalPlace - 1;
  1257. text = text.slice(0, decimalPlace) + text.slice(decimalPlace + 1);
  1258. }
  1259. if (exp < 0) throw new Error("Cannot include negative exponent part for integers");
  1260. text += (new Array(exp + 1)).join("0");
  1261. v = text;
  1262. }
  1263. var isValid = /^([0-9][0-9]*)$/.test(v);
  1264. if (!isValid) throw new Error("Invalid integer: " + v);
  1265. if (supportsNativeBigInt) {
  1266. return new NativeBigInt(BigInt(sign ? "-" + v : v));
  1267. }
  1268. var r = [], max = v.length, l = LOG_BASE, min = max - l;
  1269. while (max > 0) {
  1270. r.push(+v.slice(min, max));
  1271. min -= l;
  1272. if (min < 0) min = 0;
  1273. max -= l;
  1274. }
  1275. trim(r);
  1276. return new BigInteger(r, sign);
  1277. }
  1278. function parseNumberValue(v) {
  1279. if (supportsNativeBigInt) {
  1280. return new NativeBigInt(BigInt(v));
  1281. }
  1282. if (isPrecise(v)) {
  1283. if (v !== truncate(v)) throw new Error(v + " is not an integer.");
  1284. return new SmallInteger(v);
  1285. }
  1286. return parseStringValue(v.toString());
  1287. }
  1288. function parseValue(v) {
  1289. if (typeof v === "number") {
  1290. return parseNumberValue(v);
  1291. }
  1292. if (typeof v === "string") {
  1293. return parseStringValue(v);
  1294. }
  1295. if (typeof v === "bigint") {
  1296. return new NativeBigInt(v);
  1297. }
  1298. return v;
  1299. }
  1300. // Pre-define numbers in range [-999,999]
  1301. for (var i = 0; i < 1000; i++) {
  1302. Integer[i] = parseValue(i);
  1303. if (i > 0) Integer[-i] = parseValue(-i);
  1304. }
  1305. // Backwards compatibility
  1306. Integer.one = Integer[1];
  1307. Integer.zero = Integer[0];
  1308. Integer.minusOne = Integer[-1];
  1309. Integer.max = max;
  1310. Integer.min = min;
  1311. Integer.gcd = gcd;
  1312. Integer.lcm = lcm;
  1313. Integer.isInstance = function (x) { return x instanceof BigInteger || x instanceof SmallInteger || x instanceof NativeBigInt; };
  1314. Integer.randBetween = randBetween;
  1315. Integer.fromArray = function (digits, base, isNegative) {
  1316. return parseBaseFromArray(digits.map(parseValue), parseValue(base || 10), isNegative);
  1317. };
  1318. return Integer;
  1319. })();
  1320. // Node.js check
  1321. if (typeof module !== "undefined" && module.hasOwnProperty("exports")) {
  1322. module.exports = bigInt;
  1323. }
  1324. //amd check
  1325. if (typeof define === "function" && define.amd) {
  1326. define( function () {
  1327. return bigInt;
  1328. });
  1329. }