On Wednesday 24 December 2008 01:04:13 Bill Hart wrote: > Great! Torbjorn's patch is clearly licensed v3+, so we cannot use it > as eMPIRe is LGPL v2+. >
While waiting for Santa , I don't think he's coming , I did this This should correct the present errors , I can svn into trunk at some point ,after autoheaven :) Jason --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "mpir-devel" group. To post to this group, send email to mpir-devel@googlegroups.com To unsubscribe from this group, send email to mpir-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/mpir-devel?hl=en -~----------~----~----~----~------~----~------~--~---
/* mpz_perfect_power_p(arg) -- Return non-zero if ARG is a perfect power, zero otherwise. Copyright 1998, 1999, 2000, 2001, 2005 Free Software Foundation, Inc. This file is part of the GNU MP Library. The GNU MP Library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. The GNU MP Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with the GNU MP Library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /* We are to determine if c is a perfect power, c = a ^ b. Assume c is divisible by 2^n and that codd = c/2^n is odd. Assume a is divisible by 2^m and that aodd = a/2^m is odd. It is always true that m divides n. * If n is prime, either 1) a is 2*aodd and b = n or 2) a = c and b = 1. So for n prime, we readily have a solution. * If n is factorable into the non-trivial factors p1,p2,... Since m divides n, m has a subset of n's factors and b = n / m. */ /* This is a naive approach to recognizing perfect powers. Many things can be improved. In particular, we should use p-adic arithmetic for computing possible roots. */ #include <stdio.h> /* for NULL */ #include "gmp.h" #include "gmp-impl.h" #include "longlong.h" static unsigned long int gcd _PROTO ((unsigned long int a, unsigned long int b)); static int isprime _PROTO ((unsigned long int t)); static const unsigned short primes[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,101,103,107,109,113,127,131, 137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223, 227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311, 313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409, 419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503, 509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613, 617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719, 727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827, 829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941, 947,953,967,971,977,983,991,997,0 }; #define SMALLEST_OMITTED_PRIME 1009 int mpz_perfect_power_p (mpz_srcptr u) { unsigned long int prime; unsigned long int n, n2; int i; unsigned long int rem; mpz_t u2, q; int exact; mp_size_t uns; mp_size_t usize = SIZ (u); TMP_DECL; if (usize == 0) return 1; /* consider 0 a perfect power */ n2 = mpz_scan1 (u, 0); if (n2 == 1) return 0; /* 2 divides exactly once. */ if (n2 > 1 && POW2_P(n2) && usize < 0) return 0; /* 2 has power of two multiplicity with negative U */ TMP_MARK; uns = ABS (usize) - n2 / BITS_PER_MP_LIMB; MPZ_TMP_INIT (q, uns); MPZ_TMP_INIT (u2, uns); mpz_tdiv_q_2exp (u2, u, n2); if (isprime (n2)) goto n2prime; for (i = 1; primes[i] != 0; i++) { prime = primes[i]; if (mpz_divisible_ui_p (u2, prime)) /* divisible by this prime? */ { rem = mpz_tdiv_q_ui (q, u2, prime * prime); if (rem != 0) { TMP_FREE; return 0; /* prime divides exactly once, reject */ } mpz_swap (q, u2); for (n = 2;;) { rem = mpz_tdiv_q_ui (q, u2, prime); if (rem != 0) break; mpz_swap (q, u2); n++; } if ( POW2_P(n) && usize < 0) { TMP_FREE; return 0; /* power of two multiplicity with negative U, reject */ } n2 = gcd (n2, n); if (n2 == 1) { TMP_FREE; return 0; /* we have multiplicity 1 of some factor */ } if (mpz_cmpabs_ui (u2, 1) == 0) { TMP_FREE; if(usize<0 && POW2_P(n2))return 0;/* factoring completed; not consistent power */ return 1; /* factoring completed; consistent power */ } /* As soon as n2 becomes a prime number, stop factoring. Either we have u=x^n2 or u is not a perfect power. */ if (isprime (n2)) goto n2prime; } } if (n2 == 0) { /* We found no factors above; have to check all values of n. */ unsigned long int nth; for (nth = usize < 0 ? 3 : 2;; nth++) { if (! isprime (nth)) continue; #if 0 exact = mpz_padic_root (q, u2, nth, PTH); if (exact) #endif exact = mpz_root (q, u2, nth); if (exact) { TMP_FREE; return 1; } if (mpz_cmpabs_ui (q, SMALLEST_OMITTED_PRIME) < 0) { TMP_FREE; return 0; } } } else { unsigned long int nth; /* We found some factors above. We just need to consider values of n that divides n2. */ for (nth = usize < 0 ? 3 : 2; nth <= n2; nth++) { if (! isprime (nth)) continue; if (n2 % nth != 0) continue; #if 0 exact = mpz_padic_root (q, u2, nth, PTH); if (exact) #endif exact = mpz_root (q, u2, nth); if (exact) { TMP_FREE; return 1; } if (mpz_cmpabs_ui (q, SMALLEST_OMITTED_PRIME) < 0) { TMP_FREE; return 0; } } TMP_FREE; return 0; } n2prime: if(n2==2 && usize<0){TMP_FREE;return 0;} exact = mpz_root (NULL, u2, n2); TMP_FREE; return exact; } static unsigned long int gcd (unsigned long int a, unsigned long int b) { int an2, bn2, n2; if (a == 0) return b; if (b == 0) return a; count_trailing_zeros (an2, a); a >>= an2; count_trailing_zeros (bn2, b); b >>= bn2; n2 = MIN (an2, bn2); while (a != b) { if (a > b) { a -= b; do a >>= 1; while ((a & 1) == 0); } else /* b > a. */ { b -= a; do b >>= 1; while ((b & 1) == 0); } } return a << n2; } static int isprime (unsigned long int t) { unsigned long int q, r, d; if (t < 3 || (t & 1) == 0) return t == 2; for (d = 3, r = 1; r != 0; d += 2) { q = t / d; r = t - q * d; if (q < d) return 1; } return 0; }
/* Exercise mpz_perfect_power_p Copyright 2008 Jason Moxham */ #include <stdio.h> #include <stdlib.h> #include "gmp.h" #include "gmp-impl.h" #include "tests.h" int perfpow_ref(mpz_t x) {mpz_t y,q,r;unsigned long i;int ret=0; if(mpz_cmp_ui(x,0)==0 || mpz_cmp_ui(x,1)==0 || mpz_cmp_si(x,-1)==0)return 1; mpz_init_set(y,x);mpz_init(q);mpz_init(r);mpz_abs(y,y); for(i=2;;i++) {mpz_root(q,y,i);mpz_pow_ui(r,q,i); if(mpz_cmp(r,y)==0 && (i%2==1 || SIZ(x)>0) ){ret=1;break;} if(mpz_cmp_ui(q,1)<=0)break;} mpz_clear(y);mpz_clear(q);mpz_clear(r); return ret;} int main (void) {int i,r1,r2,j,a,b,c,d;mpz_t x,m; tests_start (); mpz_init(x);mpz_init(m); /* check small values */ for(i=-10000;i<10000;i++) {mpz_set_si(x,i); r1=mpz_perfect_power_p(x); r2=perfpow_ref(x); if(r1!=r2){printf("mpz_perfect_power_p wrong on %d got %d want %d\n",i,r1,r2);abort();} } /* check (-1)^i.2^a.3^b.5^c.x^d where x is big */ /* 1.0.0.0.5 picked up one error 1.10.6.0.0 picked up another error want a good selection of powers to try all possibilitys */ for(i=1;i<=2;i++) {for(d=0;d<10;d++) {for(a=0;a<11;a++) {for(b=0;b<11;b++) {for(c=0;c<11;c++) {mpz_set_ui(x,1); mpz_set_si(m,-1);for(j=0;j<i;j++)mpz_mul(x,x,m); mpz_set_ui(m,2);for(j=0;j<a;j++)mpz_mul(x,x,m); mpz_set_ui(m,3);for(j=0;j<b;j++)mpz_mul(x,x,m); mpz_set_ui(m,5);for(j=0;j<c;j++)mpz_mul(x,x,m); mpz_set_ui(m,117039007);for(j=0;j<d;j++)mpz_mul(x,x,m); r1=mpz_perfect_power_p(x);r2=perfpow_ref(x); if(r1!=r2){printf("mpz_perfect_power_p wrong on %d.%d.%d.%d.%d got %d want %d\n",i,a,b,c,d,r1,r2);abort();} } } } } } mpz_clear(x);mpz_clear(m); tests_end (); exit (0); }
## Process this file with automake to generate Makefile.in # Copyright 1996, 1997, 1999, 2000, 2001, 2002, 2003 Free Software # Foundation, Inc. # # This file is part of the GNU MP Library. # # The GNU MP Library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or (at your # option) any later version. # # The GNU MP Library is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public # License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with the GNU MP Library; see the file COPYING.LIB. If not, write to # the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. INCLUDES = -I$(top_srcdir) -I$(top_srcdir)/tests LDADD = $(top_builddir)/tests/libtests.la $(top_builddir)/libgmp.la check_PROGRAMS = t-addsub t-cmp t-mul t-mul_i t-tdiv t-tdiv_ui t-fdiv \ t-fdiv_ui t-cdiv_ui t-gcd t-gcd_ui t-lcm dive dive_ui t-sqrtrem convert io \ t-inp_str logic bit t-powm t-powm_ui t-pow t-div_2exp reuse \ t-root t-perfsqr t-jac t-bin t-get_d t-get_d_2exp t-get_si \ t-set_d t-set_si \ t-fac_ui t-fib_ui t-lucnum_ui t-scan t-fits \ t-divis t-divis_2exp t-cong t-cong_2exp t-sizeinbase t-set_str \ t-aorsmul t-cmp_d t-cmp_si t-hamdist t-oddeven t-popcount t-set_f \ t-io_raw t-import t-export t-pprime_p t-perfpow TESTS = $(check_PROGRAMS) # Temporary files used by the tests. Removed automatically if the tests # pass, but ensure they're cleaned if they fail. # CLEANFILES = *.tmp $(top_builddir)/tests/libtests.la: cd $(top_builddir)/tests; $(MAKE) $(AM_MAKEFLAGS) libtests.la