-
Notifications
You must be signed in to change notification settings - Fork 30
/
bigint class.h
642 lines (576 loc) · 22.9 KB
/
bigint class.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
/*
bigint class.h
bigint Library for C++
MIT License
Created by Roshan Gupta on 16-10-2020
Copyright (c) 2020 Roshan Gupta
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <string>
#include <sstream>
#include <vector>
#include <math.h>
#include <limits>
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;
}
};