```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 ``` ```/* from http://www.mindspring.com/~pate/course/chap08/squfof.c PLB 20AUG2010 */ /* Author: Pate Williams (c) 1997 "Algorithm 8.7.2 (Shanks's SQUFOF). Given an odd integer N, this algorithm tries to find a non-trivial factor of N." -Henri Cohen- See "A Course in Computational Algebraic Number Theory" by Henri Cohen page 437. */ #include #include #include struct factor {long expon, prime;}; long exp_mod(long x, long b, long n) /* returns x ^ b mod n */ { long a = 1l, s = x; while (b != 0) { if (b & 1l) a = (a * s) % n; b >>= 1; if (b != 0) s = (s * s) % n; } return a; } long gcd(long a, long b) { long r; a = labs(a); b = labs(b); while (b > 0) r = a % b, a = b, b = r; return a; } int RabinMiller(long N) /* returns 0 if N is composite 1 otherwise */ { long N1 = N - 1, a, b, c = 20, e, q = N1, t = 0; if (N == 2) return 1; while (!(q & 1)) q >>= 1, t++; while (c > 0) { do a = rand() % N; while (a == 0); e = 0; b = exp_mod(a, q, N); if (b != 1) { while (b != 1 && b != N1 && e <= t - 2) { b = (b * b) % N; e++; } if (b != N1) return 0; } c--; } return 1; } int square_test(long n, long *s) { int square = 1; long q11[11], q63[63], q64[64], q65[65]; long k, r, t; for (k = 0; k < 11; k++) q11[k] = 0; for (k = 0; k < 6; k++) q11[(k * k) % 11] = 1; for (k = 0; k < 63; k++) q63[k] = 0; for (k = 0; k < 32; k++) q63[(k * k) % 63] = 1; for (k = 0; k < 64; k++) q64[k] = 0; for (k = 0; k < 32; k++) q64[(k * k) % 64] = 1; for (k = 0; k < 65; k++) q65[k] = 0; for (k = 0; k < 33; k++) q65[(k * k) % 65] = 1; t = n % 64; r = n % 45045l; if (q64[t] == 0) square = 0; if (square && q63[r % 63] == 0) square = 0; if (square && q65[r % 65] == 0) square = 0; if (square && q11[r % 11] == 0) square = 0; if (square) *s = sqrt(n); else *s = 0; return square; } void reduce(long A, long B, long C, long *a, long *b, long *c) { long D, l, r, s; *a = A; *b = B; *c = C; D = B * B - 4 * A * C; s = sqrt(D); l = labs(*c); if (l < s / 2) r = - *b + 2 * l * ((*b + s) / (2 * l)); else r = - *b + 2 * l; *a = *c; *b = r; *c = (r * r - D) / (4 * *c); } void reduction(long A, long B, long C, long *a, long *b, long *c) { long D, l, r, s; *a = A; *b = B; *c = C; D = B * B - 4 * A * C; s = sqrt(D); L1: if (*b < 0) *b = - *b, *a = - *a, *c = - *c; if (*b > labs(s - 2 * labs(*a)) && *b <= s) return; l = labs(*c); if (l < s / 2) r = - *b + 2 * l * ((*b + s) / (2 * l)); else r = - *b + 2 * l; *a = *c; *b = r; *c = (r * r - D) / (4 * *c); goto L1; } int SQUFOF(long *N, long *f, long *e) { int found; long A, B, C, D, L, Q[1024], Q_size, a, b, b1, c; long ap, bp, cp, d, i, j, s, z; if (RabinMiller(*N)) return - 2; if (square_test(*N, f)) { *e = 2; *N = 1; return - 1; } if (*N % 4 == 1) { D = *N; d = sqrt(*N); b = (d - 1) / 2; b = (b << 1) + 1; } else { D = 4 * *N; d = sqrt(D); b = 2 * (d / 2); } a = 1; c = (b * b - D) / 4; Q_size = 0; i = 0; L = sqrt(d); ap = a; bp = b; cp = c; L4: reduce(ap, bp, cp, &A, &B, &C); i++; if (i & 1) goto L7; if (square_test(labs(A), &z)) { a = z; found = 0; for (j = 0; !found && j < Q_size; j++) found = a == Q[j]; if (!found) goto L8; } if (A == 1) return 0; L7: if (labs(A) <= L) { Q[Q_size] = A; Q_size++; } ap = A; bp = B; cp = C; goto L4; L8: s = gcd(a, gcd(B, D)); if (s > 1) { *f = s; *e = 2; *N /= s * s; return 1; } else reduction(a, - B, a * C, &a, &b, &c); L9: b1 = b; ap = a; bp = b; cp = c; reduce(a, b, c, &a, &b, &c); if (a == 1) { a = ap; b = bp; c = cp; goto L10; } if (b1 != b) goto L9; L10: if (a == 1) return 0; if (a & 1) { *e = 1; *f = labs(a); if (*f == 1 || *f == *N) return 0; *N /= *f; return 1; } *e = 1; *f = labs(a / 2); if (*f == 1 || *f == *N) return 0; *N /= *f; return 1; } int main(void) { int flag; long N, count, i, j, n; struct factor f[32], t; for (;;) { /* printf("N = "); PLB 20AUG2010 */ /* scanf("%ld", &N); PLB 20AUG2010 */ N = 633003781; /* PLB 20AUG2010 */ if (N <= 0) break; if (N & 1) { n = N; flag = SQUFOF(&N, &f[0].prime, &f[0].expon); if (flag == - 2) printf("%ld is prime\n", N); else if (flag == - 1) printf("%ld = %ld * %ld\n", f[0].prime * f[0].prime, f[0].prime, f[0].prime); else if (flag == 0) printf("%ld not factored\n", N); else { count = 1; printf("%ld is composite\nfactors:\n", n); for (;;) { flag = SQUFOF(&N, &f[count].prime, &f[count].expon); if (flag == - 1) break; if (flag == 1) count++; if (flag == - 2) { f[count].expon = 1; f[count].prime = N; count++; break; } if (flag == 0) break; if (N == 1) break; } if (flag != 0) { for (i = 0; i < count - 1; i++) for (j = i + 1; j < count; j++) if (f[i].prime > f[j].prime) t = f[i], f[i] = f[j], f[j] = t; for (i = 0; i < count; i++) { printf("%ld", f[i].prime); if (f[i].expon > 1) printf(" ^ %ld\n", f[i].expon); else printf("\n"); } } else printf("error - can't factor number\n"); } } else printf("%ld must be odd!\n", N); } return 0; } ```
 ```1 ``` ```Timeout ```