-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbig-integer.js
More file actions
207 lines (166 loc) · 5.29 KB
/
big-integer.js
File metadata and controls
207 lines (166 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
function normalize (number, digits) {
var number = number.toString(),
lessDigits = digits - number.length;
while(lessDigits--) {
number = "0" + number;
}
return number;
}
function objToArray (obj) {
var result = [];
for(var i in obj){
result.push(obj[i]);
}
return result;
}
function trim (number) {
var number = number.toString();
return number.replace(/^0+/, '');
}
module.exports = {
add: function () {
var numbers = objToArray(arguments),
numNumbers = numbers.length,
result = "";
while (numNumbers--) {
result = this._addTwoNumbers(result, numbers[numNumbers]);
}
return result;
},
_addTwoNumbers: function (operand1, operand2) {
var operand1 = operand1.toString(),
operand2 = operand2.toString(),
maxDigits = Math.max(operand1.length, operand2.length),
operand1 = normalize(operand1, maxDigits),
operand2 = normalize(operand2, maxDigits),
operand1Digits = operand1.split(""),
operand2Digits = operand2.split(""),
curDigit = maxDigits,
remainder = 0,
result = "";
while(curDigit--) {
var a = operand1Digits[curDigit],
b = operand2Digits[curDigit],
intA = parseInt(a, 10),
intB = parseInt(b, 10),
sum = intA + intB + remainder;
remainder = (sum > 9) ? 1 : 0;
result = (sum % 10).toString() + result;
if(curDigit === 0) result = remainder.toString() + result;
}
return trim(result);
},
max: function () {
var numbers = objToArray (arguments),
numNumbers = numbers.length,
largest = "";
for(var i = 0; i < numNumbers; i++) {
var number = numbers[i],
largest = this._maxTwoNumbers(largest, number);
}
return largest;
},
_maxTwoNumbers: function (operand1, operand2) {
var operand1 = operand1.toString(),
operand2 = operand2.toString(),
maxDigits = Math.max(operand1.length, operand2.length),
operand1 = normalize(operand1, maxDigits),
operand2 = normalize(operand2, maxDigits)
operand1Digits = operand1.split(""),
operand2Digits = operand2.split("");
for(var i = 0; i < maxDigits; i++) {
var operand1Digit = parseInt(operand1Digits[i], 10),
operand2Digit = parseInt(operand2Digits[i], 10);
if (operand1Digit > operand2Digit) {
return operand1;
} else if (operand2Digit > operand1Digit) {
return operand2;
} else if ((i === (maxDigits - 1)) && (operand1Digit === operand2Digit)){
return operand2;
}
}
},
min: function () {
var numbers = objToArray (arguments),
numNumbers = numbers.length,
smallest = numbers[0];
for(var i = 0; i < numNumbers; i++) {
var number = numbers[i],
smallest = this._minTwoNumbers(smallest, number);
}
return smallest;
},
_minTwoNumbers: function (operand1, operand2) {
var operand1 = operand1.toString(),
operand2 = operand2.toString(),
maxDigits = Math.max(operand1.length, operand2.length),
operand1 = normalize(operand1, maxDigits),
operand2 = normalize(operand2, maxDigits)
operand1Digits = operand1.split(""),
operand2Digits = operand2.split("");
for(var i = 0; i < maxDigits; i++) {
var operand1Digit = parseInt(operand1Digits[i], 10),
operand2Digit = parseInt(operand2Digits[i], 10);
if (operand1Digit < operand2Digit) {
return operand1;
} else if (operand2Digit < operand1Digit) {
return operand2;
} else if ((i === maxDigits - 1) && (operand1Digit === operand2Digit)) {
return operand1;
}
}
},
pow: function (base, power) {
var base = base.toString(),
power = parseInt(power, 10),
result = "1";
if (power === 0) {
return result;
} else if (power === 1) {
return base;
} else if (base === "10") {
for (var i = 0; i < power; i++) {
result+= "0";
}
} else {
for (var i = 0; i < power; i++) {
result = this.multiply(result, base);
}
}
return result;
},
multiply: function () {
var numbers = objToArray (arguments),
numNumbers = numbers.length,
result = numbers[0];
for (var i = 1; i < numNumbers; i++) {
var number = numbers[i],
result = this._multiplyTwoNumbers(result, number);
}
return result;
},
_multiplyTwoNumbers: function (operand1, operand2) {
if(parseInt(operand1, 10) === 0 || parseInt(operand2, 10) === 0) return 0;
var operand1 = operand1.toString(),
operand2 = operand2.toString(),
maxDigits = Math.max(operand1.length, operand2.length),
operand1 = normalize(operand1, maxDigits),
operand2 = normalize(operand2, maxDigits),
operand1Digits = operand1.split(""),
operand2Digits = operand2.split(""),
parts = [];
var trailingZeros1 = "";
for (var a = 0; a < maxDigits; a++) {
var place1 = maxDigits - 1 - a,
trailingZeros2 = "";
for (var b = 0; b < maxDigits; b++) {
var place2 = maxDigits - 1 - b,
lead = (parseInt(operand1Digits[place1], 10) * parseInt(operand2Digits[place2], 10)).toString();
parts.push(lead + trailingZeros1 + trailingZeros2);
trailingZeros2 += "0";
}
trailingZeros1 += "0";
}
return this.add.apply(this, parts);
}
}