forked from mmurshed/biginteger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBigIntegerOperations.h
272 lines (231 loc) · 7.33 KB
/
BigIntegerOperations.h
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
/**
* BigInteger Class
* Version 9.0
* S. M. Mahbub Murshed (murshed@gmail.com)
*/
#ifndef BIGINTEGER_OPERATIONS
#define BIGINTEGER_OPERATIONS
#include <vector>
using namespace std;
#include "BigInteger.h"
#include "BigIntegerComparator.h"
#include "algorithms/classic/ClassicAddition.h"
#include "algorithms/classic/ClassicSubtraction.h"
#include "algorithms/classic/ClassicMultiplication.h"
#include "algorithms/karatsuba/KaratsubaMultiplication.h"
#include "algorithms/toomcook/ToomCookMultiplication.h"
#include "algorithms/toomcookmemoptim/ToomCookMultiplicationMemOptimized.h"
namespace BigMath
{
class BigIntegerOperations
{
// Karatsuba performs better for over 128 digits for the result
static const SizeT MULTIPLICATION_SWITCH = 128;
private:
// Implentation of addition by paper-pencil method
static BigInteger AddUnsigned(BigInteger const& a, BigInteger const& b)
{
return BigInteger(
ClassicAddition::Add(
a.GetInteger(),
b.GetInteger(),
BigInteger::Base())
);
}
// Implentation of subtraction by paper-pencil method
// Assumption: a > b
static BigInteger SubtractUnsigned(BigInteger const& a, BigInteger const& b)
{
return BigInteger(
ClassicSubtraction::Subtract(
a.GetInteger(),
b.GetInteger(),
BigInteger::Base())
);
}
static BigInteger MultiplyUnsigned(BigInteger const& a, BigInteger const& b)
{
SizeT size = a.size() + b.size();
if(size <= MULTIPLICATION_SWITCH)
{
return BigInteger(
ClassicMultiplication::Multiply(
a.GetInteger(),
b.GetInteger(),
BigInteger::Base())
);
}
return BigInteger(
KaratsubaMultiplication::Multiply(
a.GetInteger(),
b.GetInteger(),
BigInteger::Base())
);
// ToomCookMultiplication tcm;
// return BigInteger(
// tcm.Multiply(
// a.GetInteger(),
// b.GetInteger(),
// BigInteger::Base())
// );
// return BigInteger(
// ToomCookMultiplicationMemOptimized::Multiply(
// a.GetInteger(),
// b.GetInteger(),
// BigInteger::Base())
// );
}
public:
static BigInteger Add(BigInteger const& a, BigInteger const& b)
{
// Check for zero
bool aZero = a.IsZero();
bool bZero = b.IsZero();
if(aZero && bZero)
return BigInteger(); // 0 + 0
if(aZero)
return BigInteger(b); // 0 + b
if(bZero)
return BigInteger(a); // a + 0
// Check if actually subtraction is needed
bool aNeg = a.IsNegative();
bool bNeg = b.IsNegative();
if(aNeg && !bNeg)
return SubtractUnsigned(b, a); // a is negative, b is not. return b - a
else if(!aNeg && bNeg)
return SubtractUnsigned(a, b); // b is negative and a is not. return a - b
// Add
BigInteger result = AddUnsigned(a, b);
// Flip the sign when adding two negative numbers
if(aNeg && bNeg)
result.SetSign(true);
return result;
}
// Straight pen-pencil implementation for subtraction
static BigInteger Subtract(BigInteger const& a, BigInteger const& b)
{
// Check for zero
bool aZero = a.IsZero();
bool bZero = b.IsZero();
if(aZero && bZero)
return BigInteger(); // 0 - 0
if(aZero)
{
BigInteger result(b);
result.SetSign(!b.IsNegative()); // 0 - b
return result;
}
if(bZero)
return BigInteger(a); // a - 0
// Check if actually addition is needed
bool aNeg = a.IsNegative();
bool bNeg = b.IsNegative();
if(aNeg && !bNeg)
return AddUnsigned(a, b).SetSign(true); // a is negative, b is not. return -(a + b)
else if(!aNeg && bNeg)
return AddUnsigned(a, b); // b is negative and a is not. return a + b
Int cmp = BigIntegerComparator::CompareTo(a.GetInteger(), b.GetInteger());
if(cmp < 0)
return SubtractUnsigned(b, a).SetSign(true); // -(b - a)
else if (cmp > 0)
return SubtractUnsigned(a, b); // a - b
return BigInteger(); // Zero when a == b
}
static BigInteger Multiply(BigInteger const& a, BigInteger const& b)
{
if(a.IsZero() || b.IsZero())
return BigInteger(); // 0 times anything is zero
BigInteger result = MultiplyUnsigned(a, b);
if(a.IsNegative() != b.IsNegative())
result.SetSign(true);
return result;
}
static vector<BigInteger> DivideAndRemainder(BigInteger const& a, BigInteger const& b)
{
vector<BigInteger> results(2);
if(a.IsZero() || b.IsZero())
{
results[0] = BigInteger();
results[1] = BigInteger();
return results; // case of 0
}
Int cmp = BigIntegerComparator::CompareTo(a.GetInteger(), b.GetInteger());
if(cmp == 0)
{
vector<DataT> one(1);
one[0] = 1;
results[0] = BigInteger(one, a.IsNegative() || b.IsNegative());
results[1] = BigInteger();
return results; // case of a/a
}
else if (cmp < 0)
{
results[0] = BigInteger();
results[1] = BigInteger(a.GetInteger(), a.IsNegative() || b.IsNegative() );
return results; // case of a < b
}
// Now: a > b
vector< vector<DataT> > result = ClassicDivision::DivideAndRemainder(
a.GetInteger(),
b.GetInteger(),
BigInteger::Base());
results[0] = BigInteger(result[0], false);
results[1] = BigInteger(result[1], false);
if(a.IsNegative() != b.IsNegative())
{
results[0].SetSign(true);
results[1].SetSign(true);
}
return results;
}
};
// Adds Two BigInteger
BigInteger operator+(BigInteger const& a, BigInteger const& b)
{
return BigIntegerOperations::Add(a, b);
}
// Subtructs Two BigInteger
BigInteger operator-(BigInteger const& a, BigInteger const& b)
{
return BigIntegerOperations::Subtract(a, b);
}
// Multiplies Two BigInteger
BigInteger operator*(BigInteger const& a, BigInteger const& b)
{
return BigIntegerOperations::Multiply(a, b);
}
BigInteger operator/(BigInteger const& a, BigInteger const& b)
{
return BigIntegerOperations::DivideAndRemainder(a, b)[0];
}
BigInteger operator%(BigInteger const& a, BigInteger const& b)
{
return BigIntegerOperations::DivideAndRemainder(a, b)[1];
}
// Comparison operators
bool operator==(BigInteger const& a, BigInteger const& b)
{
return a.CompareTo(b) == 0;
}
bool operator!=(BigInteger const& a, BigInteger const& b)
{
return a.CompareTo(b) != 0;
}
bool operator>=(BigInteger const& a, BigInteger const& b)
{
return a.CompareTo(b) >= 0;
}
bool operator<=(BigInteger const& a, BigInteger const& b)
{
return a.CompareTo(b) <= 0;
}
bool operator>(BigInteger const& a, BigInteger const& b)
{
return a.CompareTo(b)>0;
}
bool operator<(BigInteger const& a, BigInteger const& b)
{
return a.CompareTo(b)<0;
}
}
#endif