-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathmath.hpp
More file actions
340 lines (264 loc) · 6.79 KB
/
Copy pathmath.hpp
File metadata and controls
340 lines (264 loc) · 6.79 KB
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
/*
===========================================================================
Math functions for BigInt
===========================================================================
*/
#ifndef BIG_INT_MATH_FUNCTIONS_HPP
#define BIG_INT_MATH_FUNCTIONS_HPP
#include <string>
#include "functions/conversion.hpp"
#include "functions/random.hpp"
/*
abs
---
Returns the absolute value of a BigInt.
*/
BigInt abs(const BigInt& num) {
return num < 0 ? -num : num;
}
/*
big_pow10
---------
Returns a BigInt equal to 10^exp.
NOTE: exponent should be a non-negative integer.
*/
BigInt big_pow10(size_t exp) {
return BigInt("1" + std::string(exp, '0'));
}
/*
pow (BigInt)
------------
Returns a BigInt equal to base^exp.
*/
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 % 2)
result_odd *= result;
result *= result;
exp /= 2;
}
return result * result_odd;
}
/* pow (BigInt, BigInt)
----------------
Returns a BigInt equal to base^exp where exp is a BigInt
*/
BigInt pow(const BigInt& base, BigInt 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 % 2 == 1)
result_odd *= result;
result *= result;
exp /= 2;
}
return result * result_odd;
}
/*
pow (Integer)
-------------
Returns a BigInt equal to base^exp.
*/
BigInt pow(const long long& base, int exp) {
return pow(BigInt(base), exp);
}
/*
pow (String)
------------
Returns a BigInt equal to base^exp.
*/
BigInt pow(const std::string& base, int exp) {
return pow(BigInt(base), exp);
}
/*
sqrt
----
Returns the positive integer square root of a BigInt using Newton's method.
NOTE: the input must be non-negative.
*/
BigInt sqrt(const BigInt& num) {
if (num < 0)
throw std::invalid_argument("Cannot compute square root of a negative integer");
// Optimisations for small inputs:
if (num == 0)
return 0;
else if (num < 4)
return 1;
else if (num < 9)
return 2;
else if (num < 16)
return 3;
BigInt sqrt_prev = -1;
// The value for `sqrt_current` is chosen close to that of the actual
// square root.
// Since a number's square root has at least one less than half as many
// digits as the number,
// sqrt_current = 10^(half_the_digits_in_num - 1)
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;
}
/*
gcd(BigInt, BigInt)
-------------------
Returns the greatest common divisor (GCD, a.k.a. HCF) of two BigInts using
Euclid's algorithm.
*/
BigInt gcd(const BigInt &num1, const BigInt &num2){
BigInt abs_num1 = abs(num1);
BigInt abs_num2 = abs(num2);
// base cases:
if (abs_num2 == 0)
return abs_num1; // gcd(a, 0) = |a|
if (abs_num1 == 0)
return abs_num2; // gcd(0, a) = |a|
BigInt remainder = abs_num2;
while (remainder != 0) {
remainder = abs_num1 % abs_num2;
abs_num1 = abs_num2; // previous remainder
abs_num2 = remainder; // current remainder
}
return abs_num1;
}
/*
gcd(BigInt, Integer)
--------------------
*/
BigInt gcd(const BigInt& num1, const long long& num2){
return gcd(num1, BigInt(num2));
}
/*
gcd(BigInt, String)
-------------------
*/
BigInt gcd(const BigInt& num1, const std::string& num2){
return gcd(num1, BigInt(num2));
}
/*
gcd(Integer, BigInt)
--------------------
*/
BigInt gcd(const long long& num1, const BigInt& num2){
return gcd(BigInt(num1), num2);
}
/*
gcd(String, BigInt)
-------------------
*/
BigInt gcd(const std::string& num1, const BigInt& num2){
return gcd(BigInt(num1), num2);
}
/*
lcm(BigInt, BigInt)
-------------------
Returns the least common multiple (LCM) of two BigInts.
*/
BigInt lcm(const BigInt& num1, const BigInt& num2) {
if (num1 == 0 or num2 == 0)
return 0;
return abs(num1 * num2) / gcd(num1, num2);
}
/*
lcm(BigInt, Integer)
--------------------
*/
BigInt lcm(const BigInt& num1, const long long& num2){
return lcm(num1, BigInt(num2));
}
/*
lcm(BigInt, String)
-------------------
*/
BigInt lcm(const BigInt& num1, const std::string& num2){
return lcm(num1, BigInt(num2));
}
/*
lcm(Integer, BigInt)
--------------------
*/
BigInt lcm(const long long& num1, const BigInt& num2){
return lcm(BigInt(num1), num2);
}
/*
lcm(String, BigInt)
-------------------
*/
BigInt lcm(const std::string& num1, const BigInt& num2){
return lcm(BigInt(num1), num2);
}
/*
is_probable_prime(size_t)
------------------------
Uses the Miller-Rabin primality test to return if the BigInt
is prime with probablity ( 1 - (4^-certainty) ) * 100%
*/
bool BigInt::is_probable_prime(size_t certainty){
//treats 1, 2, and 3 as prime numbers
if (*this == BigInt(1) || *this == BigInt(2) || *this == BigInt(3)){
return true;
}
//even numbers cannot be prime
if (*this % BigInt(2) == 0){
return false;
}
const BigInt maxRand = *this - 2; //we later choose random value between 0 to n-1
const BigInt one = 1;
const BigInt two = 2;
BigInt randNum;
//need to compute d and r such that d*2^r = n - 1. where n = this
BigInt d;
BigInt x;
int r;
d = maxRand;
++d;
r = 0;
int continueWhile = 1;
while( d % two == 0){
++r;
d /= two;
}
while ( certainty-- > 0 ){
//pick a random number
randNum = n_random(maxRand.value);
x = pow(randNum, d);
x = x % *this;
if (x == one || x == *this - one){
continue;
}
continueWhile = 0;
for( int i=0; i < r-1; i++){
x = pow(x, 2) % *this;
if (x == *this-one){
continueWhile = 1;
break;
}
}
if(continueWhile) continue;
return false;
}
return true;
}
#endif // BIG_INT_MATH_FUNCTIONS_HPP