-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbigInt.cpp
1222 lines (1031 loc) · 27.1 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#ifndef BIG_INT_HPP
#define BIG_INT_HPP
class BigInt
{
private:
std::string value;
char sign;
public:
BigInt();
BigInt(const BigInt&);
BigInt(const long long&);
BigInt(const std::string&);
BigInt& operator=(const BigInt&);
BigInt& operator=(const long long&);
BigInt& operator=(const std::string&);
BigInt operator+() const;
BigInt operator-() const;
BigInt operator+(const BigInt&) const;
BigInt operator-(const BigInt&) const;
BigInt operator*(const BigInt&) const;
BigInt operator/(const BigInt&) const;
BigInt operator%(const BigInt&) const;
BigInt operator+(const long long&) const;
BigInt operator-(const long long&) const;
BigInt operator*(const long long&) const;
BigInt operator/(const long long&) const;
BigInt operator%(const long long&) const;
BigInt operator+(const std::string&) const;
BigInt operator-(const std::string&) const;
BigInt operator*(const std::string&) const;
BigInt operator/(const std::string&) const;
BigInt operator%(const std::string&) const;
BigInt& operator+=(const BigInt&);
BigInt& operator-=(const BigInt&);
BigInt& operator*=(const BigInt&);
BigInt& operator/=(const BigInt&);
BigInt& operator%=(const BigInt&);
BigInt& operator+=(const long long&);
BigInt& operator-=(const long long&);
BigInt& operator*=(const long long&);
BigInt& operator/=(const long long&);
BigInt& operator%=(const long long&);
BigInt& operator+=(const std::string&);
BigInt& operator-=(const std::string&);
BigInt& operator*=(const std::string&);
BigInt& operator/=(const std::string&);
BigInt& operator%=(const std::string&);
BigInt& operator++();
BigInt& operator--();
BigInt operator++(int);
BigInt operator--(int);
bool operator<(const BigInt&) const;
bool operator>(const BigInt&) const;
bool operator<=(const BigInt&) const;
bool operator>=(const BigInt&) const;
bool operator==(const BigInt&) const;
bool operator!=(const BigInt&) const;
bool operator<(const long long&) const;
bool operator>(const long long&) const;
bool operator<=(const long long&) const;
bool operator>=(const long long&) const;
bool operator==(const long long&) const;
bool operator!=(const long long&) const;
bool operator<(const std::string&) const;
bool operator>(const std::string&) const;
bool operator<=(const std::string&) const;
bool operator>=(const std::string&) const;
bool operator==(const std::string&) const;
bool operator!=(const std::string&) const;
friend std::istream& operator>>(std::istream&, BigInt&);
friend std::ostream& operator<<(std::ostream&, const BigInt&);
std::string to_string() const;
int to_int() const;
long to_long() const;
long long to_long_long() const;
friend BigInt big_random(size_t);
};
#endif // BIG_INT_HPP
#ifndef BIG_INT_UTILITY_FUNCTIONS_HPP
#define BIG_INT_UTILITY_FUNCTIONS_HPP
#include <tuple>
bool is_valid_number(const std::string& num)
{
for (char digit : num) {
if (digit < '0' or digit > '9') {
return false;
}
}
return true;
}
void strip_leading_zeroes(std::string& num)
{
size_t i;
for (i = 0; i < num.size(); i++) {
if (num[i] != '0') {
break;
}
}
if (i == num.size()) {
num = "0";
} else {
num = num.substr(i);
}
}
void add_leading_zeroes(std::string& num, size_t num_zeroes)
{
num = std::string(num_zeroes, '0') + num;
}
void add_trailing_zeroes(std::string& num, size_t num_zeroes)
{
num += std::string(num_zeroes, '0');
}
std::tuple<std::string, std::string> get_larger_and_smaller(const std::string& num1, const std::string& num2)
{
std::string larger, smaller;
if (num1.size() > num2.size() or (num1.size() == num2.size() and num1 > num2)) {
larger = num1;
smaller = num2;
} else {
larger = num2;
smaller = num1;
}
add_leading_zeroes(smaller, larger.size() - smaller.size());
return std::make_tuple(larger, smaller);
}
bool is_power_of_10(const std::string& num)
{
if (num[0] != '1') {
return false;
}
for (size_t i = 1; i < num.size(); i++) {
if (num[i] != '0') {
return false;
}
}
return true; // first digit is 1 and the following digits are all 0
}
#endif // BIG_INT_UTILITY_FUNCTIONS_HPP
#ifndef BIG_INT_CONSTRUCTORS_HPP
#define BIG_INT_CONSTRUCTORS_HPP
BigInt::BigInt()
{
value = "0";
sign = '+';
}
BigInt::BigInt(const BigInt& num)
{
value = num.value;
sign = num.sign;
}
BigInt::BigInt(const long long& num)
{
value = std::to_string(num);
if (num < 0) {
sign = '-';
value = value.substr(1);
} else {
sign = '+';
}
}
BigInt::BigInt(const std::string& num)
{
if (num[0] == '+' or num[0] == '-') {
std::string magnitude = num.substr(1);
if (is_valid_number(magnitude)) {
value = magnitude;
sign = num[0];
} else {
throw std::invalid_argument("Expected an integer, got \'" + num + "\'");
}
} else {
if (is_valid_number(num)) {
value = num;
sign = '+';
} else {
throw std::invalid_argument("Expected an integer, got \'" + num + "\'");
}
}
strip_leading_zeroes(value);
}
#endif // BIG_INT_CONSTRUCTORS_HPP
#ifndef BIG_INT_CONVERSION_FUNCTIONS_HPP
#define BIG_INT_CONVERSION_FUNCTIONS_HPP
std::string BigInt::to_string() const
{
return this->sign == '-' ? "-" + this->value : this->value;
}
int BigInt::to_int() const
{
return std::stoi(this->to_string());
}
long BigInt::to_long() const
{
return std::stol(this->to_string());
}
long long BigInt::to_long_long() const
{
return std::stoll(this->to_string());
}
#endif // BIG_INT_CONVERSION_FUNCTIONS_HPP
#ifndef BIG_INT_ASSIGNMENT_OPERATORS_HPP
#define BIG_INT_ASSIGNMENT_OPERATORS_HPP
BigInt& BigInt::operator=(const BigInt& num)
{
value = num.value;
sign = num.sign;
return *this;
}
BigInt& BigInt::operator=(const long long& num)
{
BigInt temp(num);
value = temp.value;
sign = temp.sign;
return *this;
}
BigInt& BigInt::operator=(const std::string& num)
{
BigInt temp(num);
value = temp.value;
sign = temp.sign;
return *this;
}
#endif // BIG_INT_ASSIGNMENT_OPERATORS_HPP
#ifndef BIG_INT_UNARY_ARITHMETIC_OPERATORS_HPP
#define BIG_INT_UNARY_ARITHMETIC_OPERATORS_HPP
BigInt BigInt::operator+() const
{
return *this;
}
BigInt BigInt::operator-() const
{
BigInt temp;
temp.value = value;
if (value != "0") {
if (sign == '+') {
temp.sign = '-';
} else {
temp.sign = '+';
}
}
return temp;
}
#endif // BIG_INT_UNARY_ARITHMETIC_OPERATORS_HPP
#ifndef BIG_INT_RELATIONAL_OPERATORS_HPP
#define BIG_INT_RELATIONAL_OPERATORS_HPP
bool BigInt::operator==(const BigInt& num) const
{
return (sign == num.sign) && (value == num.value);
}
bool BigInt::operator!=(const BigInt& num) const
{
return !(*this == num);
}
bool BigInt::operator<(const BigInt& num) const
{
if (sign == num.sign) {
if (sign == '+') {
if (value.length() == num.value.length()) {
return value < num.value;
} else {
return value.length() < num.value.length();
}
} else {
return -(*this) > -num;
}
} else {
return sign == '-';
}
}
bool BigInt::operator>(const BigInt& num) const
{
return !((*this < num) or (*this == num));
}
bool BigInt::operator<=(const BigInt& num) const
{
return (*this < num) or (*this == num);
}
bool BigInt::operator>=(const BigInt& num) const
{
return !(*this < num);
}
bool BigInt::operator==(const long long& num) const
{
return *this == BigInt(num);
}
bool operator==(const long long& lhs, const BigInt& rhs)
{
return BigInt(lhs) == rhs;
}
bool BigInt::operator!=(const long long& num) const {
return !(*this == BigInt(num));
}
bool operator!=(const long long& lhs, const BigInt& rhs)
{
return BigInt(lhs) != rhs;
}
bool BigInt::operator<(const long long& num) const
{
return *this < BigInt(num);
}
bool operator<(const long long& lhs, const BigInt& rhs)
{
return BigInt(lhs) < rhs;
}
bool BigInt::operator>(const long long& num) const
{
return *this > BigInt(num);
}
bool operator>(const long long& lhs, const BigInt& rhs)
{
return BigInt(lhs) > rhs;
}
bool BigInt::operator<=(const long long& num) const
{
return !(*this > BigInt(num));
}
bool operator<=(const long long& lhs, const BigInt& rhs)
{
return BigInt(lhs) <= rhs;
}
bool BigInt::operator>=(const long long& num) const
{
return !(*this < BigInt(num));
}
bool operator>=(const long long& lhs, const BigInt& rhs)
{
return BigInt(lhs) >= rhs;
}
bool BigInt::operator==(const std::string& num) const
{
return *this == BigInt(num);
}
bool operator==(const std::string& lhs, const BigInt& rhs)
{
return BigInt(lhs) == rhs;
}
bool BigInt::operator!=(const std::string& num) const
{
return !(*this == BigInt(num));
}
bool operator!=(const std::string& lhs, const BigInt& rhs)
{
return BigInt(lhs) != rhs;
}
bool BigInt::operator<(const std::string& num) const
{
return *this < BigInt(num);
}
bool operator<(const std::string& lhs, const BigInt& rhs)
{
return BigInt(lhs) < rhs;
}
bool BigInt::operator>(const std::string& num) const
{
return *this > BigInt(num);
}
bool operator>(const std::string& lhs, const BigInt& rhs)
{
return BigInt(lhs) > rhs;
}
bool BigInt::operator<=(const std::string& num) const
{
return !(*this > BigInt(num));
}
bool operator<=(const std::string& lhs, const BigInt& rhs)
{
return BigInt(lhs) <= rhs;
}
bool BigInt::operator>=(const std::string& num) const
{
return !(*this < BigInt(num));
}
bool operator>=(const std::string& lhs, const BigInt& rhs)
{
return BigInt(lhs) >= rhs;
}
#endif // BIG_INT_RELATIONAL_OPERATORS_HPP
#ifndef BIG_INT_MATH_FUNCTIONS_HPP
#define BIG_INT_MATH_FUNCTIONS_HPP
#include <string>
BigInt abs(const BigInt& num)
{
return num < 0 ? -num : num;
}
BigInt big_pow10(size_t exp)
{
return BigInt("1" + std::string(exp, '0'));
}
BigInt pow(const BigInt& base, int exp)
{
if (exp < 0) {
if (base == 0) {
throw std::logic_error("Cannot divide by zero");
}
return abs(base) == 1 ? base : 0;
}
if (exp == 0) {
if (base == 0) {
throw std::logic_error("Zero cannot be raised to zero");
}
return 1;
}
BigInt result = base, result_odd = 1;
while (exp > 1) {
if (exp & 1) {
result_odd *= result;
}
result *= result;
exp >>= 1;
}
return result * result_odd;
}
BigInt pow(const long long& base, int exp)
{
return pow(BigInt(base), exp);
}
BigInt pow(const std::string& base, int exp)
{
return pow(BigInt(base), exp);
}
BigInt sqrt(const BigInt& num)
{
if (num < 0) {
throw std::invalid_argument("Cannot compute square root of a negative integer");
}
if (num == 0) {
return BigInt(0);
} else if (num < 4) {
return BigInt(1);
} else if (num < 9) {
return BigInt(2);
} else if (num < 16) {
return BigInt(3);
}
BigInt sqrt_prev = 0;
BigInt sqrt_current = big_pow10(num.to_string().size() / 2 - 1);
while (abs(sqrt_current - sqrt_prev) > 1) {
sqrt_prev = sqrt_current;
sqrt_current = (num / sqrt_prev + sqrt_prev) / 2;
}
return sqrt_current;
}
BigInt gcd(const BigInt& num1, const BigInt& num2)
{
BigInt abs_num1 = abs(num1);
BigInt abs_num2 = abs(num2);
if (abs_num2 == 0) {
return abs_num1;
}
if (abs_num1 == 0) {
return abs_num2;
}
BigInt remainder = abs_num2;
while (remainder != 0) {
remainder = abs_num1 % abs_num2;
abs_num1 = abs_num2;
abs_num2 = remainder;
}
return abs_num1;
}
BigInt gcd(const BigInt& num1, const long long& num2)
{
return gcd(num1, BigInt(num2));
}
BigInt gcd(const BigInt& num1, const std::string& num2)
{
return gcd(num1, BigInt(num2));
}
BigInt gcd(const long long& num1, const BigInt& num2)
{
return gcd(BigInt(num1), num2);
}
BigInt gcd(const std::string& num1, const BigInt& num2)
{
return gcd(BigInt(num1), num2);
}
BigInt lcm(const BigInt& num1, const BigInt& num2)
{
if (num1 == 0 or num2 == 0)
return 0;
return abs(num1 * num2) / gcd(num1, num2);
}
BigInt lcm(const BigInt& num1, const long long& num2)
{
return lcm(num1, BigInt(num2));
}
BigInt lcm(const BigInt& num1, const std::string& num2)
{
return lcm(num1, BigInt(num2));
}
BigInt lcm(const long long& num1, const BigInt& num2)
{
return lcm(BigInt(num1), num2);
}
BigInt lcm(const std::string& num1, const BigInt& num2)
{
return lcm(BigInt(num1), num2);
}
#endif // BIG_INT_MATH_FUNCTIONS_HPP
#ifndef BIG_INT_BINARY_ARITHMETIC_OPERATORS_HPP
#define BIG_INT_BINARY_ARITHMETIC_OPERATORS_HPP
#include <climits>
#include <cmath>
#include <string>
const long long FLOOR_SQRT_LLONG_MAX = 3037000499;
BigInt BigInt::operator+(const BigInt& num) const
{
if (this->sign == '+' && num.sign == '-') {
BigInt rhs = num;
rhs.sign = '+';
return *this - rhs;
} else if (this->sign == '-' && num.sign == '+') {
BigInt lhs = *this;
lhs.sign = '+';
return -(lhs - num);
}
std::string larger, smaller;
std::tie(larger, smaller) = get_larger_and_smaller(this->value, num.value);
BigInt result;
result.value = "";
short carry = 0, sum;
// add the two values
for (long i = larger.size() - 1; i >= 0; i--) {
sum = larger[i] - '0' + smaller[i] - '0' + carry;
result.value = std::to_string(sum % 10) + result.value;
carry = sum / (short) 10;
}
if (carry) {
result.value = std::to_string(carry) + result.value;
}
if (this->sign == '-' and result.value != "0") {
result.sign = '-';
}
return result;
}
BigInt BigInt::operator-(const BigInt& num) const
{
if (this->sign == '+' && num.sign == '-') {
BigInt rhs = num;
rhs.sign = '+';
return *this + rhs;
} else if (this->sign == '-' && num.sign == '+') {
BigInt lhs = *this;
lhs.sign = '+';
return -(lhs + num);
}
BigInt result;
std::string larger, smaller;
if (abs(*this) > abs(num)) {
larger = this->value;
smaller = num.value;
if (this->sign == '-') {
result.sign = '-';
}
} else {
larger = num.value;
smaller = this->value;
if (num.sign == '+') {
result.sign = '-';
}
}
add_leading_zeroes(smaller, larger.size() - smaller.size());
result.value = "";
short diff;
long i, j;
for (i = larger.size() - 1; i >= 0; i--) {
diff = larger[i] - smaller[i];
if (diff < 0) {
for (j = i-1; j >= 0; j--) {
if (larger[j] != '0') {
larger[j]--;
break;
}
}
j++;
while (j != i) {
larger[j] = '9';
j++;
}
diff += 10;
}
result.value = std::to_string(diff) + result.value;
}
strip_leading_zeroes(result.value);
if (result.value == "0") {
result.sign = '+';
}
return result;
}
BigInt BigInt::operator*(const BigInt& num) const
{
if (*this == 0 || num == 0) {
return BigInt(0);
}
if (*this == 1) {
return num;
}
if (num == 1) {
return *this;
}
BigInt product;
if (abs(*this) <= FLOOR_SQRT_LLONG_MAX && abs(num) <= FLOOR_SQRT_LLONG_MAX) {
product = std::stoll(this->value) * std::stoll(num.value);
} else {
std::string larger, smaller;
std::tie(larger, smaller) = get_larger_and_smaller(this->value, num.value);
size_t half_length = larger.size() / 2;
auto half_length_ceil = (size_t)ceil(larger.size() / 2.0);
BigInt num1_high, num1_low;
num1_high = larger.substr(0, half_length);
num1_low = larger.substr(half_length);
BigInt num2_high, num2_low;
num2_high = smaller.substr(0, half_length);
num2_low = smaller.substr(half_length);
strip_leading_zeroes(num1_high.value);
strip_leading_zeroes(num1_low.value);
strip_leading_zeroes(num2_high.value);
strip_leading_zeroes(num2_low.value);
BigInt prod_high, prod_mid, prod_low;
prod_high = num1_high * num2_high;
prod_low = num1_low * num2_low;
prod_mid = (num1_high + num1_low) * (num2_high + num2_low) - prod_high - prod_low;
add_trailing_zeroes(prod_high.value, 2 * half_length_ceil);
add_trailing_zeroes(prod_mid.value, half_length_ceil);
strip_leading_zeroes(prod_high.value);
strip_leading_zeroes(prod_mid.value);
strip_leading_zeroes(prod_low.value);
product = prod_high + prod_mid + prod_low;
}
strip_leading_zeroes(product.value);
if (this->sign == num.sign) {
product.sign = '+';
} else {
product.sign = '-';
}
return product;
}
std::tuple<BigInt, BigInt> divide(const BigInt& dividend, const BigInt& divisor)
{
BigInt quotient, remainder, temp;
temp = divisor;
quotient = 1;
while (temp < dividend) {
quotient++;
temp += divisor;
}
if (temp > dividend) {
quotient--;
remainder = dividend - (temp - divisor);
}
return std::make_tuple(quotient, remainder);
}
BigInt BigInt::operator/(const BigInt& num) const
{
BigInt abs_dividend = abs(*this);
BigInt abs_divisor = abs(num);
if (num == 0) {
throw std::logic_error("Attempted division by zero");
}
if (abs_dividend < abs_divisor) {
return BigInt(0);
}
if (num == 1) {
return *this;
}
if (num == -1) {
return -(*this);
}
BigInt quotient;
if (abs_dividend <= LLONG_MAX && abs_divisor <= LLONG_MAX) {
quotient = std::stoll(abs_dividend.value) / std::stoll(abs_divisor.value);
} else if (abs_dividend == abs_divisor) {
quotient = 1;
} else {
quotient.value = "";
BigInt chunk, chunk_quotient, chunk_remainder;
size_t chunk_index = 0;
chunk_remainder.value = abs_dividend.value.substr(chunk_index, abs_divisor.value.size() - 1);
chunk_index = abs_divisor.value.size() - 1;
while (chunk_index < abs_dividend.value.size()) {
chunk.value = chunk_remainder.value.append(1, abs_dividend.value[chunk_index]);
chunk_index++;
while (chunk < abs_divisor) {
quotient.value += "0";
if (chunk_index < abs_dividend.value.size()) {
chunk.value.append(1, abs_dividend.value[chunk_index]);
chunk_index++;
} else {
break;
}
}
if (chunk == abs_divisor) {
quotient.value += "1";
chunk_remainder = 0;
} else if (chunk > abs_divisor) {
strip_leading_zeroes(chunk.value);
std::tie(chunk_quotient, chunk_remainder) = divide(chunk, abs_divisor);
quotient.value += chunk_quotient.value;
}
}
}
strip_leading_zeroes(quotient.value);
if (this->sign == num.sign) {
quotient.sign = '+';
} else {
quotient.sign = '-';
}
return quotient;
}
BigInt BigInt::operator%(const BigInt& num) const
{
BigInt abs_dividend = abs(*this);
BigInt abs_divisor = abs(num);
if (abs_divisor == 0) {
throw std::logic_error("Attempted division by zero");
}
if (abs_divisor == 1 || abs_divisor == abs_dividend) {
return BigInt(0);
}
BigInt remainder;
if (abs_dividend <= LLONG_MAX && abs_divisor <= LLONG_MAX) {
remainder = std::stoll(abs_dividend.value) % std::stoll(abs_divisor.value);
} else if (abs_dividend < abs_divisor) {
remainder = abs_dividend;
} else {
BigInt quotient = abs_dividend / abs_divisor;
remainder = abs_dividend - quotient * abs_divisor;
}
strip_leading_zeroes(remainder.value);
remainder.sign = this->sign;
if (remainder.value == "0") {
remainder.sign = '+';
}
return remainder;
}
BigInt BigInt::operator+(const long long& num) const
{
return *this + BigInt(num);
}
BigInt operator+(const long long& lhs, const BigInt& rhs)
{
return BigInt(lhs) + rhs;
}
BigInt BigInt::operator-(const long long& num) const
{
return *this - BigInt(num);
}
BigInt operator-(const long long& lhs, const BigInt& rhs)
{
return BigInt(lhs) - rhs;
}
BigInt BigInt::operator*(const long long& num) const
{
return *this * BigInt(num);
}
BigInt operator*(const long long& lhs, const BigInt& rhs)
{
return BigInt(lhs) * rhs;
}
BigInt BigInt::operator/(const long long& num) const
{
return *this / BigInt(num);
}
BigInt operator/(const long long& lhs, const BigInt& rhs)
{
return BigInt(lhs) / rhs;
}
BigInt BigInt::operator%(const long long& num) const
{
return *this % BigInt(num);
}
BigInt operator%(const long long& lhs, const BigInt& rhs)
{
return BigInt(lhs) % rhs;
}
BigInt BigInt::operator+(const std::string& num) const
{
return *this + BigInt(num);
}
BigInt operator+(const std::string& lhs, const BigInt& rhs)
{
return BigInt(lhs) + rhs;
}
BigInt BigInt::operator-(const std::string& num) const
{
return *this - BigInt(num);
}
BigInt operator-(const std::string& lhs, const BigInt& rhs)
{
return BigInt(lhs) - rhs;
}
BigInt BigInt::operator*(const std::string& num) const
{
return *this * BigInt(num);
}
BigInt operator*(const std::string& lhs, const BigInt& rhs)
{
return BigInt(lhs) * rhs;
}
BigInt BigInt::operator/(const std::string& num) const
{
return *this / BigInt(num);
}
BigInt operator/(const std::string& lhs, const BigInt& rhs)
{
return BigInt(lhs) / rhs;
}
BigInt BigInt::operator%(const std::string& num) const
{
return *this % BigInt(num);
}
BigInt operator%(const std::string& lhs, const BigInt& rhs)
{
return BigInt(lhs) % rhs;
}
#endif // BIG_INT_BINARY_ARITHMETIC_OPERATORS_HPP
#ifndef BIG_INT_ARITHMETIC_ASSIGNMENT_OPERATORS_HPP
#define BIG_INT_ARITHMETIC_ASSIGNMENT_OPERATORS_HPP
BigInt& BigInt::operator+=(const BigInt& num)
{
*this = *this + num;
return *this;
}
BigInt& BigInt::operator-=(const BigInt& num)
{
*this = *this - num;
return *this;
}
BigInt& BigInt::operator*=(const BigInt& num)
{
*this = *this * num;
return *this;
}
BigInt& BigInt::operator/=(const BigInt& num)
{
*this = *this / num;
return *this;
}
BigInt& BigInt::operator%=(const BigInt& num)
{
*this = *this % num;
return *this;
}
BigInt& BigInt::operator+=(const long long& num)
{
*this = *this + BigInt(num);