-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigint.h
1322 lines (1191 loc) · 45.4 KB
/
bigint.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
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
//SOMA UDAY KIRAN
//VELLORE INSTITUTE OF TECHNOLOGY
#pragma once
#include <string>
#include <sstream>
#include <vector>
#include <cmath>
#include <limits>
// Macros for Programmer(User) use cases.
#define big_abs bigint::_big_abs
#define big_max bigint::_big_max
#define big_min bigint::_big_min
#define big_pow bigint::_big_pow
#define big_sqrt bigint::_big_sqrt
#define big_log2 bigint::_big_log2
#define big_log10 bigint::_big_log10
#define big_logwithbase bigint::_big_logwithbase
#define big_antilog2 bigint::_big_antilog2
#define big_antilog10 bigint::_big_antilog10
#define big_swap bigint::_big_swap
#define big_reverse bigint::_big_reverse
#define big_gcd bigint::_big_gcd
#define big_lcm bigint::_big_lcm
#define big_fact bigint::_big_fact
#define big_isPrime bigint::_big_isPrime
#define big_isPalindrome bigint::_big_isPalindrome
#define to_bigint bigint::_to_bigint
// Big Integer Class
class bigint {
private:
std::string str; // only data memeber for strong Big Integer as String. [For signed int, str[0] ='-']
// Function Definitions for Internal Uses
static std::string trim(std::string);
static std::string add(std::string, std::string);
static std::string subtract(std::string, std::string);
static std::string multiply(std::string, std::string);
static std::string divide(std::string, std::string);
static std::string mod(std::string, std::string);
static std::string shortDivide(std::string, unsigned long long int);
static std::string maximum(std::string, std::string);
static bool is_maximum(std::string, std::string);
static bool is_strictlyMaximum(std::string, std::string);
static std::string minimum(std::string, std::string);
static bool is_minimum(std::string, std::string);
static bool is_strictlyMinimum(std::string, std::string);
static bool is_bigint(std::string);
// Public Property
static std::string abs(std::string);
static std::string pow(std::string, std::string);
static std::string sqrt(std::string);
static std::string log2(std::string);
static std::string log10(std::string);
static std::string logwithbase(std::string, std::string);
static std::string antilog2(std::string);
static std::string antilog10(std::string);
static void swap(std::string&, std::string&);
static std::string reverse(std::string);
static std::string gcd(std::string, std::string);
static std::string lcm(std::string, std::string);
static std::string fact(std::string);
static bool isPalindrome(std::string);
static bool isPrime(std::string);
public:
// Constructors for big int.
bigint() {
str = '0'; //default value
}
bigint(std::string s) {
if(!is_bigint(s))
throw std::runtime_error("Invalid Big Integer has been fed."); // if the input string is not valid number.
str = s;
}
bigint(long long int n) {
str = std::to_string(n);
}
bigint(int n) {
str = std::to_string(n);
}
bigint(long int n) {
str = std::to_string(n);
}
bigint(const bigint &n) {
str = n.str;
}
// operator overloading for output stream {<<}
friend std::ostream &operator << (std::ostream& stream, const bigint &n) {
stream << n.str;
return stream;
}
// operator overloading for input stream {>>}
friend std::istream &operator >> (std::istream& stream, bigint &n) {
stream >> n.str;
return stream;
}
/* Operator {+} Overloadings, for different kind of
parameter for the programmer's convinience */
bigint operator + (bigint const &n) {
bigint ans;
ans.str = add(str, n.str);
return ans;
}
friend bigint operator + (bigint const &n1, int n2) {
bigint ans;
ans.str = add(n1.str, std::to_string(n2));
return ans;
}
friend bigint operator + (int n1, bigint const &n2) {
bigint ans;
ans.str = add(n2.str, std::to_string(n1));
return ans;
}
friend bigint operator + (bigint const &n1, long int n2) {
bigint ans;
ans.str = add(n1.str, std::to_string(n2));
return ans;
}
friend bigint operator + (long int n1, bigint const &n2) {
bigint ans;
ans.str = add(n2.str, std::to_string(n1));
return ans;
}
friend bigint operator + (bigint const &n1, long long int n2) {
bigint ans;
ans.str = add(n1.str, std::to_string(n2));
return ans;
}
friend bigint operator + (long long int n1, bigint const &n2) {
bigint ans;
ans.str = add(n2.str, std::to_string(n1));
return ans;
}
// Extra shortcut feature
bigint& operator += (bigint const n) {
(*this).str = add((*this).str, n.str);
return (*this);
}
/* Operator {-} Overloadings, for different kind of
parameter for the programmer's convinience */
bigint operator - (bigint const &n) {
bigint ans;
ans.str = subtract(str, n.str);
return ans;
}
friend bigint operator - (bigint const &n1, int n2) {
bigint ans;
ans.str = subtract(n1.str, std::to_string(n2));
return ans;
}
friend bigint operator - (int n1, bigint const &n2) {
bigint ans;
ans.str = subtract(std::to_string(n1), n2.str);
return ans;
}
friend bigint operator - (bigint const &n1, long int n2) {
bigint ans;
ans.str = subtract(n1.str, std::to_string(n2));
return ans;
}
friend bigint operator - (long int n1, bigint const &n2) {
bigint ans;
ans.str = subtract(std::to_string(n1), n2.str);
return ans;
}
friend bigint operator - (bigint const &n1, long long int n2) {
bigint ans;
ans.str = subtract(n1.str, std::to_string(n2));
return ans;
}
friend bigint operator - (long long int n1, bigint const &n2) {
bigint ans;
ans.str = subtract(std::to_string(n1), n2.str);
return ans;
}
// Extra shortcut feature
bigint& operator -= (bigint const n) {
(*this).str = subtract((*this).str, n.str);
return (*this);
}
/* Operator {*} Overloadings, for different kind of
parameter for the programmer's convinience */
bigint operator * (bigint const &n) {
bigint ans;
ans.str = multiply(str, n.str);
return ans;
}
friend bigint operator * (bigint const &n1, int n2) {
bigint ans;
ans.str = multiply(n1.str, std::to_string(n2));
return ans;
}
friend bigint operator * (int n1, bigint const &n2) {
bigint ans;
ans.str = multiply(std::to_string(n1), n2.str);
return ans;
}
friend bigint operator * (bigint const &n1, long int n2) {
bigint ans;
ans.str = multiply(n1.str, std::to_string(n2));
return ans;
}
friend bigint operator * (long int n1, bigint const &n2) {
bigint ans;
ans.str = multiply(std::to_string(n1), n2.str);
return ans;
}
friend bigint operator * (bigint const &n1, long long int n2) {
bigint ans;
ans.str = multiply(n1.str, std::to_string(n2));
return ans;
}
friend bigint operator * (long long int n1, bigint const &n2) {
bigint ans;
ans.str = multiply(std::to_string(n1), n2.str);
return ans;
}
// Extra shortcut feature
bigint& operator *= (bigint const n) {
(*this).str = multiply((*this).str, n.str);
return (*this);
}
/* Operator {/} Overloadings, for different kind of
parameter for the programmer's convinience */
bigint operator / (bigint const &n) {
bigint ans;
ans.str = divide(str, n.str);
return ans;
}
friend bigint operator / (bigint const &n1, int n2) {
bigint ans;
ans.str = divide(n1.str, std::to_string(n2));
return ans;
}
friend bigint operator / (int n1, bigint const &n2) {
bigint ans;
ans.str = divide(std::to_string(n1), n2.str);
return ans;
}
friend bigint operator / (bigint const &n1, long int n2) {
bigint ans;
ans.str = divide(n1.str, std::to_string(n2));
return ans;
}
friend bigint operator / (long int n1, bigint const &n2) {
bigint ans;
ans.str = divide(std::to_string(n1), n2.str);
return ans;
}
friend bigint operator / (bigint const &n1, long long int n2) {
bigint ans;
ans.str = divide(n1.str, std::to_string(n2));
return ans;
}
friend bigint operator / (long long int n1, bigint const &n2) {
bigint ans;
ans.str = divide(std::to_string(n1), n2.str);
return ans;
}
// Extra shortcut feature
bigint& operator /= (bigint const n) {
(*this).str = divide((*this).str, n.str);
return (*this);
}
/* Operator {%} Overloadings, for different kind of
parameter for the programmer's convinience */
bigint operator % (bigint const &n) {
bigint ans;
ans.str = mod(str, n.str);
return ans;
}
friend bigint operator % (bigint const &n1, int n2) {
bigint ans;
ans.str = mod(n1.str, std::to_string(n2));
return ans;
}
friend bigint operator % (int n1, bigint const &n2) {
bigint ans;
ans.str = mod(std::to_string(n1), n2.str);
return ans;
}
friend bigint operator % (bigint const &n1, long int n2) {
bigint ans;
ans.str = mod(n1.str, std::to_string(n2));
return ans;
}
friend bigint operator % (long int n1, bigint const &n2) {
bigint ans;
ans.str = mod(std::to_string(n1), n2.str);
return ans;
}
friend bigint operator % (bigint const &n1, long long int n2) {
bigint ans;
ans.str = mod(n1.str, std::to_string(n2));
return ans;
}
friend bigint operator % (long long int n1, bigint const &n2) {
bigint ans;
ans.str = mod(std::to_string(n1), n2.str);
return ans;
}
// Extra shortcut feature
bigint& operator %= (bigint const n) {
(*this).str = mod((*this).str, n.str);
return (*this);
}
//-------------------------------------------------
//------------Increment and Decrement--------------
//-------------------------------------------------
/* Operator {++} Overloadings
for the pre incremention */
bigint& operator ++ () {
(*this).str = add((*this).str, "1");
return (*this);
}
/* Operator {++} Overloadings
for the post incremention */
bigint operator ++ (int) {
(*this).str = add((*this).str, "1");
return (*this);
}
/* Operator {--} Overloadings
for the pre decremention */
bigint& operator -- () {
(*this).str = subtract((*this).str, "1");
return (*this);
}
/* Operator {--} Overloadings
for the post incremention */
bigint operator -- (int) {
(*this).str = subtract((*this).str, "1");
return (*this);
}
//-------------------------------------------------
//------------Conditional Operators----------------
//-------------------------------------------------
/* Operator {>} Overloadings, for different kind of
parameter for the programmer's convinience */
bool operator > (bigint const &n) {
return is_strictlyMaximum(str, n.str);
}
friend bool operator > (bigint const &n1, int n2) {
return is_strictlyMaximum(n1.str, std::to_string(n2));
}
friend bool operator > (int n1, bigint const &n2) {
return is_strictlyMaximum(std::to_string(n1), n2.str);
}
friend bool operator > (bigint const &n1, long int n2) {
return is_strictlyMaximum(n1.str, std::to_string(n2));
}
friend bool operator > (long int n1, bigint const &n2) {
return is_strictlyMaximum(std::to_string(n1), n2.str);
}
friend bool operator > (bigint const &n1, long long int n2) {
return is_strictlyMaximum(n1.str, std::to_string(n2));
}
friend bool operator > (long long int n1, bigint const &n2) {
return is_strictlyMaximum(std::to_string(n1), n2.str);
}
/* Operator {<} Overloadings, for different kind of
parameter for the programmer's convinience */
bool operator < (bigint const &n) {
return is_strictlyMinimum(str, n.str);
}
friend bool operator < (bigint const &n1, int n2) {
return is_strictlyMinimum(n1.str, std::to_string(n2));
}
friend bool operator < (int n1, bigint const &n2) {
return is_strictlyMinimum(std::to_string(n1), n2.str);
}
friend bool operator < (bigint const &n1, long int n2) {
return is_strictlyMinimum(n1.str, std::to_string(n2));
}
friend bool operator < (long int n1, bigint const &n2) {
return is_strictlyMinimum(std::to_string(n1), n2.str);
}
friend bool operator < (bigint const &n1, long long int n2) {
return is_strictlyMinimum(n1.str, std::to_string(n2));
}
friend bool operator < (long long int n1, bigint const &n2) {
return is_strictlyMinimum(std::to_string(n1), n2.str);
}
/* Operator {>=} Overloadings, for different kind of
parameter for the programmer's convinience */
bool operator >= (bigint const &n) {
return is_maximum(str, n.str);
}
friend bool operator >= (bigint const &n1, int n2) {
return is_maximum(n1.str, std::to_string(n2));
}
friend bool operator >= (int n1, bigint const &n2) {
return is_maximum(std::to_string(n1), n2.str);
}
friend bool operator >= (bigint const &n1, long int n2) {
return is_maximum(n1.str, std::to_string(n2));
}
friend bool operator >= (long int n1, bigint const &n2) {
return is_maximum(std::to_string(n1), n2.str);
}
friend bool operator >= (bigint const &n1, long long int n2) {
return is_maximum(n1.str, std::to_string(n2));
}
friend bool operator >= (long long int n1, bigint const &n2) {
return is_maximum(std::to_string(n1), n2.str);
}
/* Operator {<=} Overloadings, for different kind of
parameter for the programmer's convinience */
bool operator <= (bigint const &n) {
return is_minimum(str, n.str);
}
friend bool operator <= (bigint const &n1, int n2) {
return is_minimum(n1.str, std::to_string(n2));
}
friend bool operator <= (int n1, bigint const &n2) {
return is_minimum(std::to_string(n1), n2.str);
}
friend bool operator <= (bigint const &n1, long int n2) {
return is_minimum(n1.str, std::to_string(n2));
}
friend bool operator <= (long int n1, bigint const &n2) {
return is_minimum(std::to_string(n1), n2.str);
}
friend bool operator <= (bigint const &n1, long long int n2) {
return is_minimum(n1.str, std::to_string(n2));
}
friend bool operator <= (long long int n1, bigint const &n2) {
return is_minimum(std::to_string(n1), n2.str);
}
/* Operator {==} Overloadings, for different kind of
parameter for the programmer's convinience */
bool operator ==(bigint const &n) {
return (*this).str == n.str;
}
friend bool operator == (bigint const &n1, int n2) {
return n1.str == std::to_string(n2);
}
friend bool operator == (int n1, bigint const &n2) {
return std::to_string(n1) == n2.str;
}
friend bool operator == (bigint const &n1, long int n2) {
return n1.str == std::to_string(n2);
}
friend bool operator == (long int n1, bigint const &n2) {
return std::to_string(n1) == n2.str;
}
friend bool operator == (bigint const &n1, long long int n2) {
return n1.str == std::to_string(n2);
}
friend bool operator == (long long int n1, bigint const &n2) {
return std::to_string(n1) == n2.str;
}
/* Operator {!=} Overloadings, for different kind of
parameter for the programmer's convinience */
bool operator !=(bigint const &n) {
return (*this).str != n.str;
}
friend bool operator != (bigint const &n1, int n2) {
return n1.str != std::to_string(n2);
}
friend bool operator != (int n1, bigint const &n2) {
return std::to_string(n1) != n2.str;
}
friend bool operator != (bigint const &n1, long int n2) {
return n1.str != std::to_string(n2);
}
friend bool operator != (long int n1, bigint const &n2) {
return std::to_string(n1) != n2.str;
}
friend bool operator != (bigint const &n1, long long int n2) {
return n1.str != std::to_string(n2);
}
friend bool operator != (long long int n1, bigint const &n2) {
return std::to_string(n1) != n2.str;
}
//-----------------------------------------------------------
//--------Function Definitions for External Uses-------------
//-----------------------------------------------------------
static bigint _big_max(bigint &a, bigint &b) { // returns the maximum value between two Big Integers.
bigint ans;
ans.str = maximum(a.str, b.str);
return ans;
}
static bigint _big_min(bigint &a, bigint &b) { // returns the minimum value between two Big Integers.
bigint ans;
ans.str = minimum(a.str, b.str);
return ans;
}
static bigint _big_abs(bigint &a) { // returns the absolute value of Big Integer.
bigint ans;
ans.str = abs(a.str);
return ans;
}
static bigint _big_pow(bigint &a, bigint &b) { // returns the power value between two Big Integers i.e., a^b, ^ -> power
bigint ans;
ans.str = pow(a.str, b.str);
return ans;
}
static bigint _big_sqrt(bigint &a) { // returns the square root value of Big Integer.
bigint ans;
ans.str = sqrt(a.str);
return ans;
}
static bigint _big_log2(bigint &a) { // returns the log of Big Integer to the base of 2.
bigint ans;
ans.str = log2(a.str);
return ans;
}
static bigint _big_log10(bigint &a) { // returns the log of Big Integer to the base of 10.
bigint ans;
ans.str = log10(a.str);
return ans;
}
static bigint _big_logwithbase(bigint &a, bigint &b) { // returns the log of Big Integer(a) to the base of (b).
bigint ans;
ans.str = logwithbase(a.str, b.str);
return ans;
}
static bigint _big_antilog2(bigint &a) { // returns the antilog of Big Integer to the base of 2.
bigint ans;
ans.str = antilog2(a.str);
return ans;
}
static bigint _big_antilog10(bigint &a) { // returns the antilog of Big Integer to the base of 10.
bigint ans;
ans.str = antilog10(a.str);
return ans;
}
static void _big_swap(bigint &a, bigint &b) { // swaps the two Big Integers.
swap(a.str, b.str);
}
static bigint _big_reverse(bigint &a) { //Reverses the Big Integer.
bigint ans;
ans.str = reverse(a.str);
return ans;
}
static bigint _big_gcd(bigint &a, bigint &b) { // returns the gcd of Big Integers a and b.
bigint ans;
ans.str = gcd(a.str, b.str);
return ans;
}
static bigint _big_lcm(bigint &a, bigint &b) { // returns the lcm of Big Integers a and b.
bigint ans;
ans.str = lcm(a.str, b.str);
return ans;
}
static bigint _big_fact(bigint &a) { // returns the factorial of Big Integer.
bigint ans;
ans.str = fact(a.str);
return ans;
}
static bool _big_isPalindrome(bigint &a) { // Check if the Big Integer is Palindromic Integer.
return isPalindrome(a.str);
}
static bool _big_isPrime(bigint &a) { // Check if the Big Integer is Prime Integer.
return isPrime(a.str);
}
// to biginteger functions
static bigint _to_bigint(std::string s) {
bigint ans;
ans.str = s;
return ans;
}
static bigint _to_bigint(int n) {
bigint ans;
ans.str = std::to_string(n);
return ans;
}
static bigint _to_bigint(long int n) {
bigint ans;
ans.str = std::to_string(n);
return ans;
}
static bigint _to_bigint(long long int n) {
bigint ans;
ans.str = std::to_string(n);
return ans;
}
};
bool bigint::is_bigint(std::string s) { // Checks if the feeded integer is valid Number or not.
if(s[0] == '-')
s.erase(0, 1);
for(unsigned long long int i = 0; i < s.length(); ++i) {
if(!((int)s[i] > 47 && (int)s[i] < 58))
return false;
}
return true;
}
//---------------------------------------------------------
//--------Internal Arithmetic functions Definitions--------
//---------------------------------------------------------
/*
______________________________________________________
Addition Function
______________________________________________________
Algorithm Explanation
Simple traversal from right to left in the longest string and adding each character
from each of both strings as integer and keeping track of carry and storing the result
into new String(sum). If the numbers are negative in either of the Strings provided
the respective Subtract function is called.
Algorithmic Time Complexity : O(max(n, m))
Algorithmic Space Complexity : O(max(n, m))
where n ans m are the lengths of respective strings(Integers) provided.
*/
std::string bigint::add(std::string str1, std::string str2) { // returns arithmetic addition of str1+str2
int str1_len = str1.length();
int str2_len = str2.length();
std::string sum = "";
if(str1_len == 0 && str2_len == 0) {
sum = "0";
} else if(str1[0] == '-' && str2[0] == '-') {
if(str1_len == 1 && str2_len == 1) {
sum = "0";
} else {
sum = "-" + add(str1.erase(0, 1), str2.erase(0, 1));
}
} else if(str1[0] == '-') {
sum = subtract(str2, str1.erase(0, 1));
} else if(str2[0] == '-') {
sum = subtract(str1, str2.erase(0, 1));
} else {
int i, j, track_sum, carry = 0;
for(i = str1_len-1, j = str2_len-1; i >= 0 && j >= 0; --i, --j) {
track_sum = ((int)str1[i])-48 + ((int)str2[j])-48 + carry;
carry = track_sum/10;
sum = std::to_string(track_sum%10) + sum;
}
if(i >= 0 && j < 0) {
for(; i >= 0; --i) {
track_sum = ((int)str1[i])-48 + carry;
carry = track_sum/10;
sum = std::to_string(track_sum%10) + sum;
}
} else if(j >= 0 && i < 0) {
for(; j >= 0; --j) {
track_sum = ((int)str2[j])-48 + carry;
carry = track_sum/10;
sum = std::to_string(track_sum%10) + sum;
}
}
if(carry) {
sum = std::to_string(carry) + sum;
}
}
return trim(sum);
}
/*
______________________________________________________
Subtraction Function
______________________________________________________
Algorithm Explanation
Simple traversal from right to left in the longest string and subtracting each character
from second string from first string integer and keeping track of carry and storing the result
into new String(ans). If the both numbers are negative in the Strings provided the respective
Addition function is called.
Algorithmic Time Complexity : O(max(n, m))
Algorithmic Space Complexity : O(max(n, m))
where n ans m are the lengths of respective strings(Integers) provided.
*/
std::string bigint::subtract(std::string str1, std::string str2) { // returns arithmetic subtraction of str1-str2
int str1_len = str1.length();
int str2_len = str2.length();
std::string sum = "";
if(str1 == str2) {
return "0";
} else if(str1[0] == '-' && str2[0] == '-') {
if(str2_len == 1 && str1_len == 1) {
sum = "0";
} else {
bigint t1(str1.erase(0, 1));
bigint t2(str2.erase(0, 1));
std::string temp = subtract(t2.str, t1.str);
bigint mx = maximum(t2.str, t1.str);
if(temp[0] != '-' && mx.str == t1.str)
sum = "-" + temp;
else
sum = temp;
}
} else if(str1[0] == '-') {
sum = "-" + add(str1.erase(0, 1), str2);
} else if(str2[0] == '-') {
sum = add(str1, str2.erase(0, 1));
} else {
//? Implement Subtract
int i, j, track_sum, carry = 0;
if(str1_len < str2_len) {
std::string tp = str1;
str1 = str2;
str2 = tp;
sum = "-" + subtract(str1, str2);
return trim(sum);
} else if(str1_len == str2_len) {
bigint a(str1);
bigint b(str2);
bigint mx = maximum(a.str, b.str);
if(mx.str == str2) {
std::string tp = str1;
str1 = str2;
str2 = tp;
bigint t1(str1);
bigint t2(str2);
bigint temp = subtract(str1, str2);
sum = "-" + subtract(str1, str2);
return trim(sum);
}
}
int val1, val2;
for(i = str1_len-1, j = str2_len-1; i >= 0 || j >= 0; --i, --j) {
if(i >= 0) {
val1 = (int) str1[i] - 48;
} else {
val1 = 0;
}
if(j >= 0) {
val2 = (int) str2[j] - 48;
} else {
val2 = 0;
}
track_sum = val1 - val2 - carry;
if(track_sum < 0) {
track_sum += 10;
carry = 1;
} else {
carry = 0;
}
sum = std::to_string(track_sum) + sum;
}
}
return trim(sum);
}
/*
______________________________________________________
Multiplication Function
______________________________________________________
Algorithm Explanation
Like naive way of Multiplication. Traversing from the left of first string and multiplying
each character as integer to every character of second String as integer and tracking the
value of carry and adding to the last of each iterations.
Algorithmic Time Complexity : O(n*m)
Algorithmic Space Complexity : O(n + m)
where n ans m are the lengths of respective strings(Integers) provided.
*/
std::string bigint::multiply(std::string str1, std::string str2) { // return arithmetic multiplication of str1*str2
bool toAddNeg = false;
int str1_len = str1.length();
int str2_len = str2.length();
std::string ans = "";
if(str1[0] == '-' && str2[0] == '-') {
ans = multiply(str1.erase(0, 1), str2.erase(0, 1));
} else if(str1[0] == '-') {
toAddNeg = true;
ans = multiply(str1.erase(0, 1), str2);
} else if(str2[0] == '-') {
toAddNeg = true;
ans = multiply(str1, str2.erase(0, 1));
} else {
if (str1_len == 0 || str2_len == 0)
return "0";
std::vector<int> result(str1_len + str2_len, 0);
int i_n1 = 0;
int i_n2 = 0;
for (int i=str1_len-1; i>=0; i--) {
int carry = 0;
int n1 = str1[i] - '0';
i_n2 = 0;
for (int j=str2_len-1; j>=0; j--) {
int n2 = str2[j] - '0';
int sum = n1*n2 + result[i_n1 + i_n2] + carry;
carry = sum/10;
result[i_n1 + i_n2] = sum % 10;
i_n2++;
}
if (carry > 0)
result[i_n1 + i_n2] += carry;
i_n1++;
}
int i = result.size() - 1;
while (i>=0 && result[i] == 0)
i--;
if (i == -1)
return "0";
while (i >= 0)
ans += std::to_string(result[i--]);
}
if(toAddNeg && ans[0]!='0') {
ans = '-' + ans;
}
return ans;
}
/*
______________________________________________________
Division Function
______________________________________________________
Algorithm Explanation
The divisor increases iteratively till it reaches the Dividend. For fast reaching, a
pre-determined limit is calculated depending upon the length difference of divisor
and divident=d, and power of 10 is calculated for length of answer precision. Further simple
addition is done till it reaches dividend.
If the divisior can be strored in primitive type of Int. :
As the dividend and result can be very large we store them in string. We first take
digits which are divisible by number. After this take each digit and store result in string.
Algorithmic Time Complexity : O(log(val. of divisor)) , if divisor can be stored as primitive type
Algorithmic Time Complexity : O((10^(len. diff.)) log(val. of divisor))
Algorithmic Space Complexity : O(log(n-m))
where n ans m are the lengths of respective strings(Integers) provided.
*/
std::string bigint::divide(std::string str1, std::string str2) { // return arithmetic division of str1/str2
std::string ans = "";
if(str2 == "0") {
return "0";
} else if(str1 == str2) {
return "1";
} else if(str1[0] == '-' && str2[0] == '-') {
ans = divide(str1.erase(0, 1), str2.erase(0, 1));
} else if(str1[0] == '-') {
std::string temp = divide(str1.erase(0, 1), str2);
if(temp == "0")
ans = temp;
else
ans = '-' + temp;
} else if(str2[0] == '-') {
std::string temp = divide(str1, str2.erase(0, 1));
if(temp == "0")
ans = temp;
else
ans = '-' + temp;
} else {
if(str2 == "1")
return str1;
if(is_strictlyMaximum(str2, str1)) {
return "0";
}
if(str2.length() <= 19) {
std::stringstream strstrm(str2);
unsigned long long int int_str2 = 0;
strstrm >> int_str2;
ans = shortDivide(str1, int_str2);
}
else {
std::string temp = str2;
ans = "0";
std::string count = "0";
while(str1 == maximum(str1, str2)) {
int lenDiff = str1.length() - str2.length();
if(lenDiff > 0 && str1[0] > str2[0]) {
count = add(count, pow("10", std::to_string(lenDiff)));
str1 = subtract(str1, multiply(str2, pow("10", std::to_string(lenDiff))));
} else if(lenDiff > 0) {
count = add(count, pow("10", std::to_string(lenDiff-1)));
str1 = subtract(str1, multiply(str2, pow("10", std::to_string(lenDiff-1))));
} else {
count = add(count, "1");
str1 = subtract(str1, str2);
}
}
ans = count;
}
}
return ans;
}
std::string bigint::shortDivide(std::string s1, unsigned long long int divisor) { // return arithmetic division of str1/str2
std::string ans;
int idx = 0;
long long int temp = s1[idx] - '0';
while (temp < divisor) {
temp = temp*10 + (s1[++idx] - '0');
if(idx >= s1.length())
break;
}
while (s1.length() > idx) {
ans += (temp / divisor) + '0';
temp = (temp % divisor)*10 + s1[++idx] - '0';
}
if (ans.length() == 0)
return "0";
return ans;
}
std::string bigint::mod(std::string str1, std::string str2) { // return arithmetic modulos of str1%str2
std::string ans = subtract(str1, multiply(divide(str1, str2), str2));
return ans;
}
std::string bigint::maximum(std::string str1, std::string str2) { // return maximum of both strings.
std::string max = "";
bool bothNeg = false;
bool isMax1 = false;
bool isMax2 = false;
if(str1[0] == '-' && str2[0] == '-') {
bothNeg = true;
str1.erase(0, 1);
str2.erase(0, 1);
} else if(str1[0] == '-') {
return trim(str2);
} else if(str2[0] == '-') {
return trim(str1);
}
int str1_len = str1.length(), str2_len = str2.length();
if(str1_len == str2_len) {
for(int i = 0; i < str1_len; ++i) {
if(str1[i] != str2[i]) {
if(str1[i] > str2[i]) {
isMax1 = true;
} else {
isMax2 = true;
}
break;
}
}
if(!(isMax2 ^ isMax1)) {
isMax1 = true;