-
Notifications
You must be signed in to change notification settings - Fork 21
/
powerfree.c
282 lines (231 loc) · 6.07 KB
/
powerfree.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FUNC_isqrt 1
#define FUNC_ipow 1
#define FUNC_ctz 1
#include "ptypes.h"
#include "constants.h"
#include "powerfree.h"
#include "util.h"
#include "factor.h"
#include "real.h"
static INLINE UV T(UV n) {
return (n*(n+1)) >> 1;
}
static UV fprod(UV n, UV r) {
UV P, fac[MPU_MAX_FACTORS+1];
int i, nfactors;
P = 1;
nfactors = factor_exp(n, fac, 0);
for (i = 0; i < nfactors; i++)
P *= 1 - ipow(fac[i], r);
return P;
}
int is_powerfree(UV n, uint32_t k)
{
UV fac[MPU_MAX_FACTORS+1];
UV exp[MPU_MAX_FACTORS+1];
int i, nfactors;
if (k < 2 || n <= 1) return (n==1);
if (k >= BITS_PER_WORD) return 1;
if (n < (UVCONST(1) << (k-1))) return 1;
if (n == ((n >> k) << k)) return 0;
if (k == 2) return is_square_free(n);
/* Try to quickly find common powers so we don't have to factor */
if (k == 3) {
if ( !(n % 27) || !(n % 125) || !(n % 343) || !(n%1331) || !(n%2197) )
return 0;
if (n < 4913) return 1;
}
/* A factor iterator would be good to use here */
nfactors = factor_exp(n, fac, exp);
for (i = 0; i < nfactors; i++) {
if (exp[i] >= k)
return 0;
}
return 1;
}
/* Basic method from https://arxiv.org/pdf/1107.4890.pdf */
static UV squarefree_count(UV n)
{
signed char* mu;
IV *M, *Mx, Mxisum, mert;
UV I, D, i, j, S1 = 0, S2 = 0;
if (n < 4) return n;
I = rootint(n, 5); /* times loglogn ^ (4/5) */
D = isqrt(n / I);
mu = range_moebius(0, D);
S1 += n;
New(0, M, D+1, IV);
M[0] = 0;
M[1] = 1;
mert = 1;
for (i = 2; i <= D; i++) {
if (mu[i] != 0) {
S1 += mu[i] * (n/(i*i));
mert += mu[i];
}
M[i] = mert;
}
Safefree(mu);
Newz(0, Mx, I+1, IV);
Mxisum = 0;
for (i = I-1; i > 0; i--) {
IV Mxi = 1;
UV xi = isqrt(n/i);
UV L = isqrt(xi);
for (j = 1; j <= xi/(L+1); j++)
Mxi -= M[j] * (xi/j - xi/(j+1));
for (j = 2; j <= L; j++)
Mxi -= (xi/j <= D) ? M[xi/j] : Mx[j*j*i];
Mx[i] = Mxi;
Mxisum += Mxi;
}
S2 = Mxisum - (I - 1) * M[D];
Safefree(Mx);
Safefree(M);
return S1 + S2;
}
UV powerfree_count(UV n, uint32_t k)
{
UV i, nk, count;
if (k < 2) return (n >= 1);
if (n < 4) return n;
if (k == 2) return squarefree_count(n);
count = n;
nk = rootint(n, k);
if (nk <= 100) {
for (i = 2; i <= nk; i++) {
int m = moebius(i);
if (m != 0)
count += m * (n / ipow(i, k));
}
} else {
signed char* mu = range_moebius(0, nk);
for (i = 2; i <= nk; i++)
if (mu[i] != 0)
count += mu[i] * (n/ipow(i,k));
Safefree(mu);
}
return count;
}
UV powerfree_sum(UV n, uint32_t k)
{
UV i, nk, sum;
if (k < 2) return (n >= 1);
if (n >= (UVCONST(1) << (BITS_PER_WORD/2))) return 0; /* Overflow */
sum = T(n);
nk = rootint(n, k);
for (i = 2; i <= nk; i++) {
int m = moebius(i);
if (m != 0) {
UV ik = (k==2) ? i*i : ipow(i,k);
UV nik = n / ik;
sum += m * ik * T(nik);
}
}
return sum;
}
UV powerfree_part(UV n, uint32_t k)
{
UV fac[MPU_MAX_FACTORS+1];
UV exp[MPU_MAX_FACTORS+1];
UV t, P;
int i, nfactors;
if (k < 2 || n <= 1)
return (n==1);
if (k >= BITS_PER_WORD || n < (UVCONST(1) << (k-1)))
return n;
/* Pull all powers of two out */
t = ctz(n);
P = n >> t;
if ((t % k)) P <<= (t % k);
nfactors = factor_exp(P, fac, exp);
for (i = 0; i < nfactors; i++)
if (exp[i] >= k)
P /= ipow(fac[i], exp[i] - (exp[i] % k));
return P;
}
UV powerfree_part_sum(UV n, uint32_t k)
{
UV j, nk, sum = 0;
if (k < 2 || n <= 1) return (n >= 1);
if (n >= (UVCONST(1) << (BITS_PER_WORD/2))) return 0; /* Overflow */
sum = T(n);
nk = rootint(n,k);
/* Using the factor iterator is overkill because of the limited range. */
if (nk <= 100) {
for (j = 2; j <= nk; j++)
sum += fprod(j,k) * T(n/ipow(j,k));
} else {
UV P, *factors;
factor_range_context_t fctx;
int i, nfactors;
fctx = factor_range_init(2, nk, 0);
for (j = 2; j <= nk; j++) {
nfactors = factor_range_next(&fctx);
factors = fctx.factors;
for (P = 1, i = 0; i < nfactors; i++)
if (i == 0 || factors[i] != factors[i-1])
P *= 1 - ipow(factors[i], k);
sum += P * T(n/ipow(j,k));
}
factor_range_destroy(&fctx);
}
return sum;
}
#if BITS_PER_WORD == 64
#define MAX_PFC2 UVCONST(11214275663373200251)
#define MAX_PFC3 UVCONST(15345982395028449439)
#define MAX_PFC4 UVCONST(17043655258566511333)
#else
#define MAX_PFC2 UVCONST(2611027094)
#define MAX_PFC3 UVCONST(3573014938)
#define MAX_PFC4 UVCONST(3968285222)
#endif
UV nth_powerfree(UV n, uint32_t k)
{
long double zm;
UV qk, count, diff, thresh, i;
if (k < 2) return 0;
if (n < 4) return n;
/* Check for overflow. */
if (k == 2 && n > MAX_PFC2) return 0;
if (k == 3 && n > MAX_PFC3) return 0;
if (k >= 4 && n > MAX_PFC4) {
if (k == 4) return 0;
if (n > powerfree_count(UV_MAX,k)) return 0;
}
/* Step 1: Density ZM and expected value QK. */
zm = 1.0 + ld_riemann_zeta(k);
qk = (UV)(zm * (long double) n + 0.5);
thresh = (k <= 2) ? 200 : (k == 3) ? 60 : (k == 4) ? 2 : 1;
for (i = 0; i < 10; i++) {
/* Step 2: Initial count at QK and difference from goal. */
count = powerfree_count(qk, k);
diff = (count >= n) ? count-n : n-count;
/* Step 3: Update estimate using expected density. */
if (diff <= thresh) break;
if (count > n) qk -= (UV)((long double)diff * zm);
else qk += (UV)((long double)diff * zm);
}
/* Step 4: Get ourselves onto a powerfree number */
while (!is_powerfree(qk,k)) qk--;
/* Step 5: Walk forwards or backwards until we get to the goal. */
while (count != n) {
do { qk += (count < n) ? 1 : -1; } while (!is_powerfree(qk,k));
count += (count < n) ? 1 : -1;
}
return qk;
}
/******************************************************************************/
UV squarefree_kernel(UV n)
{
UV P, fac[MPU_MAX_FACTORS+1];
int i, nfactors;
nfactors = factor_exp(n, fac, 0);
for (P = 1, i = 0; i < nfactors; i++)
P *= fac[i];
return P;
}