dixon
[damir.git] / dixon / dixon.cc
blob07ebb4172f0063afb1f914767b09d37d5fba59ef
1 #include <myint.h>
3 myInt factor(myInt n) {
4 myInt x, xx, d, root;
6 root = sqrt(n);
8 if (issquare(n))
9 return root;
11 x = root;
13 for (;; x++) {
14 d = gcd(x, n);
16 if (1 < d && d < n)
17 return d;
19 xx = (x * x) % n;
21 if (issquare(xx)) {
22 d = gcd((x - sqrt(xx)) % n, n);
24 if (1 < d && d < n)
25 return d;
28 cout << "couldn't find!" << endl;
30 return 0;
33 int main(int argc, char *argv[]) {
34 myInt n, f;
36 if (argc < 2) {
37 cerr << "usage: " << argv[0] << " number_to_factorize" << endl;
38 return 1;
41 n = argv[1];
43 if (n == 1) {
44 cout << n << ": is " << n << endl;
45 return 0;
48 cout << n << ": " << flush;
50 if (isprime(n)) {
51 cout << "is prime" << endl;
52 return 0;
55 while (!isprime(n)) {
56 f = factor(n);
57 cout << f << " " << flush;
58 n /= f;
60 cout << n << endl;
62 return 0;