-
Notifications
You must be signed in to change notification settings - Fork 0
/
factorize.h
121 lines (84 loc) · 1.79 KB
/
factorize.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
#ifndef FACTORIZATION_H
#define FACTORIZATION_H
#include <cstdint>
#include <algorithm>
/*
https://stackoverflow.com/questions/31953836/decompose-a-number-into-2-prime-co-factors
https://github.com/michaeljclark/bignum
*/
int randint(int a, int b)
{
return (rand() % (b - a)) + a;
}
template <typename T>
T abs(const T &v) { return v < 0 ? -v : v; }
template <typename T>
T gcd(T a, T b)
{
while (b != 0) {
a %= b;
if (a == 0)
return b;
b %= a;
}
return a;
}
template <typename T>
T PollardRho(const T &n)
{
if (n % 2 == 0)
return 2;
T x, c, g, y;
x = randint(1, 1000);
c = randint(1, 1000);
g = 1;
y = x;
while (g == 1) {
x = ((x * x) % n + c) % n;
y = ((y * y) % n + c) % n;
y = ((y * y) % n + c) % n;
g = gcd(abs(x - y), n);
}
return g;
}
template <typename T>
T BrentRichard(const T &n)
{
if (n % 2 == 0)
return 2;
T y, c, m;
y = randint(1, 1000);
c = randint(1, 1000);
m = randint(1, 1000);
T g, r, q;
g = r = q = 1;
T x, ys;
x = ys = 0;
while (g == 1) {
x = y;
for (T i = 0; i < r; ++i) {
y = ((y * y) % n + c) % n;
}
T k = 0;
while (k < r && g == 1) {
ys = y;
for (T i = 0; i < std::min(m, r - k); ++i) {
y = ((y * y) % n + c) % n;
q = q * (abs(x - y)) % n;
}
g = gcd(q, n);
k += m;
}
r *= 2;
}
if (g == n) {
while (1) {
ys = ((ys * ys) % n + c) % n;
g = gcd(abs(x - ys), n);
if (g > 1)
break;
}
}
return g;
}
#endif