/*****************************************************************************/ /* */ /* FACTOR (a**p+b**p)/(a+b) */ /* 11/03/06 (dkc) */ /* */ /* This C program determines if a (or b) is a pth power w.r.t. (a**p+b**p)/ */ /* (a+b). Whether a (or b) is a pth power modulo p**2 when p does not */ /* a (or b) is determined. Whether a-b is a pth power modulo p**2 when */ /* a-b is a pth power w.r.t. (a**p+b**p)/(a+b) and p does divide a-b or a+b */ /* is also determined. Whether a+b is a pth power modulo p**2 when a+b is */ /* a pth power w.r.t. (a**p+b**p)/(a+b) and p does not divide a-b or a+b */ /* is also determined. */ /* */ /* Note: The least residues modulo p**2 table ("residue") is dependent on */ /* p. Modify the look-up table accordingly. */ /* */ /* The output is "(a<<16)|b". If p**2 does not divide a, b, or a-b, then */ /* an error is indicated ("error[1]" is set to a non-zero value). If p>3 */ /* and p**2 does not divide a+b, then an error is indicated. */ /* */ /*****************************************************************************/ #include <math.h> #include <stdio.h> #include "table11.h" unsigned int lmbd(unsigned int mode, unsigned int a); void bigprod(unsigned int a, unsigned int b, unsigned int c, unsigned int *p); void bigbigd(unsigned int *a, unsigned int *b); void differ(unsigned int *a, unsigned int *b); void dummy(unsigned int a, unsigned int b, unsigned int c); void bigbigs(unsigned int *addend, unsigned int *augend); void hugeprod(unsigned int a, unsigned int b, unsigned int c, unsigned d, unsigned int *e, unsigned int f); void bigbigq(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int *e, unsigned int f, unsigned int g); void bigresx(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int *e, unsigned int f); void quotient(unsigned int *a, unsigned int *b, unsigned int c); int main () { // // Note: The maximum "dbeg" value for p=3 is about 5000 // The maximum "dbeg" value for p=5 is about 1000 // The maximum "dbeg" value for p=7 is about 250 // The maximum "dbeg" value for p=11 is about 25 // unsigned int p=3; // input prime unsigned int dbeg=1000; // starting "a" value unsigned int dend=1; // ending "a" value //unsigned int stop=1; unsigned int sumdif=1; // select [(a**p+b**p)/(a+b)] if "sumdif" is non-zero // or [(a**p-b**p)/(a-b)] otherwise unsigned int select=0; // selects a, b, a-b, or a+b unsigned int correct=1; // There is a small probability that (a**p+b**p)/(a+b) is not // completely factored if "correct" is not set to 1. extern unsigned short residue[]; extern unsigned short table[]; extern unsigned int tmptab[]; extern unsigned int output[]; extern unsigned int error[]; extern unsigned int tmpsav; extern unsigned int count; unsigned int maxsiz=15600; unsigned int tsize=303; unsigned int tmpsiz; unsigned int outsiz=1999; unsigned int save[16]; // solutions array unsigned int savsiz=15; // size of solutions array minus one unsigned int d,e,a,b,temp; unsigned int i,j,k,l,m; unsigned int flag; unsigned short rem; unsigned int S[2],T[2],U[2],V[2],W[2],X[3],Y[4],Z[4]; unsigned int ps,base,tbase,limit; unsigned int n=0; double sqrt2=1.4142135; FILE *Outfp; Outfp = fopen("out25.dat","w"); /*********************************/ /* extend prime look-up table */ /*********************************/ error[0]=0; tmpsiz=0; for (i=0; i<tsize; i++) { j = (int)(table[i]); if (((j-1)/p)*p==(j-1)) { tmptab[tmpsiz] = j; tmpsiz=tmpsiz+1; } } for (d=2007; d<4000000; d++) { if (((d-1)/p)*p!=(d-1)) continue; if(d==(d/2)*2) continue; if(d==(d/3)*3) continue; if(d==(d/5)*5) continue; if(d==(d/7)*7) continue; if(d==(d/11)*11) continue; if(d==(d/13)*13) continue; if(d==(d/17)*17) continue; if(d==(d/19)*19) continue; /************************************************/ /* look for prime factors using look-up table */ /************************************************/ l = (int)(2.0 + sqrt((double)d)); k=0; if (l>table[tsize-1]) { error[0]=1; goto bskip; } else { for (i=0; i<tsize; i++) { if (table[i] < l) k=i; else break; } } flag=1; l=k; for (i=0; i<=l; i++) { k = table[i]; if ((d/k)*k == d) { flag=0; break; } } if (flag==1) { tmptab[tmpsiz]=d; tmpsiz = tmpsiz + 1; if (tmpsiz>=maxsiz) break; } } tmpsav=tmpsiz; limit=(tmptab[tmpsiz-1])>>16; limit=limit*limit; /***********************************/ /* factor (d**p + e**p)/(d + e) */ /***********************************/ ps=p*p; error[0]=0; // clear error array error[1]=0; error[2]=0; error[3]=0; count=0; for (d=dbeg; d>=dend; d--) { for (e=d-1; e>0; e--) { // if (e!=stop) continue; /******************************************/ /* check for common factors of d and e */ /******************************************/ if((d==(d/2)*2)&&(e==(e/2)*2)) continue; if((d==(d/3)*3)&&(e==(e/3)*3)) continue; if((d==(d/5)*5)&&(e==(e/5)*5)) continue; if((d==(d/7)*7)&&(e==(e/7)*7)) continue; /***********************/ /* Euclidean G.C.D. */ /***********************/ a=d; b=e; if (b>a) { temp=a; a=b; b=temp; } loop: temp = a - (a/b)*b; a=b; b=temp; if (b!=0) goto loop; if (a!=1) continue; /*************/ /* set base */ /*************/ if (select==0) { base=d; tbase=1; goto zskip; } if (select==1) { base=e; tbase=1; goto zskip; } if (sumdif==1) { if (select==2) { base=d-e; tbase=d+e; } else { base=d+e; tbase=d-e; } } else { if (select==2) { base=d+e; tbase=d-e; } else { base=d-e; tbase=d+e; } } /************************************/ /* compute (d**p + e**p)/(d + e) */ /************************************/ zskip:Y[0]=0; Y[1]=0; Y[2]=0; Y[3]=d; for (i=0; i<p-1; i++) hugeprod(Y[0], Y[1], Y[2], Y[3], Y, d); Z[0]=0; Z[1]=0; Z[2]=0; Z[3]=e; for (i=0; i<p-1; i++) hugeprod(Z[0], Z[1], Z[2], Z[3], Z, e); if (sumdif==1) { bigbigs(Y, Z); temp=d+e; if (((d+e)/p)*p==(d+e)) temp=temp*p; bigbigq(Z[0], Z[1], Z[2], Z[3], Y, 0, temp); } else { bigbigd(Y, Z); temp=d-e; if (((d-e)/p)*p==(d-e)) temp=temp*p; bigbigq(Z[0], Z[1], Z[2], Z[3], Y, 0, temp); } S[0]=Y[2]; S[1]=Y[3]; W[0]=S[0]; W[1]=S[1]; /************************************************/ /* look for prime factors using look-up table */ /************************************************/ if (S[0]==0) l = 32 - lmbd(1, S[1]); else l = 64 - lmbd(1, S[0]); j=l-(l/2)*2; l=l/2; l = 1 << l; if (j==1) l=(int)(((double)(l))*sqrt2); l=l+1; flag=0; if (l>tmptab[tmpsiz-1]) { flag=1; k=tmpsiz-1; } else { k=0; for (i=0; i<tmpsiz; i++) { if (tmptab[i] < l) k=i; else break; } } m=0; for (i=0; i<=k; i++) { l = tmptab[i]; quotient(S, T, l); V[0]=T[0]; V[1]=T[1]; bigprod(T[0], T[1], l, X); if ((S[0]!=X[1]) || (S[1]!=X[2])) continue; bigresx(0, (l-1)/p, 0, l, U, base); if ((U[0]!=0)||(U[1]!=1)) { goto askip; } aloop: S[0]=V[0]; S[1]=V[1]; save[m]=l; if (m < savsiz) m=m+1; else { error[0]=3; goto bskip; } quotient(S, T, l); V[0]=T[0]; V[1]=T[1]; bigprod(T[0], T[1], l, X); if ((S[0]==X[1]) && (S[1]==X[2])) goto aloop; } /***********************************************/ /* output prime factors satisfying criterion */ /***********************************************/ if ((S[0]!=0) || (S[1]!=1)) { if ((flag==1) && (correct==1)) { if (S[0]==0) j = (32 - lmbd(1, S[1])); else j = (64 - lmbd(1, S[0])); k=j-(j/2)*2; j=j/2; j = 1 << j; if (k==1) j=(int)(((double)(j))*sqrt2); for (i=tmptab[tmpsiz-1]; i<j; i+=2*p) { quotient(S, T, i); bigprod(T[0], T[1], i, X); if ((X[1]==S[0]) && (X[2]==S[1])) { bigresx(0, (i-1)/p, 0, i, U, base); if ((U[0]!=0)||(U[1]!=1)) { goto askip; } if (T[0]<limit) { // largest prime in table is 0x126f5f S[0]=T[0]; // for p=7 S[1]=T[1]; save[m]=i; if (m < savsiz) m=m+1; else { error[0]=3; goto bskip; } goto cskip; } else { error[0]=4; goto bskip; } } } } cskip: T[0]=0; T[1]=1; differ(S, T); quotient(T, T, p); bigresx(T[0], T[1], S[0], S[1], U, base); if ((U[0]==0)&&(U[1]==1)) { if (n>outsiz) { error[0]=6; goto bskip; } if (m>0) printf("d=%d, e=%d, m=%d \n",d,e,m+1); output[n]=((int)(d) << 16) | (int)(e); n=n+1; count=count+1; T[0]=S[0]; T[1]=S[1]; for (i=0; i<m; i++) { bigprod(T[0], T[1], save[i], X); T[0] = X[1]; T[1] = X[2]; } if ((T[0]!=W[0]) || (T[1]!=W[1])) { error[0]=7; error[1]=99; goto bskip; } if (((base/p)*p!=base)&&((tbase/p)*p!=tbase)) { flag=0; rem=base-(base/ps)*ps; for (l=0; l<p-1; l++) { if (rem==residue[l]) flag=1; } if (flag==0) { if ((p==3)&&((select==0)||(select==1))) { if (sumdif!=0) { if (((d+e)/p)*p==(d+e)) { if (((d+e)/ps)*ps!=(d+e)) goto ajump; } } else { if (((d-e)/p)*p==(d-e)) { if (((d-e)/ps)*ps!=(d-e)) goto ajump; } } } error[1]=100; error[2]=d; error[3]=e; goto bskip; } ajump: flag=0; } } else { goto askip; } } else { if (n>outsiz) { error[0]=6; goto bskip; } if (m>1) printf("d=%d, e=%d, m=%d \n",d,e,m); output[n]=((int)(d) << 16) | (int)(e); n=n+1; count=count+1; S[0]=0; S[1]=1; for (i=0; i<m; i++) { bigprod(S[0], S[1], save[i], X); S[0] = X[1]; S[1] = X[2]; } if ((S[0]!=W[0]) || (S[1]!=W[1])) { error[0]=7; error[1]=99; goto bskip; } if (((base/p)*p!=base)&&((tbase/p)*p!=tbase)) { flag=0; rem=base-(base/ps)*ps; for (l=0; l<p-1; l++) { if (rem==residue[l]) flag=1; } if (flag==0) { if ((p==3)&&((select==0)||(select==1))) { if (sumdif!=0) { if (((d+e)/p)*p==(d+e)) { if (((d+e)/ps)*ps!=(d+e)) goto bjump; } } else { if (((d-e)/p)*p==(d-e)) { if (((d-e)/ps)*ps!=(d-e)) goto bjump; } } } error[1]=100; error[2]=d; error[3]=e; goto bskip; } bjump: flag=0; } } askip:dummy(d,e,5); } } bskip: output[n]=-1; fprintf(Outfp," error0=%d error1=%d aflag=%d bflag=%d \n",error[0],error[1], error[2],error[3]); fprintf(Outfp," count=%d \n",n-1); if (n!=0) { for (i=0; i<n-1; i++) fprintf(Outfp," %#10x \n",output[i]); } if (error[1]!=0) printf(" error \n"); return(0); }