forked from indy256/codelibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigint.cpp
78 lines (67 loc) · 2.04 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
#include "bigint.h"
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(1);
bigint random_bigint(int n) {
string s;
for (int i = 0; i < n; i++) {
s += uniform_int_distribution<int>('0', '9')(rng);
}
return bigint(s);
}
// random tests
int main() {
bigint x = bigint("120");
bigint y = bigint("5");
cout << x / y << endl;
for (int i = 0; i < 1000; i++) {
int n = uniform_int_distribution<int>(1, 100)(rng);
bigint a = random_bigint(n);
bigint res = sqrt(a);
bigint xx = res * res;
bigint yy = (res + 1) * (res + 1);
if (xx > a || yy <= a) {
cout << i << endl;
cout << a << " " << res << endl;
break;
}
int m = uniform_int_distribution<int>(1, n)(rng);
bigint b = random_bigint(m) + 1;
res = a / b;
xx = res * b;
yy = b * (res + 1);
if (xx > a || yy <= a) {
cout << i << endl;
cout << a << " " << b << " " << res << endl;
break;
}
}
{
bigint a = random_bigint(10'000);
bigint b = random_bigint(2000);
auto t1 = chrono::high_resolution_clock::now();
bigint c = a / b;
auto t2 = chrono::high_resolution_clock::now();
chrono::duration<double, milli> duration = t2 - t1;
cout << duration.count() << " ms" << endl;
cout << endl;
}
bigint a = random_bigint(200'000);
bigint b = random_bigint(200'000);
bigint c1, c2;
{
auto t1 = chrono::high_resolution_clock::now();
c1 = a * b;
auto t2 = chrono::high_resolution_clock::now();
chrono::duration<double, milli> duration = t2 - t1;
cout << duration.count() << " ms" << endl;
}
{
auto t1 = chrono::high_resolution_clock::now();
c2 = a.mul_simple(b);
auto t2 = chrono::high_resolution_clock::now();
chrono::duration<double, milli> duration = t2 - t1;
cout << duration.count() << " ms" << endl;
}
cout << (c1 == c2) << endl;
}