1 /* mpz_perfect_power_p(arg) -- Return non-zero if ARG is a perfect power,
4 Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or (at your
11 option) any later version.
13 The GNU MP Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 MA 02111-1307, USA. */
24 We are to determine if c is a perfect power, c = a ^ b.
25 Assume c is divisible by 2^n and that codd = c/2^n is odd.
26 Assume a is divisible by 2^m and that aodd = a/2^m is odd.
27 It is always true that m divides n.
29 * If n is prime, either 1) a is 2*aodd and b = n
30 or 2) a = c and b = 1.
31 So for n prime, we readily have a solution.
32 * If n is factorable into the non-trivial factors p1,p2,...
33 Since m divides n, m has a subset of n's factors and b = n / m.
35 BUG: Should handle negative numbers, since they can be odd perfect powers.
38 /* This is a naive approach to recognizing perfect powers.
39 Many things can be improved. In particular, we should use p-adic
40 arithmetic for computing possible roots. */
42 #include <stdio.h> /* for NULL */
47 static unsigned long int gcd _PROTO ((unsigned long int a, unsigned long int b));
48 static int isprime _PROTO ((unsigned long int t));
50 static const unsigned short primes[] =
51 { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53,
52 59, 61, 67, 71, 73, 79, 83, 89, 97,101,103,107,109,113,127,131,
53 137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,
54 227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,
55 313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,
56 419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,
57 509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,
58 617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,
59 727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,
60 829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,
61 947,953,967,971,977,983,991,997,0
63 #define SMALLEST_OMITTED_PRIME 1009
68 mpz_perfect_power_p (mpz_srcptr u)
70 mpz_perfect_power_p (u)
74 unsigned long int prime;
75 unsigned long int n, n2;
77 unsigned long int rem;
83 if (mpz_cmp_ui (u, 1) <= 0)
86 n2 = mpz_scan1 (u, 0);
92 uns = ABSIZ (u) - n2 / BITS_PER_MP_LIMB;
93 MPZ_TMP_INIT (q, uns);
94 MPZ_TMP_INIT (u2, uns);
96 mpz_tdiv_q_2exp (u2, u, n2);
101 for (i = 1; primes[i] != 0; i++)
104 rem = mpz_tdiv_ui (u2, prime);
105 if (rem == 0) /* divisable? */
107 rem = mpz_tdiv_q_ui (q, u2, prime * prime);
116 rem = mpz_tdiv_q_ui (q, u2, prime);
130 /* As soon as n2 becomes a prime number, stop factoring.
131 Either we have u=x^n2 or u is not a perfect power. */
137 if (mpz_cmp_ui (u2, 1) == 0)
145 unsigned long int nth;
146 /* We did not find any factors above. We have to consider all values
148 for (nth = 2;; nth++)
153 exact = mpz_padic_root (q, u2, nth, PTH);
156 exact = mpz_root (q, u2, nth);
162 if (mpz_cmp_ui (q, SMALLEST_OMITTED_PRIME) < 0)
171 unsigned long int nth;
172 /* We found some factors above. We just need to consider values of n
174 for (nth = 2; nth <= n2; nth++)
181 exact = mpz_padic_root (q, u2, nth, PTH);
184 exact = mpz_root (q, u2, nth);
190 if (mpz_cmp_ui (q, SMALLEST_OMITTED_PRIME) < 0)
202 exact = mpz_root (NULL, u2, n2);
207 static unsigned long int
209 gcd (unsigned long int a, unsigned long int b)
212 unsigned long int a, b;
222 count_trailing_zeros (an2, a);
225 count_trailing_zeros (bn2, b);
237 while ((a & 1) == 0);
244 while ((b & 1) == 0);
253 isprime (unsigned long int t)
259 unsigned long int q, r, d;
261 if (t < 3 || (t & 1) == 0)
264 for (d = 3, r = 1; r != 0; d += 2)