1 /* Test mpz_perfect_power_p.
3 Contributed to the GNU project by Torbjorn Granlund and Martin Boij.
5 Copyright 2008, 2009 Free Software Foundation, Inc.
7 This file is part of the GNU MP Library.
9 The GNU MP Library is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
14 The GNU MP Library is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
64 { "0xed1b1182118135d", 1},
65 {"-0xed1b1182118135d", 1},
66 { "0xe71f6eb7689cc276b2f1", 1},
67 {"-0xe71f6eb7689cc276b2f1", 0},
68 { "0x12644507fe78cf563a4b342c92e7da9fe5e99cb75a01", 1},
69 {"-0x12644507fe78cf563a4b342c92e7da9fe5e99cb75a01", 0},
70 { "0x1ff2e7c581bb0951df644885bd33f50e472b0b73a204e13cbe98fdb424d66561e4000000", 1},
71 {"-0x1ff2e7c581bb0951df644885bd33f50e472b0b73a204e13cbe98fdb424d66561e4000000", 1},
72 { "0x2b9b44db2d91a6f8165c8c7339ef73633228ea29e388592e80354e4380004aad84000000", 1},
73 {"-0x2b9b44db2d91a6f8165c8c7339ef73633228ea29e388592e80354e4380004aad84000000", 1},
74 { "0x28d5a2b8f330910a9d3cda06036ae0546442e5b1a83b26a436efea5b727bf1bcbe7e12b47d81", 1},
75 {"-0x28d5a2b8f330910a9d3cda06036ae0546442e5b1a83b26a436efea5b727bf1bcbe7e12b47d81", 1},
89 for (i = 0; tests[i].num_as_str != NULL; i++)
91 mpz_set_str (x, tests[i].num_as_str, 0);
92 got = mpz_perfect_power_p (x);
96 fprintf (stderr, "mpz_perfect_power_p returns %d when %d was expected\n", got, want);
97 fprintf (stderr, "fault operand: %s\n", tests[i].num_as_str);
108 check_random (int reps)
110 mpz_t n, np, temp, primes[NRP];
111 int i, j, k, unique, destroy, res;
112 unsigned long int nrprimes, primebits, g, exp[NRP], e;
113 gmp_randstate_ptr rands;
121 for (i = 0; i < NRP; i++)
122 mpz_init (primes[i]);
124 for (i = 0; i < reps; i++)
126 mpz_urandomb (np, rands, 32);
127 nrprimes = mpz_get_ui (np) % NRP + 1; /* 1-NRP unique primes */
129 mpz_urandomb (np, rands, 32);
130 g = mpz_get_ui (np) % 32 + 2; /* gcd 2-33 */
132 for (j = 0; j < nrprimes;)
134 mpz_urandomb (np, rands, 32);
135 primebits = mpz_get_ui (np) % 100 + 3; /* 3-102 bit primes */
136 mpz_urandomb (primes[j], rands, primebits);
137 mpz_nextprime (primes[j], primes[j]);
139 for (k = 0; k < j; k++)
141 if (mpz_cmp (primes[j], primes[k]) == 0)
149 mpz_urandomb (np, rands, 32);
150 e = 371 / (10 * primebits) + mpz_get_ui (np) % 11 + 1; /* Magic constants */
157 /* Destroy d exponents, d in [1, nrprimes - 1] */
164 mpz_urandomb (np, rands, 32);
165 destroy = mpz_get_ui (np) % (nrprimes - 2) + 1;
169 for (k = destroy + 1; k < nrprimes; k++)
170 g = mpn_gcd_1 (&g, 1, exp[k]);
172 for (j = 0; j < destroy; j++)
174 mpz_urandomb (np, rands, 32);
175 e = mpz_get_ui (np) % 50 + 1;
176 while (mpn_gcd_1 (&g, 1, e) > 1)
184 mpz_pow_ui (n, primes[0], exp[0]);
185 for (j = 1; j < nrprimes; j++)
187 mpz_pow_ui (temp, primes[j], exp[j]);
188 mpz_mul (n, n, temp);
191 res = mpz_perfect_power_p (n);
195 if (res == 0 && exp[0] > 1)
197 printf("n is a perfect power, perfpow_p disagrees\n");
198 gmp_printf("n = %Zu\nprimes[0] = %Zu\nexp[0] = %lu\n", n, primes[0], exp[0]);
201 else if (res == 1 && exp[0] == 1)
203 gmp_printf("n = %Zu\n", n);
204 printf("n is now a prime number, but perfpow_p still believes n is a perfect power\n");
212 gmp_printf("n = %Zu\nn was destroyed, but perfpow_p still believes n is a perfect power\n", n);
221 for (i = 0; i < NRP; i++)
222 mpz_clear (primes[i]);
226 main (int argc, char **argv)
237 n_tests = atoi (argv[1]);
238 check_random (n_tests);