-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
75 lines (61 loc) · 1.58 KB
/
main.c
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
#include "config.h"
#include "factors.h"
#include "primesieve.h"
#include "trialdivision.h"
#include "quadraticsieve.h"
#include <stdio.h>
#include <string.h>
static void
print_result(mpz_t n, struct factors *f)
{
gmp_printf("%Zd: ", n);
for (ulong i = 0; i < f->size; i++) {
gmp_printf("%Zd ", f->factors[i]);
}
printf("\n");
}
int
main(int argc, char **argv)
{
char line[INTEGER_LIMIT];
if (argc > 1)
strncpy(&line[0], argv[1], INTEGER_LIMIT);
else if (scanf("%s\n", &line[0]) != 1) {
fprintf(stderr, "factor: unable to read number from stdin\n");
return 1;
}
mpz_t n;
mpz_init(n);
if (mpz_set_str(n, &line[0], 0) == -1 || mpz_cmp_ui(n, 1) < 0) {
fprintf(stderr, "factor: input must be a positive integer\n");
mpz_clear(n);
return 1;
}
if (mpz_cmp_ui(n, 1) == 0 || mpz_probab_prime_p(n, MILLERRABIN_REPEATS) > 0) {
gmp_printf("%Zd: %Zd\n", n, n);
mpz_clear(n);
return 0;
}
mpz_t t;
mpz_init(t);
mpz_sqrt(t, n);
struct factors *f = factors_create();
struct prime_sieve *ps = prime_sieve_create(MIN(TRIALDIVISION_LIMIT, mpz_get_ui(t)));
if (mpz_perfect_square_p(n)) {
factors_push(f, t);
factors_push(f, t);
} else {
mpz_set(t, n);
factors_push(f, t);
}
/* run trial division to find find small factors */
while (factors_remove_composite(f, t) && trial_division(t, f, ps));
prime_sieve_destroy(ps);
/* run quadratic sieve until factorized into only prime numbers */
while (factors_remove_composite(f, t) && quadratic_sieve(t, f, QUADRATIC_SIEVE_SIZE));
factors_sort(f);
print_result(n, f);
mpz_clears(n, t, NULL);
factors_destroy(f);
return 0;
}