Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. class BigNumber {
  2. constructor(num) {
  3. this.stepN = 8;
  4. this.step = ~~ Math.pow(10, this.stepN);
  5. if (num instanceof BigNumber) {
  6. this.digits = num.digits.slice();
  7. this.trailing0 = num.trailing0;
  8. } else if (typeof num === 'object') {
  9. this.digits = num.digits || [];
  10. this.trailing0 = num.trailing0 || 0;
  11. } else {
  12. this.digits = [];
  13. this.trailing0 = 0;
  14. while (num) {
  15. this.digits.push(num % this.step);
  16. num = ~~ (num / this.step);
  17. }
  18. }
  19. }
  20.  
  21. multiply(other) {
  22. if (!(other instanceof BigNumber)) {
  23. return this.multiplySimple(other);
  24. }
  25. let x = 0;
  26. const digits = [];
  27. const selfLength = this.digits.length;
  28. const otherLength = other.digits.length;
  29. const totalLength = selfLength + otherLength - 1;
  30. for (let i = 0; i < totalLength; i ++) {
  31. for (let j = Math.max(0, i - otherLength + 1); j < selfLength && j <= i; j ++) {
  32. x += this.digits[j] * other.digits[i - j];
  33. }
  34. digits.push(x % this.step);
  35. x = ~~ (x / this.step);
  36. }
  37. while (x) {
  38. digits.push(x % this.step);
  39. x = ~~ (x / this.step);
  40. }
  41. let trailing0 = this.trailing0 + other.trailing0;
  42. while (digits.length && !digits[0]) {
  43. digits.shift();
  44. trailing0 ++;
  45. }
  46. return new BigNumber({digits, trailing0});
  47. }
  48.  
  49. multiplySimple(num) {
  50. let x = 0;
  51. const digits = [];
  52. const length = this.digits.length;
  53. for (let i = 0; i < length; i ++) {
  54. x += this.digits[i] * num;
  55. digits.push(x % this.step);
  56. x = ~~ (x / this.step);
  57. }
  58. while (x) {
  59. digits.push(x % this.step);
  60. x = ~~ (x / this.step);
  61. }
  62. let trailing0 = this.trailing0;
  63. while (digits.length && !digits[0]) {
  64. digits.shift();
  65. trailing0 ++;
  66. }
  67. return new BigNumber({digits, trailing0});
  68. }
  69.  
  70. pow(n) {
  71. const midValues = {1: new BigNumber(this)};
  72. const keys = [1];
  73. for (let i = 2; i <= n; i *= 2) {
  74. keys.push(i);
  75. const midValue = new BigNumber(midValues[i >> 1]);
  76. midValues[i] = midValue.multiply(midValue);
  77. }
  78. let res = new BigNumber(1);
  79. while (n) {
  80. const key = +keys.pop();
  81. if (key <= n) {
  82. n -= key;
  83. res = res.multiply(midValues[key]);
  84. }
  85. }
  86. return res;
  87. }
  88.  
  89. leftpad(s, n, c='0') {
  90. s = s.toString();
  91. return s.length < n ? c.repeat(n - s.length) + s : s;
  92. }
  93.  
  94. toString() {
  95. const res = [];
  96. const length = this.digits.length;
  97. if (length) {
  98. res.push(this.digits[length - 1]);
  99. for (let i = length - 1; i --; ) {
  100. res.push(this.leftpad(this.digits[i], this.stepN));
  101. }
  102. res.push('0'.repeat(this.trailing0 * this.stepN));
  103. } else {
  104. res.push(0);
  105. }
  106. return res.join('');
  107. }
  108. }
  109.  
  110. function test1() {
  111. let n = new BigNumber(65);
  112. console.time('calculate');
  113. for (let i = 0; i < 65537; i ++) n = n.multiply(65);
  114. console.timeEnd('calculate');
  115. console.time('repr');
  116. const s = n.toString();
  117. console.timeEnd('repr');
  118. // console.log(s);
  119. }
  120.  
  121. function test2() {
  122. let n = new BigNumber(1);
  123. console.time('calculate');
  124. for (let i = 2; i <= 2000; i ++) n = n.multiply(i);
  125. console.timeEnd('calculate');
  126. console.time('repr');
  127. const s = n.toString();
  128. console.timeEnd('repr');
  129. // console.log(s);
  130. }
  131.  
  132. function test3() {
  133. const n = new BigNumber(65);
  134. console.time('calculate');
  135. const p = n.pow(65537);
  136. console.timeEnd('calculate');
  137. console.time('repr');
  138. const s = p.toString();
  139. console.timeEnd('repr');
  140. // console.log(s);
  141. }
  142.  
  143. test1();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement