-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBigNumber.java
293 lines (260 loc) · 7.32 KB
/
BigNumber.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
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
/*
ICA_1
Oscar Barbosa Aquino A01329173
Antonio Diaz Flores A01329256
*/
import java.util.*;
/*Class BigNumber that implements Comparable*/
class BigNumber implements Comparable<BigNumber>{
String number;
/*Constructor with string*/
BigNumber(String s){
number=s;
if (s.length()>64)
throw new BigNumberOverflowException();
for (int i = 0;i<s.length();i++) {
if((int)(s.charAt(i))<48 || (int)(s.charAt(i))>57)
throw new InvalidBigNumberException();
}
}
/*Default Constructor*/
BigNumber(){
number="0";
}
/*Method that returns the length of the BigNumber*/
int length(){
return number.length();
}
/*Method that returns the BigNumber as a String*/
String str(){
return number;
}
/*Method for parsing Integers*/
int intt(String s){
return Integer.parseInt(s);
}
/*Method that sums two BigNumbers*/
BigNumber add(BigNumber other){
String sum = "";
String first ="";
String second = "";
int carry=0;
if (this.compareTo(other)>=0) {
second=other.str();
int dif = this.compareTo(other);
for (int i = 0;i<dif;i++)
second="0"+second;
first = number;
}
else{
second=this.str();
int dif = other.compareTo(this);
for (int i = 0;i<dif;i++)
second="0"+second;
first = other.str();
}
for (int i = first.length()-1;i>=0;i--) {
int tSum = intt(Character.toString(first.charAt(i)))+intt(Character.toString(second.charAt(i)))+carry;
carry=0;
if(tSum>9){
carry=1;
tSum=tSum%10;
}
sum=Integer.toString(tSum)+sum;
if(i==0 && carry==1)
sum=carry+sum;
}
return new BigNumber(sum);
}
/*Method that substracts BigNumbers*/
BigNumber subs(BigNumber other){
String sub = "";
String first ="";
String second = "";
int extraSub=0;
if (this.compareNumericValue(other)>=0) {
second=other.str();
int dif = this.compareTo(other);
for (int i = 0;i<dif;i++)
second="0"+second;
first = number;
}
else{
throw new ArithmeticException();
}
for (int i = first.length()-1;i>=0;i--) {
int upDigit=intt(Character.toString(first.charAt(i)))-extraSub;
int downDigit=intt(Character.toString(second.charAt(i)));
int tSub=0;
if (upDigit<downDigit) {
if(upDigit==-1)
upDigit=9;
else
upDigit=upDigit+10;
tSub = upDigit-downDigit;
extraSub=1;
}
else{
tSub = upDigit-downDigit;
extraSub=0;
}
if (i==0 && tSub==0) {
sub=sub;
}
else
sub=Integer.toString(tSub)+sub;
}
if (sub.equals(""))
sub="0";
if (String.valueOf(sub.charAt(0)).equals("0") && sub.length()>1)
sub=sub.substring(1);
return new BigNumber(sub);
}
/*Method that multiplies BigNumbers*/
BigNumber mult(BigNumber other){
String mult = "";
String first ="";
String second = "";
ArrayList<BigNumber> sums = new ArrayList<BigNumber>();
int noSum = 0;
int carry=0;
if (this.compareTo(other)>=0) {
first = number;
second=other.str();
}
else{
first = other.str();
second = number;
}
for (int i =second.length()-1;i>=0;i--) {
String tempBN="";
carry=0;
for (int k = 0;k<noSum;k++)
tempBN+="0";
int multiple = intt(Character.toString(second.charAt(i)));
for (int j =first.length()-1;j>=0;j--) {
int tMult= multiple * intt(Character.toString(first.charAt(j))) + carry;
String stMult = Integer.toString(tMult);
if (tMult>9) {
carry=intt(Character.toString(stMult.charAt(0)));
stMult=Character.toString(stMult.charAt(1));
}
else{
carry=0;
stMult=Character.toString(stMult.charAt(0));
}
tempBN=stMult+tempBN;
if(j==0 && carry>0)
tempBN=carry+tempBN;
}
sums.add(new BigNumber(tempBN));
noSum++;
}
BigNumber totalSum = new BigNumber();
for (int i = 0;i<sums.size();i++) {
totalSum=sums.get(i).add(totalSum);
}
return totalSum;
}
/*Method that divides BigNumbers*/
BigNumber div(BigNumber other){
BigNumber quotient = null;
if(this.compareNumericValue(other)==-1 || other.str().equals("0"))
throw new ArithmeticException("Not possible division");
else if (this.compareNumericValue(other)==0)
quotient=new BigNumber("1");
else{
int current=0;
String quotientStr="";
boolean stillDividing=true;
BigNumber residue = new BigNumber();
boolean firstTry=true;
int pastDec=0;
BigNumber numDivOr=this;
while(stillDividing){
BigNumber e = new BigNumber("1");
String numStr ="";
int lastDec=0;
int timesSubs=0;
BigNumber numDiv=new BigNumber();
current=0;
for (int i =0;this.length()-numDivOr.length()>0;i++ )
numDivOr=new BigNumber("0"+numDivOr.str());
for (int i=0;numDiv.compareNumericValue(other)<0;i++){
numStr+=numDivOr.str().charAt(i);
numDiv = new BigNumber(numStr);
current++;
}
while(numDiv.compareNumericValue(other)>=0){
numDiv=numDiv.subs(other);
timesSubs++;
}
if (current-pastDec>1)
if (firstTry==false)
quotientStr+="0";
quotientStr+=Integer.toString(timesSubs);
quotient=new BigNumber(quotientStr);
for (int i = this.length()-1;i>=current;i--)
lastDec++;
for (int j=0;j<lastDec;j++)
e=e.mult(new BigNumber("10"));
residue=this.subs(e.mult(other).mult(quotient));
numDivOr=residue;
pastDec=current;
firstTry=false;
if (residue.compareNumericValue(other)<0 || lastDec==0){
stillDividing=false;
if (residue.compareNumericValue(new BigNumber())==0 || residue.compareNumericValue(new BigNumber())==1)
quotient=quotient.mult(e);
}
}
}
return quotient;
}
/*Method that gets the reminder of a
BigNumbers division*/
BigNumber mod(BigNumber other){
BigNumber residue = this.subs(this.div(other).mult(other));
int zeros=0;
for (int i = 0;i<residue.length()-1;i++)
if (Character.toString(residue.str().charAt(i)).equals("0"))
zeros++;
return new BigNumber(residue.str().substring(zeros));
}
/*CompareTo of length Overriding*/
public int compareTo(BigNumber other){
if (number.length()==other.length())
return 0;
else if (number.length()>other.length())
return number.length()-other.length();
else
return number.length()-other.length();
}
/*Method that compares numeric value of BigNumbers*/
public int compareNumericValue(BigNumber other){
boolean equals=true;
String first =this.str();
String second =other.str();
if (this.compareTo(other)>=0) {
int dif = this.compareTo(other);
for (int i = 0;i<dif;i++)
second="0"+second;
}
else{
int dif = other.compareTo(this);
for (int i = 0;i<dif;i++)
first="0"+first;
}
for (int i =0;i<first.length();i++) {
if (intt(Character.toString(first.charAt(i)))>intt(Character.toString(second.charAt(i))))
return 1;
else if (intt(Character.toString(first.charAt(i)))<intt(Character.toString(second.charAt(i))))
return -1;
}
return 0;
}
/*Method toString*/
public String toString(){
return number;
}
}