/
bigint.cpp
445 lines (406 loc) · 11.9 KB
/
bigint.cpp
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
// $Id: bigint.cpp,v 1.250 2012-04-30 12:58:21-07 - - $
/*
* B Stewart Bracken
* bbracken@ucsc.edu
*/
#include <locale>
#include <exception>
#include <limits>
#include <stack>
#include <stdexcept>
#include <sstream>
#include <cmath>
using namespace std;
#include "bigint.h"
#include "trace.h"
#define CDTOR_TRACE TRACE ('~', this<<" -> " << \
big_value <<" = "<<*this)
bigint::bigint (): negative(false),
big_value (new bigvalue_t()) {
CDTOR_TRACE;
}
bigint::bigint (const bigint &that): negative(that.negative){
if (this == &that)
*this = that;
else
big_value = new bigvalue_t(*that.big_value);
CDTOR_TRACE;
}
bigint &bigint::operator= (const bigint &that) {
TRACE('c', "Copying by value (deep) " << *this);
if (this == &that) return *this;
if ( big_value != NULL )
delete big_value;
big_value = new bigvalue_t(*(that.big_value));
this->negative = that.negative;
return *this;
}
bigint::~bigint() {
TRACE('~', "Deconstructing: this["<<this<<"] -> "<<*this);
if ( big_value!=NULL)
delete big_value;
}
bigint::bigint (int that): negative(false),
big_value(new bigvalue_t()) {
string s;
stringstream out;
out << that;
s = out.str();
init(s);
CDTOR_TRACE;
}
bigint::bigint (const string &that): negative(false),
big_value(new bigvalue_t()){
init(that);
CDTOR_TRACE;
}
void bigint::init(const string& s){
string::const_reverse_iterator itor = s.rbegin();
string::const_reverse_iterator end = s.rend();
int offset = 0;
if ( *(end - 1) == '-' || *(end - 1) == '_' ) {
negative = true;
offset = 1;
}
for(; itor != end-offset; ++itor ){
if (! isdigit (*itor)) continue;
big_value->push_back( *itor - '0' );
}
}
bigint bigint::operator+ (const bigint &that) const {
bigint result = bigint();
if ( this->negative == that.negative ){
do_bigadd(this->big_value, that.big_value, result.big_value);
result.set_negative(that.negative);
}else {
int abs_compare = abscompare(that);
if ( abs_compare > 0 ){
do_bigsub(this->big_value, that.big_value, result.big_value);
result.set_negative(negative);
}else if ( abs_compare < 0 ){
do_bigsub(that.big_value, this->big_value, result.big_value);
result.set_negative(that.negative);
}
}
result.zero_positive_check();
return result;
}
bigint bigint::operator- (const bigint &that) const {
bigint result = bigint();
if ( negative == that.negative ){
int abs_compare = abscompare(that);
if ( abs_compare > 0 ){
do_bigsub(big_value, that.big_value, result.big_value);
result.set_negative(this->negative?true:false);
}else if ( abs_compare < 0){
do_bigsub(that.big_value, big_value, result.big_value);
result.set_negative(negative?false:true);
}
}else {
do_bigadd( big_value, that.big_value, result.big_value);
if ( negative ) result.set_negative(negative);
}
result.zero_positive_check();
return result;
}
bigint bigint::operator- () const {
bigint result = *this;
result.set_negative(!negative);
return result;
}
int bigint::compare (const bigint &that) const {
if ( negative == false && that.negative == true )
return 1;
else if ( negative == true && that.negative == false )
return -1;
bool sign = negative;
unsigned long this_size, that_size;
this_size = big_value->size();
that_size = that.big_value->size();
if ( this_size > that_size ){
if( sign == false )
return 1;
else
return -1;
}else if ( this_size < that_size ){
if( sign == false )
return -1;
else
return 1;
}
else { //equal size vectors
bigvalue_t::const_reverse_iterator itor_this =
big_value->rbegin();
bigvalue_t::const_reverse_iterator end_this =
big_value->rend();
bigvalue_t::const_reverse_iterator itor_that =
that.big_value->rbegin();
bigvalue_t::const_reverse_iterator end_that =
that.big_value->rend();
while ( itor_this != end_this && itor_that != end_that ){
if ( *itor_this > *itor_that ){
if ( sign == false ) return 1;
else return -1;
}else if ( *itor_this < *itor_that ){
if ( sign == false ) return -1;
else return 1;
}
++itor_this;
++itor_that;
}
}
//EQUAL!!!
return 0;
}
int bigint::abscompare (const bigint &that) const {
unsigned long this_size, that_size;
bigvalue_t::const_reverse_iterator itor_this = big_value->rbegin();
bigvalue_t::const_reverse_iterator end_this = big_value->rend();
bigvalue_t::const_reverse_iterator itor_that =
that.big_value->rbegin();
bigvalue_t::const_reverse_iterator end_that =
that.big_value->rend();
this_size = big_value->size();
that_size = that.big_value->size();
if ( this_size > that_size )
return 1;
else if ( this_size < that_size )
return -1;
else {
while ( itor_this != end_this && itor_that != end_that ){
if ( *itor_this > *itor_that ) return 1;
else if ( *itor_this < *itor_that ) return -1;
++itor_this;
++itor_that;
}
}
return 0;
}
int bigint::smallint () const {
if (*this < numeric_limits<int>::min()
|| *this > numeric_limits<int>::max())
throw range_error ("smallint: out of range");
int small = 0;
bigvalue_t::const_iterator itor = big_value->begin();
bigvalue_t::const_iterator end = big_value->end();
for ( int i = 1; itor != end; ++itor, i*=10 ){
small += i * (*itor);
}
if ( negative ) small = -small;
return small;
}
bigint bigint::mul_by_2 () {
bigint result = bigint();
do_bigadd(big_value,big_value,result.big_value);
return result;
}
//a is bigger than b
void bigint::do_bigadd(const bigvalue_t* a,
const bigvalue_t* b,
bigvalue_t* target) const{
int carry = 0;
bool a_done = false, b_done = false;
bigvalue_t::const_iterator itor_a = a->begin();
bigvalue_t::const_iterator end_a = a->end();
bigvalue_t::const_iterator itor_b = b->begin();
bigvalue_t::const_iterator end_b = b->end();
if ( a->size() == 0 ) a_done = true;
if ( b->size() == 0 ) b_done = true;
for(;;){
int sum = (b_done?0:*itor_b) + (a_done?0:*itor_a) + carry;
TRACE('b', char(*itor_a+'0') << " + "<<char(*itor_b+'0')<<
" + "<<carry<< " = " << sum);
carry = 0;
if ( sum >= 10 ) {
carry = 1;
sum -= 10;
TRACE('b'," Accounting for >10, diff - 10 = "
<< sum);
}
target->push_back(sum);
if( ++itor_a == end_a ) a_done = true;
if ( ++itor_b == end_b ) b_done = true;
if ( a_done && b_done ){
if ( carry > 0 ) target->push_back(carry);
break;
}
}
remove_lead_0s(target);
}
//a is bigger than b
void bigint::do_bigsub(const bigvalue_t* a,
const bigvalue_t* b,
bigvalue_t* target) const{
int carry = 0;
bool a_done = false, b_done = false;
bigvalue_t::const_iterator itor_a = a->begin();
bigvalue_t::const_iterator end_a = a->end();
bigvalue_t::const_iterator itor_b = b->begin();
bigvalue_t::const_iterator end_b = b->end();
if ( a->size() == 0 ) a_done = true;
if ( b->size() == 0 ) b_done = true;
for(;;){
int diff = (a_done?0:*itor_a) - (b_done?0:*itor_b) + carry;
TRACE('b', char(*itor_a+'0') << " - "<<char(*itor_b+'0')<<
" + "<<carry<< " = " << diff);
carry = 0;
if ( diff < 0 && !a_done ) {
carry = -1;
diff += 10;
TRACE('b'," Accounting for negative, diff + 10 = "
<< diff);
}
target->push_back(diff);
if( !a_done && ++itor_a == end_a ) a_done = true;
if ( !b_done && ++itor_b == end_b ) b_done = true;
if ( a_done && b_done )break;
}
remove_lead_0s(target);
}
void bigint::remove_lead_0s(bigvalue_t* value) const{
bigvalue_t::const_reverse_iterator itor = value->rbegin();
bigvalue_t::const_reverse_iterator end = value->rend();
while ( *itor == 0 && itor != end){
++itor;
value->pop_back();
}
}
void bigint::set_negative(bool positivity){
negative = positivity;
}
void bigint::zero_positive_check(){
if ( big_value->size() == 0 )
negative = false;
}
static bigpair popstack (stack <bigpair> &egyptstack) {
bigpair result = egyptstack.top ();
egyptstack.pop();
return result;
}
//
// Ancient Egyptian multiplication algorithm.
//
bigint bigint::operator* (const bigint &that) const {
bigint left = *this;
bigint top = that;
bigint count = 1;
TRACE ('*', *this << " * " << that);
stack <bigpair> egyptstack;
while ( count <= *this ){
TRACE('*',"Pushing: "<<count<<" & "<<top);
egyptstack.push(bigpair(count,top));
top = top.mul_by_2();
count = count.mul_by_2();
}
bigint result;
while ( !egyptstack.empty() ){
bigpair pair = popstack(egyptstack);
TRACE('*',"Popping: "<<pair.first<<" & "<<pair.second);
if ( pair.first <= left ){
left = left - pair.first;
result = result + pair.second;
}
}
if ( negative != that.negative )
result.set_negative(true);
return result;
}
//
// Ancient Egyptian division algorithm.
//
bigpair bigint::div_rem (const bigint &that) const {
if (that == 0) throw range_error ("divide by 0");
TRACE ('/', *this << " /% " << that);
stack <bigpair> egyptstack;
bigint quotient("0"), remainder = *this;
bigint top = bigint("1"), right = that;
while ( right <= *this ){
egyptstack.push(bigpair(top,right));
top = top.mul_by_2();
right = right.mul_by_2();
}
while ( !egyptstack.empty() ){
bigpair pair = popstack(egyptstack);
if ( pair.second <= remainder ){
remainder = remainder - pair.second;
quotient = quotient + pair.first;
}
}
return bigpair (quotient, remainder);
}
bigint bigint::operator/ (const bigint &that) const {
return div_rem (that).first;
}
bigint bigint::operator% (const bigint &that) const {
return div_rem (that).second;
}
#define TRACE_POW TRACE ('^', "result: " << result \
<< ", base: " << base << ", expt: " << expt);
bigint bigint::pow (const bigint &that) const {
bigint base = *this;
if (that > 999) throw range_error ("exp too big");
int expt = that.smallint();
bigint result = 1;
TRACE_POW;
if (expt < 0) {
base = 1 / base;
expt = - expt;
}
while (expt > 0) {
TRACE_POW;
if (expt & 1) { //odd
result = result * base;
--expt;
}else { //even
base = base * base;
expt /= 2;
}
}
TRACE_POW;
return result;
}
//
// Macros can make repetitive code easier.
//
#define COMPARE(OPER) \
bool bigint::operator OPER (const bigint &that) const { \
return compare (that) OPER 0; \
}
COMPARE (==)
COMPARE (!=)
COMPARE (< )
COMPARE (<=)
COMPARE (> )
COMPARE (>=)
#define INT_LEFT(RESULT,OPER) \
RESULT operator OPER (int left, const bigint &that) { \
return bigint (left) OPER that; \
}
INT_LEFT (bigint, +)
INT_LEFT (bigint, -)
INT_LEFT (bigint, *)
INT_LEFT (bigint, /)
INT_LEFT (bigint, %)
INT_LEFT (bool, ==)
INT_LEFT (bool, !=)
INT_LEFT (bool, < )
INT_LEFT (bool, <=)
INT_LEFT (bool, > )
INT_LEFT (bool, >=)
ostream &operator<< (ostream &out, const bigint &that) {
bigint::bigvalue_t::reverse_iterator itor = that.big_value->rbegin();
bigint::bigvalue_t::const_reverse_iterator end =
that.big_value->rend();
if ( that.negative == true ) out << "_";
if ( itor != end ){
for (int i = 1; itor != end; ++itor, ++i ){
out << char(*itor+'0');
if ( i == 69 ){
out << "\\\n";
i=0;
}
}
} else
out << '0';
return out;
}