diff options
author | tege <tege@gmplib.org> | 2001-10-22 04:52:47 +0200 |
---|---|---|
committer | tege <tege@gmplib.org> | 2001-10-22 04:52:47 +0200 |
commit | 4e30dd927372079984dc06e70a8579c4f1f5cc1d (patch) | |
tree | 4cd604c1b8d189b7cad60c50cfb0cd5e6b669e48 /demos/factorize.c | |
parent | 1bc89ba9e33df8185ab3be4994c4b270b9fa0c43 (diff) | |
download | gmp-4e30dd927372079984dc06e70a8579c4f1f5cc1d.tar.gz |
(factor): Check for number to factor == 0.
(main): When invoked without arguments, read from stdin.
Diffstat (limited to 'demos/factorize.c')
-rw-r--r-- | demos/factorize.c | 69 |
1 files changed, 41 insertions, 28 deletions
diff --git a/demos/factorize.c b/demos/factorize.c index ce7bbde60..d1aab2474 100644 --- a/demos/factorize.c +++ b/demos/factorize.c @@ -285,6 +285,9 @@ factor (mpz_t t, unsigned long p) { unsigned int division_limit; + if (mpz_sgn (t) == 0) + return; + /* Set the trial division limit according the size of t. */ division_limit = mpz_sizeinbase (t, 2); if (division_limit > 1000) @@ -324,40 +327,50 @@ main (int argc, char *argv[]) argc--; } - p = 0; - for (i = 1; i < argc; i++) + mpz_init (t); + if (argc > 1) { - if (!strncmp (argv[i], "-Mp", 3)) - { - p = atoi (argv[i] + 3); - mpz_init_set_ui (t, 1); - mpz_mul_2exp (t, t, p); - mpz_sub_ui (t, t, 1); - } - else if (!strncmp (argv[i], "-2kp", 4)) + p = 0; + for (i = 1; i < argc; i++) { - p = atoi (argv[i] + 4); - continue; - } - else - { - mpz_init_set_str (t, argv[i], 0); - } + if (!strncmp (argv[i], "-Mp", 3)) + { + p = atoi (argv[i] + 3); + mpz_set_ui (t, 1); + mpz_mul_2exp (t, t, p); + mpz_sub_ui (t, t, 1); + } + else if (!strncmp (argv[i], "-2kp", 4)) + { + p = atoi (argv[i] + 4); + continue; + } + else + { + mpz_set_str (t, argv[i], 0); + } - if (mpz_cmp_ui (t, 0) == 0) - puts ("-"); - else + if (mpz_cmp_ui (t, 0) == 0) + puts ("-"); + else + { + factor (t, p); + puts (""); + } + } + } + else + { + for (;;) { - factor (t, p); + mpz_inp_str (t, stdin, 0); + if (feof (stdin)) + break; + mpz_out_str (stdout, 10, t); printf (" = "); + factor (t, 0); puts (""); } } - exit (0); -} -void -dmp (mpz_t x) -{ - mpz_out_str (stdout, 10, x); - puts (""); + exit (0); } |