-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigInt.java
219 lines (204 loc) · 4.97 KB
/
BigInt.java
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
package lab4;
import java.util.Arrays;
// so that we can print out arrays using Arrays.toString(...)
public class BigInt {
public static final int SIZE = 20;
// length of arrays representing big ints
public static final int[] NaBI = { -1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
// error value
public static int[] intToBigInt(int n) {
int flength = 20;
int [] a = new int [SIZE];
int lengthn = String.valueOf(n).length();
String b = Integer.toString(n);
if (lengthn > SIZE||n < 0) {
return NaBI;
}
for (int i = 0; i < b.length(); i++) {
char extra = b.charAt(i);
int integer = Character.getNumericValue(extra);
a[flength - b.length()] = integer;
flength++;
}
return a;
}
public static int[] stringToBigInt(String s) {
int i = 0;
int flength = 20;
int [] a = new int [SIZE];
int lengths = s.length();
for (int h = 0; h < s.length(); h++) {
char b = s.charAt(h);
if (Character.isLetter(b) == true) {
return NaBI;
}
}
if (lengths > SIZE) {
return NaBI;
}
int actualn = Character.getNumericValue(s.charAt(i));
while (i < lengths) {
if (actualn < 0 || actualn > 9) {
return NaBI;
}
else {
i++;
}
}
for (int j = 0; j < lengths; j++) {
char extra = s.charAt(j);
int integer = Character.getNumericValue(extra);
a[flength - s.length()] = integer;
flength++;
}
return a; // just to get it to compile
}
public static String bigIntToString(int[] A) {
String b = "";
int right = 0;
for (int i = 0; i < A.length; i++) {
if (Character.isLetter(A[i])||A[i] > 9||A[i] < 0) {
return "NaBI";
}
if (A[i] > 0 && A[i] < 9){
right = i;
break;
}
}
String a = Arrays.toString(A);
b = a.substring(right, a.length()-1);
String d = b.replaceAll(",", "");
String e = d.replaceAll(" ", "");
e = e.replace("[", "");
e = e.replace("]", "");
for (int i = 0; i < e.length(); i++) {
if (e.charAt(i) != '0'){
right = i;
break;
}
if (i == e.length()-1 && e.charAt(i) == '0') {
return "0";
}
}
String f = e.substring(right, e.length());
return f;
}
// Compare the two big integers A and B and return -1, 0, or 1, depending
// on whether A < B, A == B, or A > B, respectively.
public static int compareTo(int[] A, int[] B) {
int twenty = 20;
int largenuma = 0;
int largenumb = 0;
for (int i = 0; i < twenty; i++) {
if (A[i] > 0) {
largenuma = i;
}
if (B[i] > 0) {
largenumb = i;
}
if (largenuma > largenumb) {
return 1;
}
if (largenuma < largenumb) {
return -1;
}
if(A[i] < B[i]) {
if(i == twenty - 1) {
return -1;
}
if (i == 0) {
return -1;
}
}
if (A[i] > B[i]) {
if (i == twenty - 1) {
return 1;
}
if (i == 0) {
return 1;
}
return 1;
}
if (A[i] == B[i]) {
if(i == twenty - 1) {
return 0;
}
}
}
return 0; // just to get it to compile
}
// Add two big integers and return a new array representing their sum; if the result overflows,
// i.e., contains more than SIZE digits, return NaBI.
public static int[] mul(int[] A, int[] B) {
int[] main = new int[20];
int adder = 0;
int perm = 0;
int extra = 0;
int f = 0;
for (int i = 19; i >= 0; i--) {
if (B[i] == 0){
continue;
}
for (int j = 19; j >= 0; j--){
if (A[j] == 0){
continue;
}
f = j + i - 20 + 1;
if (f >= 20){
return null;
}
main[f] = main[f] + A[j] * B[i];
if (main[f] > 9){
perm = main[f] % 10;
extra = (main[f] - perm)/10;
adder = extra;
if (f > 0){
main[f - 1] = main[f - 1] + adder;
}
main[f] = perm;
if (f == 0){
return null;
}
}
else{
adder = 0;
}
}
}
return main;
}
public static int[] add(int[] A, int[] B) {
int r = 19;
int additional = 0;
int [] last = new int [SIZE];
if (additional == 1) {
return NaBI;
}
while (r >=0) {
int added = A[r] + B[r];
if (additional == 1) {
added += 1;
additional = 0;
}
if (added > 9) {
additional = 1;
added -= 10;
}
last[r] = added;
r--;
}
int z = 0;
while (0 < last.length) {
if (last[z] == 0) {
z++;
if (z == last.length) {
return NaBI;
}
}
if (last[z] > 0) {
break;
}
}
return last; // just to get it to compile
}
}