-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInt3.java
More file actions
349 lines (298 loc) · 8.94 KB
/
Int3.java
File metadata and controls
349 lines (298 loc) · 8.94 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
class Int3 {
int [] rep = new int [3];// rep[3] is the 6 lowest order digits
int max = 100000000,
mask = max-1;;
Int3 () {}
Int3 (String str) {
if (str.length() > 24){
System.out.println("ERROR: Max number: 99999999 99999999 99999999");
return;
}
while (str.length() < 24){
str = "0" + str;
}
rep[0] = Integer.parseInt(str.substring(0,8));
rep[1] = Integer.parseInt(str.substring(8,16));
rep[2] = Integer.parseInt(str.substring(16,24));
}
Int3 (int a, int b, int c) {
rep[0] = a;
rep[1] = b;
rep[2] = c;
}
Int3 (int[] number) {
rep = number;
}
Int3 (long init) {
long temp = init;
int t;
for (int i = 2; i >=0; i--){
rep[i] = (int) (temp % max);
temp = temp/max;
}
}
public void mul(Int3 n){
int[] rep2 = n.getValues();
long temp, rest;
int[] res = new int[3];
temp = (long)rep[2] * (long)rep2[2];
res[2] = (int)(temp%max);
rest = temp/max;
temp = ((long)rep[1] * (long)rep2[2]) + rest;
rest = temp/max;
res[1] = (int)(temp%max);
temp = (long)rep[0] * (long)rep2[2] + rest;
res[0] = (int)(temp%max);
temp = (long)rep[2] * (long)rep2[1] + res[1];
res[1] = (int)(temp%max);
rest = temp/max;
temp = ((long)rep[1] * (long)rep2[1]) + res[0] + rest;
res[0] = (int)(temp%max);
temp = (long)rep[2] * (long)rep2[0] + res[0];
res[0] = (int)(temp%max);
rep[0] = res[0];
rep[1] = res[1];
rep[2] = res[2];
}
public long sqrt(long g){
Int3 x = clone();
x.div(g);
long val = (x.toLong() + g)/2;
while (val-x.toLong() > 1){
x = clone();
x.div(val);
val = (x.toLong() + val)/2;
}
return val;
}
public long sqrt(Int3 g2){
long g;
if (g2.greater(new Int3(Long.MAX_VALUE))){
g = Long.MAX_VALUE;
} else{
g = g2.toLong();
}
Int3 x = clone();
x.div(g);
long val = (x.toLong() + g)/2;
while (val-x.toLong() > 1){
x = clone();
x.div(val);
val = (x.toLong() + val)/2;
}
return val;
}
// Assume that addend is never bigger than 99999999
public void addSmall (int addend) {
rep[2] += addend;
if (rep[2] < max){
return;
}
rep[2] -= max;
rep[1]++;
if (rep[1] > max){
rep[0]++;
rep[1] -= max;
}
if (rep[0] >= max){
System.out.printf("ERROR: %s + %d will overflow.%n", toString(), addend);
}
} // end add
public void add(long addend){
add(new Int3(addend));
}
public void add (Int3 addend) {
int[] addendArr = addend.getValues();
int sum[] = new int[rep.length];
int rest = 0;
sum[2] = rep[2] + addendArr[2] + rest;
rest = 0;
while (sum[2] > mask){
sum[2]-=max;
rest++;
}
rep[2] = sum[2];
sum[1] = rep[1] + addendArr[1] + rest;
rest = 0;
while (sum[1] > mask){
sum[1]-=max;
rest++;
}
rep[1] = sum[1];
sum[0] = rep[0] + addendArr[0] + rest;
rest = 0;
if (sum[0] > mask){
System.out.printf("ERROR: %s + %s will overflow.%n", toString(), addend.toString());
return;
}
rep[0] = sum[0];
} // end add
public void sub(int n){
rep[2]-=n;
if (rep[2] < 0){
rep[2]+= max;
if (rep[1] > 0){
rep[1]--;
} else if (rep[2] > 0){
rep[1] = mask;
rep[2]--;
} else{
System.out.println("Int3 class can not handle negative numbers.");
System.out.printf("%s - %d will be negative..%n", toString(), n);
}
}
}
public void sub(Int3 n) {
int[] repN = n.getValues();
if (repN[0] > rep[0] || (repN[1] > rep[1] && repN[0] > rep[0]) ||
(repN[2] > rep[2] && (repN[1] > rep[1] && repN[0] > rep[0]))){
System.out.println("Int3 class can not handle negative numbers.");
System.out.printf("%s - %s will be negative..%n", toString(), n.toString());
return;
}
rep[0] -= repN[0];
rep[1] -= repN[1];
rep[2] -= repN[2];
if (rep[2] < 0){
rep[2]+=max;
rep[1]--;
}
if (rep[1] < 0){
rep[1]+=max;
rep[0]--;
}
}
public void div(long divisor ) {
long mente =0;
long dividend = mente*max + rep[0];
rep[0] = (int)(dividend/divisor);
mente = (dividend%divisor);
dividend = mente*max + rep[1];
rep[1] = (int)(dividend/divisor);
mente = (dividend%divisor);
dividend = mente*max + rep[2];
rep[2] = (int)(dividend/divisor);
}
public long modulo(long x) {
return (((((((long)rep[0])%x)*max) + (long)rep[1])%x)*max + (long)rep[2])%x;
}
public boolean dividable (long x){
return modulo(x) == 0;
}
public boolean isZero (){
return (rep[0] == 0 && rep[1] == 0 && rep[2] == 0);
}
public boolean isOne (){
return (rep[0] == 0 && rep[1] == 0 && rep[2] == 1);
}
public boolean greater (Int3 n){
int[] repN = n.getValues();
if (rep[0] > repN[0]){
return true;
} else if (rep[0] < repN[0]){
return false;
} else if (rep[1] > repN[1]){
return true;
} else if (rep[1] < repN[1]){
return false;
} else if (rep[2] > repN[2]){
return true;
} else if (rep[2] < repN[2]){
return false;
}
return false;
}
// Faster greater for small values
public boolean greater (long n){
if (max < n){
return (rep[2] > n || rep[1] > 1 || rep[0] > 1);
}
return greater(new Int3(n));
}
public boolean less (Int3 n){
int[] repN = n.getValues();
if (rep[0] < repN[0]){
return true;
} else if (rep[0] > repN[0]){
return false;
} else if (rep[1] < repN[1]){
return true;
} else if (rep[1] > repN[1]){
return false;
} else if (rep[2] < repN[2]){
return true;
} else if (rep[2] > repN[2]){
return false;
}
return false;
}
// Faster greater for small values
public boolean less (long n){
if (max < n){
return (rep[2] < n && rep[1] < 1 && rep[0] < 1);
}
return less(new Int3(n));
}
public boolean equal (Int3 n){
int[] repN = n.getValues();
if (rep[0] != repN[0]){
return false;
} else if (rep[1] != repN[1]){
return false;
} else if (rep[2] != repN[2]){
return false;
}
return true;
}
public boolean equal (long n){
if (n < max){
return (n == rep[2]) && (rep[1] == 0) && (rep[0] == 0);
} else{
return equal(new Int3(n));
}
}
public int[] getValues() {
return rep;
}
public Int3 clone() {
return (new Int3(rep[0], rep[1], rep[2]));
}
public int toInt() {
if (rep[0] == 0 && rep[1] == 0){
return rep[2];
} else if (rep[0] == 0 && (rep[1] < 21 || (rep[1] == 21 && rep[2] <= 47483647))) {
return rep[2] + rep[1]*max;
}
System.out.println("Int3 number is to big to represent as one int: " + toString());
return -1;
}
public long toLong() {
long ret = -1;
if (rep[0] == 0){
return (long)rep[2] + ((long)rep[1]*(long)max);
} else if((rep[0] < 922) ||
(rep[0] == 922 && rep[1] < 33720368) ||
(rep[0] == 922 && rep[1] == 33720368 && rep[2] <= 54775807)){
return (long)rep[2] + (long)rep[1]*(long)max + (long)rep[0]*(long)max*(long)max;
}
System.out.println("Int3 number is to big to represent as one long: " + toString());
return -1;
}
public String toString(){
String ret ="";
for (int i = 0; i < 3 ; i++){
ret += " " + format(rep[i]);
}
return ret;
}// end print
public String format(int rep) {
return String.format("%08d", rep);
}
// Returns number of bits used to represent the number
public int nBitUsed(){
int counter = (32 - Integer.numberOfLeadingZeros(rep[0]));
counter += (32 - Integer.numberOfLeadingZeros(rep[1]));
counter += (32 - Integer.numberOfLeadingZeros(rep[2]));
return counter;
}
} // end class Int4