-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargeInteger.java
More file actions
451 lines (414 loc) · 12.9 KB
/
Copy pathLargeInteger.java
File metadata and controls
451 lines (414 loc) · 12.9 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
* Networked Chat with RSA Encryption/Decryption
* Project 5
* CS 342 - Fall 2017
*
* - Margi Katwala
* - Bushra Baqui
* - Aditya Sinha
*
* **LargeInteger.java**
*/
import java.util.List;
import java.util.Collections;
import java.util.ArrayList;
public class LargeInteger
{
List<Integer> integerValue;
boolean negativeNumber;
public LargeInteger()
{
negativeNumber = false;
integerValue = new ArrayList<>();
}
public LargeInteger(String str)
{
integerValue = new ArrayList<>();
// value for the integers start with -1
negativeNumber = str.startsWith("-1");
for (int i = 0; i < str.length(); i++)
{
if (Character.isDigit(str.charAt(i)))
{
integerValue.add(str.charAt(i) - '0');
}
}
Collections.reverse(integerValue);
}
public LargeInteger(List<Integer> values, boolean negative)
{
this.integerValue = values;
this.negativeNumber = negative;
}
public LargeInteger(int n)
{
negativeNumber = false;
if (n < 0)
{
negativeNumber = true;
n = -n;
}
integerValue = new ArrayList<>();
while (n > 0)
{
int rem = n % 10;
n /= 10;
integerValue.add(rem);
}
}
public LargeInteger(LargeInteger n)
{
integerValue = new ArrayList<>();
for (int v : n.getIntegerValue())
{
integerValue.add(v);
}
negativeNumber = n.isNegativeNumber();
}
static LargeInteger divideBy2(LargeInteger a)
{
List<Integer> endList = new ArrayList<>();
int lastNumber = 0;
boolean firstNumber = false;
for (int i = a.getIntegerValue().size() - 1; i >= 0; i--)
{
if (!firstNumber)
{
if (a.getIntegerValue().get(i) == 0)
continue;
}
firstNumber = true;
int tmp = a.getIntegerValue().get(i) + 10 * lastNumber;
int v = tmp / 2;
endList.add(v);
lastNumber = tmp % 2;
}
Collections.reverse(endList);
trimLeadingZeros(endList);
return new LargeInteger(endList, a.isNegativeNumber());
}
private static void trimLeadingZeros(List<Integer> newList)
{
while (newList.size() > 0)
{
if (newList.get(newList.size() - 1) == 0)
newList.remove(newList.size() - 1);
else
break;
}
}
private static int absCompare(LargeInteger a, LargeInteger b)
{
for (int i = Math.max(a.getIntegerValue().size(), b.getIntegerValue().size()) - 1; i >= 0; i--)
{
int t1 = ((i < a.getIntegerValue().size()) ? a.getIntegerValue().get(i) : 0);
int t2 = ((i < b.getIntegerValue().size()) ? b.getIntegerValue().get(i) : 0);
if (t1 < t2)
return -1;
else if (t2 < t1)
return 1;
}
return 0;
}
// compares the two integers...
public static int compare(LargeInteger a, LargeInteger b)
{
if (!a.isNegativeNumber() && !b.isNegativeNumber())
return absCompare(a, b);
else if (a.isNegativeNumber() && b.isNegativeNumber())
{
return -absCompare(a, b);
}
else if (a.isNegativeNumber())
{
return -1;
}
else if (b.isNegativeNumber())
{
return 1;
}
return 0;
}
public List<Integer> getIntegerValue()
{
return integerValue;
}
public boolean isNegativeNumber()
{
return negativeNumber;
}
public void setNegative(boolean negative)
{
this.negativeNumber = negative;
}
// adding the large integers...
void add(LargeInteger from)
{
if (!from.isNegativeNumber() && !isNegativeNumber())
{
List<Integer> fromList = from.getIntegerValue();
int index = 0;
int remainingNumber = 0;
List<Integer> newList = new ArrayList<>();
for (index = 0; index < fromList.size() && index < integerValue.size(); index++)
{
int a = fromList.get(index) + integerValue.get(index) + remainingNumber;
newList.add(a % 10);
remainingNumber = a / 10;
}
for (; index < fromList.size(); index++)
{
int a = fromList.get(index) + remainingNumber;
newList.add(a % 10);
remainingNumber = a / 10;
}
for (; index < integerValue.size(); index++)
{
int a = integerValue.get(index) + remainingNumber;
newList.add(a % 10);
remainingNumber = a / 10;
}
if (remainingNumber > 0)
{
newList.add(remainingNumber);
}
integerValue = newList;
}
else if (from.isNegativeNumber() && isNegativeNumber())
{
from.setNegative(false);
setNegative(false);
add(from);
setNegative(true);
}
else if (absCompare(from, this) == 0)
{
integerValue = new ArrayList<>();
negativeNumber = false;
}
else if (isNegativeNumber())
{
if (absCompare(this, from) > 0)
{
LargeInteger t = subHelper(this, from);
integerValue = t.getIntegerValue();
negativeNumber = true;
}
else
{
LargeInteger t = subHelper(from, this);
integerValue = t.getIntegerValue();
negativeNumber = false;
}
}
else
{
if (absCompare(this, from) > 0)
{
LargeInteger t = subHelper(this, from);
integerValue = t.getIntegerValue();
negativeNumber = false;
}
else
{
LargeInteger t = subHelper(from, this);
integerValue = t.getIntegerValue();
negativeNumber = true;
}
}
}
// multiplies the large integers...
LargeInteger multiply(LargeInteger a)
{
if (a.getIntegerValue().size() == 0 || getIntegerValue().size() == 0)
{
setNegative(false);
integerValue = new ArrayList<>();
}
LargeInteger answer = new LargeInteger();
for (int i = 0; i < a.getIntegerValue().size(); i++)
{
ArrayList<Integer> create = new ArrayList<>();
int remaining = 0;
for (int j = 0; j < integerValue.size(); j++)
{
remaining = a.getIntegerValue().get(i) * getIntegerValue().get(j) + remaining;
create.add(remaining % 10);
remaining /= 10;
}
create.add(remaining);
for (int j = 0; j < i; j++)
{
create.add(0, 0);
}
answer.add(new LargeInteger(create, false));
}
integerValue = answer.getIntegerValue();
setNegative(negativeNumber != a.isNegativeNumber());
return this;
}
// mods the large integers...
LargeInteger modMultiply(LargeInteger a, LargeInteger mod)
{
if (a.getIntegerValue().size() == 0 || getIntegerValue().size() == 0)
{
setNegative(false);
integerValue = new ArrayList<>();
}
LargeInteger result = new LargeInteger();
for (int i = 0; i < a.getIntegerValue().size(); i++)
{
ArrayList<Integer> aList = new ArrayList<>();
int remNum = 0;
for (int j = 0; j < integerValue.size(); j++)
{
remNum = a.getIntegerValue().get(i) * getIntegerValue().get(j) + remNum;
aList.add(remNum % 10);
remNum /= 10;
}
aList.add(remNum);
for (int j = 0; j < i; j++)
{
aList.add(0, 0);
}
LargeInteger t = new LargeInteger(aList, false);
t = t.mod(mod);
result.add(t);
result = result.mod(mod);
}
integerValue = result.getIntegerValue();
setNegative(negativeNumber != a.isNegativeNumber());
return this;
}
// divides the large integers...
LargeInteger divide(LargeInteger a)
{
if (absCompare(a, this) > 0)
{
integerValue = new ArrayList<>();
negativeNumber = false;
}
else if (absCompare(a, this) == 0)
{
integerValue = new ArrayList<>();
integerValue.add(1);
negativeNumber = isNegativeNumber() != a.isNegativeNumber();
}
else
{
negativeNumber = isNegativeNumber() != a.isNegativeNumber();
LargeInteger lowNumber = new LargeInteger(1);
LargeInteger highNumber = new LargeInteger(this);
highNumber.setNegative(false);
lowNumber.setNegative(false);
while (absCompare(highNumber, lowNumber) > 0)
{
LargeInteger newNumber = new LargeInteger(highNumber);
newNumber.add(lowNumber);
LargeInteger middleNumber = divideBy2(newNumber);
newNumber = new LargeInteger(middleNumber);
newNumber.multiply(a);
if (absCompare(newNumber, this) <= 0)
{
LargeInteger newLargeNum = new LargeInteger(middleNumber);
newLargeNum.add(new LargeInteger(1));
newLargeNum.multiply(a);
if (absCompare(newLargeNum, this) > 0)
{
integerValue = middleNumber.getIntegerValue();
return this;
}
}
if (absCompare(newNumber, this) > 0)
{
highNumber = middleNumber;
}
else
{
lowNumber = middleNumber;
lowNumber.add(new LargeInteger(1));
}
}
}
return this;
}
// parses the integers
public int toInt()
{
return Integer.parseInt(toString());
}
LargeInteger mod(LargeInteger a)
{
LargeInteger modNumber = new LargeInteger(this);
modNumber.divide(a);
LargeInteger multiplyNumber = new LargeInteger(a);
multiplyNumber.multiply(modNumber);
LargeInteger subtractNumber = new LargeInteger(this);
subtractNumber.subtract(multiplyNumber);
return subtractNumber;
}
public boolean isEven()
{
return integerValue.size() == 0 || integerValue.get(0) % 2 == 0;
}
public LargeInteger subtract(LargeInteger a)
{
LargeInteger t = new LargeInteger(a);
t.setNegative(!a.isNegativeNumber());
add(t);
return this;
}
// helper function for the subtraction function...
private LargeInteger subHelper(LargeInteger a, LargeInteger b)
{
int carryNumber = 0;
List<Integer> newList = new ArrayList<>();
for (int i = 0; i < Math.max(a.getIntegerValue().size(), b.getIntegerValue().size()); i++)
{
int result = ((i < a.integerValue.size()) ? a.integerValue.get(i) : 0) -
((i < b.getIntegerValue().size()) ? b.getIntegerValue().get(i) : 0) - carryNumber;
if (result < 0)
{
newList.add(result + 10);
carryNumber = 1;
}
else
{
newList.add(result);
carryNumber = 0;
}
}
return new LargeInteger(newList, carryNumber > 0);
}
@Override
public String toString()
{
if (integerValue.size() == 0)
{
return "0";
}
trimLeadingZeros(integerValue);
String ret = "";
if (negativeNumber)
ret = "-";
for (int i = integerValue.size() - 1; i >= 0; i--)
ret += integerValue.get(i);
return ret;
}
LargeInteger power(int p)
{
LargeInteger returnInteger = new LargeInteger(1);
LargeInteger newNumber = new LargeInteger(this);
while (p > 0)
{
if (p % 2 == 1)
{
returnInteger.multiply(newNumber);
}
p = p / 2;
newNumber.multiply(newNumber);
}
trimLeadingZeros(returnInteger.getIntegerValue());
return returnInteger;
}
}