-
Notifications
You must be signed in to change notification settings - Fork 0
/
prune.cpp
193 lines (180 loc) · 3.32 KB
/
prune.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
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
/* prune.c -- Compute T(s) from Project Euler Problem 256
Written November 28, 2019 by Eric Olson */
#include <iostream>
const uint64_t smax = 100000000000ULL;
const uint32_t Pnum = 40000;
const uint32_t fnum = 20;
typedef struct
{
uint64_t s;
uint32_t fmax, i, p[fnum];
uint8_t n[fnum];
} factors;
factors x;
uint32_t Pn, Tisn, in;
uint64_t P[Pnum], smin;
uint8_t z[fnum];
bool tfree(uint64_t k, uint64_t l)
{
uint64_t n = l / k;
uint64_t lmin = (k + 1) * n + 2;
uint64_t lmax = (k - 1) * (n + 1) - 2;
return lmin <= l && l <= lmax;
}
bool isprime(uint64_t p)
{
uint32_t i;
for (i = 1; i < in; i++)
{
if (!(p % P[i]))
return 0;
}
for (i = in; P[i] * P[i] <= p; i++)
{
if (!(p % P[i]))
return 0;
}
in = i - 1;
return 1;
}
void doinit()
{
uint32_t i, p;
uint64_t r;
smin = smax;
P[0] = 2;
P[1] = 3;
Pn = 2, in = 1;
for (p = 5; Pn < Pnum; p += 2)
{
if (isprime(p))
P[Pn++] = p;
}
if (p <= smax / p + 1)
{
std::cout << "The maximum prime " << p << " is too small!" << std::endl;
exit(1);
}
r = 1;
for (i = 0; i < fnum - 1; i++)
{
if (P[i] > smax / r + 1)
return;
r *= P[i];
}
std::cout << "Distinct primes " << fnum << " in factorisation too few!" << std::endl;
exit(2);
}
uint64_t ppow(uint64_t p, uint32_t n)
{
uint64_t r;
if (!n)
return 1;
r = 1;
for (;;)
{
if (n & 1)
r *= p;
n >>= 1;
if (!n)
return r;
p *= p;
}
}
uint32_t sigma()
{
uint32_t i;
uint32_t r = x.n[0];
for (i = 1; i <= x.fmax; i++)
r *= x.n[i] + 1;
return r;
}
uint32_t T()
{
uint32_t r, w;
for (w = 0; w < fnum; w++)
z[w] = 0;
r = 0;
for (;;)
{
uint32_t i;
uint64_t k, l;
for (i = 0; i <= x.fmax; i++)
{
if (z[i] < x.n[i])
{
z[i]++;
break;
}
z[i] = 0;
}
if (i > x.fmax)
break;
k = 1;
l = 1;
for (i = 0; i <= x.fmax; i++)
{
k *= ppow(x.p[i], z[i]);
l *= ppow(x.p[i], x.n[i] - z[i]);
}
if (k <= l)
r += tfree(k, l) ? 1 : 0;
}
return r;
}
void Twork()
{
uint64_t s, pmax;
uint32_t fmax, i, p, r;
s = x.s;
r = sigma();
if (r >= Tisn)
{
r = T();
if (r == Tisn && s < smin)
smin = s;
}
i = x.i;
fmax = x.fmax;
pmax = smin / s + 1;
p = (uint32_t)P[i];
if (p <= pmax)
{
x.n[fmax]++;
x.s = s * p;
Twork();
x.n[fmax]--;
}
fmax++;
x.n[fmax] = 1;
for (i++; i < Pnum; i++)
{
p = (uint32_t)P[i];
if (p > pmax)
break;
x.p[fmax] = p;
x.s = s * p;
x.i = i;
x.fmax = fmax;
Twork();
}
x.n[fmax] = 0;
}
uint64_t Tinv(uint32_t n)
{
Tisn = n;
x.p[0] = uint32_t(P[0]);
x.n[0] = 1;
x.i = 0;
x.s = 2;
x.fmax = 0;
Twork();
return smin < smax ? smin : -1;
}
int main()
{
uint32_t n = 1000;
doinit();
std::cout << "T(" << Tinv(n) << ")=" << n << std::endl;
return 0;
}