chiark / gitweb /
Add commentary and licence notices.
[rhodes] / factor.c
1 /* -*-c-*-
2  *
3  * Trivial interface to PARI's integer factoring machinery
4  *
5  * (c) 2017 Mark Wooding
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Rhodes, a distributed discrete-log finder.
11  *
12  * Rhodes is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * Rhodes is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Rhodes; if not, write to the Free Software Foundation,
24  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 #include <assert.h>
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31
32 #include <pari/pari.h>
33
34 static const char *prog;
35
36 __attribute__((noreturn))
37 static void barf(const char *msg, ...)
38 {
39   va_list ap;
40
41   va_start(ap, msg);
42   fprintf(stderr, "%s: ", prog);
43   vfprintf(stderr, msg, ap);
44   fputc('\n', stderr);
45   va_end(ap);
46   exit(2);
47 }
48
49 int main(int argc, char *argv[])
50 {
51   GEN n, ff, pp, ee;
52   long nf;
53
54   prog = argv[0];
55   if (argc != 2) { fprintf(stderr, "usage: %s N\n", prog); exit(2); }
56   pari_init(16ul*1024ul*1024ul, 0);
57   n = gp_read_str(argv[1]);
58   if (typ(n) != t_INT || signe(n) <= 0) barf("expected a positive integer");
59   sd_factor_proven("1", d_SILENT);
60   ff = Z_factor(n);
61   assert(typ(ff) == t_MAT);
62   assert(lg(ff) == 3);
63   pp = gel(ff, 1); ee = gel(ff, 2);
64   assert(typ(pp) == t_COL);
65   assert(typ(ee) == t_COL);
66   nf = lg(pp); assert(nf == lg(ee));
67   nf--;
68   while (nf--) {
69     pp++; ee++;
70     pari_printf("%Ps %Ps\n", *pp, *ee);
71   }
72   return (0);
73 }