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
|
#include <string.h>
#include <stdio.h>
#include <math.h> /* for findk */
#if defined(_WIN32)
#include <windows.h>
#endif
#include "pdefs.h"
#ifdef __STDC__
#include <stdlib.h>
#endif
#include "precision.h"
#include "pfactor.h"
#ifdef __STDC__
extern unsigned *pfactorbase(precision n, unsigned k,
unsigned *m, unsigned aborts);
extern double pomeranceLpow(double n, double alpha);
#else
extern unsigned *pfactorbase();
extern double pomeranceLpow();
#endif
int verbose = 0;
int debug = 0;
extern unsigned cfracNabort;
extern unsigned cfracTsolns;
extern unsigned cfracPsolns;
extern unsigned cfracT2solns;
extern unsigned cfracFsolns;
extern unsigned short primes[];
extern unsigned primesize;
/*
* Return the value of "f(p,d)" from Knuth's exercise 28
*/
float pfKnuthEx28(p, d)
unsigned p;
precision d;
{
register float res;
precision k = pUndef;
(void) pparm(d);
if (p == 2) {
if (peven(d)) {
pset(&k, phalf(d));
if (peven(k)) {
res = 2.0/3.0 + pfKnuthEx28(2,k)/2.0; /* eliminate powers of 2 */
} else { /* until only one 2 left in d. */
res = 1.0/3.0; /* independent of (the now odd) k. Wow! */
}
} else { /* d now odd */
pset(&k, phalf(d));
if (podd(k)) {
res = 1.0/3.0; /* f(2,4k+3): d%8 == 3 or 7 */
} else {
if (podd(phalf(k))) {
res = 2.0/3.0; /* f(2,8k+5): d%8 == 5 */
} else {
res = 4.0/3.0; /* f(2,8k+1): d%8 == 1 */
}
}
}
} else { /* PART 3: p odd, d could still be even (OK) */
pset(&k, utop(p));
if peq(ppowmod(d, phalf(psub(k, pone)), k), pone) {
res = (float) (p+p) / (((float) p)*p-1.0); /* beware int overflow! */
} else {
res = 0.0;
}
}
pdestroy(k);
pdestroy(d);
if (debug > 1) {
fprintf(stdout, "f(%u,", p);
fprintf(stdout, "d) = %9.7f\n", res);
}
return res;
}
float cfrac_logf(unsigned p, precision n, unsigned k)
{
register float res;
(void) pparm(n);
#if 0 /* old code for non-float machines; not worth the cost */
pset(&r, utop(k));
log2sqrtk = plogb(pipow(r, q >> 1), ptwo);
fplog2p = (f(p,pmul(r,n),q) * plogb(pipow(utop(p),q),ptwo)+(q>>1))/q;
#endif
res = pfKnuthEx28(p, pmul(itop(k),n)) * log((double) p);
/* res -= log((double) k) * 0.5; */
pdestroy(n);
return res;
}
/*
* Find the best value of k for the given n and m.
*
* Input/Output:
* n - the number to factor
* m - pointer to size of factorbase (0 = select "best" size)
* aborts - the number of early aborts
*/
unsigned findk(n, m, aborts, maxk)
precision n;
register unsigned *m;
unsigned aborts, maxk;
{
unsigned k, bestk = 0, count, bestcount = 0, maxpm;
float sum, max = -1.0E+15; /* should be small enough */
unsigned *p;
register unsigned i;
register unsigned short *primePtr;
(void) pparm(n);
for (k = 1; k < maxk; k++) { /* maxk should best be m+m? */
if (debug) {
fputs("kN = ", stdout);
fputp(stdout, pmul(utop(k), n)); putc('\n', stdout);
}
count = *m;
p = pfactorbase(n, k, &count, aborts);
if (p == (unsigned *) 0) {
fprintf(stderr, "couldn't compute factor base in findk\n");
exit(1);
}
maxpm = p[count-1];
sum = 0.0;
primePtr = primes;
while (*primePtr <= maxpm) {
sum += cfrac_logf((unsigned) *primePtr++, n, k);
}
sum -= log((double) k) * 0.5;
if (verbose > 2) fprintf(stdout, "%u: %5.2f", k, sum);
if (debug) fprintf(stdout, " log(k)/2=%5.2f", log((double) k) * 0.5);
if (verbose > 2) {
fputs("\n", stdout);
fflush(stdout);
}
if (sum > max) {
max = sum;
bestk = k;
bestcount = count;
}
#ifndef IGNOREFREE
free(p);
#endif
}
*m = bestcount;
pdestroy(n);
return bestk;
}
extern char *optarg;
extern int optind;
char *progName;
extern int getopt();
int main(argc, argv)
int argc;
char *argv[];
{
unsigned m = 0, k = 0;
unsigned maxCount = 1<<30, count, maxk = 0;
int ch;
precision n = pUndef, f = pUndef;
unsigned aborts = 3;
unsigned *p;
double d;
progName = *argv;
while ((ch = getopt(argc, argv, "a:k:i:dv")) != EOF) switch (ch) {
case 'a':
aborts = atoi(optarg);
break;
case 'k':
maxk = atoi(optarg);
break;
case 'i':
maxCount = atoi(optarg);
break;
case 'd':
debug++;
break;
case 'v':
verbose++;
break;
default:
usage: fprintf(stderr,
"usage: %s [-dv] [-a aborts ] [-k maxk ] [-i maxCount ] n [[ m ] k ]\n",
progName);
return 1;
}
argc -= optind;
argv += optind;
if (argc == 0) {
argc = 1;
static char* argvx[2] = { "17545186520507317056371138836327483792736", NULL };
argv = argvx;
}
if (argc < 1 || argc > 3) goto usage;
pset(&n, atop(*argv++)); --argc;
if (argc) { m = atoi(*argv++); --argc; }
if (argc) { k = atoi(*argv++); --argc; }
if (k == 0) {
if (maxk == 0) {
maxk = m / 2 + 5;
if (verbose) fprintf(stdout, "maxk = %u\n", maxk);
}
k = findk(n, &m, aborts, maxk);
if (verbose) {
fprintf(stdout, "k = %u\n", k);
}
}
count = maxCount;
pcfracInit(m, k, aborts);
pset(&f, pcfrac(n, &count));
count = maxCount - count;
if (verbose) {
putc('\n', stdout);
fprintf(stdout, "Iterations : %u\n", count);
fprintf(stdout, "Early Aborts : %u\n", cfracNabort);
fprintf(stdout, "Total Partials : %u\n", cfracTsolns);
fprintf(stdout, "Used Partials : %u\n", cfracT2solns);
fprintf(stdout, "Full Solutions : %u\n", cfracPsolns);
fprintf(stdout, "Factor Attempts: %u\n", cfracFsolns);
}
if (f != pUndef) {
fputp(stdout, n);
fputs(" = ", stdout);
fputp(stdout, f);
fputs(" * ", stdout);
pdivmod(n, f, &n, pNull);
fputp(stdout, n);
putc('\n', stdout);
}
pdestroy(f);
pdestroy(n);
return 0;
}
|