1 // Factor using the Pollard rho method
6 static unsigned int *n
;
19 if (equaln(p1
, 0) || equaln(p1
, 1) || equaln(p1
, -1)) {
33 push_symbol(MULTIPLY
);
41 // factor using table look-up, then switch to rho method if necessary
43 // From TAOCP Vol. 2 by Knuth, p. 380 (Algorithm A)
55 for (k
= 0; k
< 10000; k
++) {
59 // if n is 1 then we're done
61 if (MLENGTH(n
) == 1 && n
[0] == 1) {
74 unsigned int *d
, *q
, *r
;
76 d
= mint(primetab
[k
]);
82 // if n is 1 then we're done
84 if (MLENGTH(n
) == 1 && n
[0] == 1) {
86 push_factor(d
, count
);
92 mdivrem(&q
, &r
, n
, d
);
94 // continue looping while remainder is zero
96 if (MLENGTH(r
) == 1 && r
[0] == 0) {
108 push_factor(d
, count
);
110 // q = n/d, hence if q < d then n < d^2 so n is prime
112 if (mcmp(q
, d
) == -1) {
123 // From TAOCP Vol. 2 by Knuth, p. 385 (Algorithm B)
129 unsigned int *g
, *one
, *t
, *x
, *xprime
;
158 // g = gcd(x' - x, n)
174 // x = (x ^ 2 + 1) mod n
189 if (mcmp(g
, n
) == 0) {
209 // xprime = xprime mod n
221 push_factor(unsigned int *d
, int count
)
233 p1
->u
.q
.a
= mint(count
);