/*****************************************************************************/
/*									     */
/*  FACTOR (a**p+b**p)/(a+b)						     */
/*  11/03/06 (dkc)							     */
/*									     */
/*  This C program determines if a, b, a-b, p(a+b) (and not p) are pth powers*/
/*  w.r.t. (a**p+b**p)/(a+b).  p**2 must divide a, b, a-b, or a+b.	     */
/*									     */
/*  Note:  The least-residues modulo p**2 table is dependent on p.  Modify   */
/*  the look-up table accordingly.					     */
/*									     */
/*  The output is "(a<<16)|b, count" where "count" is the number of prime    */
/*  factors of [(a**p+b**p)/(a+b)].					     */
/*									     */
/*****************************************************************************/
#include <math.h>
#include <stdio.h>
#include "table12.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 65000.
//	 The maximum "dbeg" value for p=5 is about 65000.
//	 The maximum "dbeg" value for p=7 is about 1000.
//	 The maximum "dbeg" value for p=11 is about 50.
//
unsigned int p=3;	  // input prime
unsigned int dbeg=2000; 	// starting "a" value
unsigned int dend=1;	      // ending "a" value
//unsigned int stop=165;
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 ppower=1;	  // power of p factor of 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.
unsigned int flag2=0;	 // if set, check if 2 is a pth power w.r.t (a^p+b^p)/(a+b)
unsigned int residue[2]={1,8};			       // p=3
//unsigned int residue[4]={1,7,18,24};			     // p=5
//unsigned int residue[6]={1,18,19,30,31,48};		   // p=7
//unsigned int residue[10]={1,3,9,27,40,81,94,112,118,120};  // p=11

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=100000;
unsigned int tsize=1228;
unsigned int tmpsiz,iters;
unsigned int outsiz=199;
unsigned int save[16];	 // solutions array
unsigned int savsiz=15;  // size of solutions array minus one
unsigned int d,e,a,b,temp,ptemp;
unsigned int i,j,k,l,m;
unsigned int flag,flag0,limit;
unsigned int rem;
unsigned int S[2],T[2],U[2],V[2],W[2],X[3],Y[4],Z[4];
unsigned int ps;
unsigned int n=0;
unsigned int histo[500];
unsigned int histom[500],histop[500];
double sqrt2=1.4142135;
FILE *Outfp;
Outfp = fopen("out44.dat","w");
for (i=0; i<500; i++) {
   histo[i]=0;
   histom[i]=0;
   histop[i]=0;
   }
iters=0;
/*********************************/
/*  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=9975; d<100000000; 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;
      }
   }
printf("size=%d, prime=%d \n",tmpsiz,tmptab[tmpsiz-1]);
tmpsav=tmpsiz;
limit=(tmptab[tmpsiz-1])>>16;
limit=limit*limit;
/***********************************/
/*  factor (d**p + e**p)/(d + e)   */
/***********************************/
ptemp=1;
for (i=0; i<ppower; i++)
   ptemp=ptemp*p;
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;
/******************************************/
/* check if p^2 divides a, b, a+b, or a-b */
/******************************************/
      if ((d/ps)*ps==d)
	 goto dskip;
      if ((e/ps)*ps==e)
	 goto dskip;
      if (((d-e)/ps)*ps==(d-e))
	 goto dskip;
      if (((d+e)/ps)*ps!=(d+e))
	 continue;
/************************************/
/*  compute (d**p + e**p)/(d + e)   */
/************************************/
dskip:for (i=0; i<16; i++)
	 save[i]=0;
      flag0=1;
      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;
	 if (((l-1)/ps)*ps!=(l-1))
	    goto askip;
	 bigresx(0, (l-1)/p, 0, l, U, d);
	 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])) {
		  if (((i-1)/ps)*ps!=(i-1))
		     goto askip;
		  bigresx(0, (i-1)/p, 0, i, U, d);
		  if ((U[0]!=0)||(U[1]!=1)) {
		     goto askip;
		     }
		  if (T[0]<=limit) {	 // largest prime in table is
		     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, ps);
	 bigprod(T[0], T[1], ps, X);
	 T[0]=0;
	 T[1]=1;
	 differ(S, T);
	 if ((X[1]!=T[0]) || (X[2]!=T[1]))
	    goto askip;
	 quotient(T, T, p);
	 bigresx(T[0], T[1], S[0], S[1], U, d);
	 if ((U[0]==0)&&(U[1]==1)) {
	    bigresx(T[0], T[1], S[0], S[1], U, p);
	    if ((U[0]==0)&&(U[1]==1))
	       goto askip;
	    bigresx(T[0], T[1], S[0], S[1], U, e);
	    if ((U[0]!=0)||(U[1]!=1))
	       goto askip;
	    if (sumdif!=0)
	       bigresx(T[0], T[1], S[0], S[1], U, d-e);
	    else
	       bigresx(T[0], T[1], S[0], S[1], U, d+e);
	    if ((U[0]!=0)||(U[1]!=1))
	       goto askip;
	    if (sumdif!=0)
	       bigresx(T[0], T[1], S[0], S[1], U, ptemp*(d+e));
	    else
	       bigresx(T[0], T[1], S[0], S[1], U, ptemp*(d-e));
	    if ((U[0]!=0)||(U[1]!=1))
	       goto askip;
	    if (flag2!=0) {
	       bigresx(T[0], T[1], S[0], S[1], U, 2);
	       if ((U[0]!=0)||(U[1]!=1))
		  goto askip;
	       }
	    for (i=0; i<m; i++) {
	       l=save[i];
	       bigresx(0, (l-1)/p, 0, l, U, p);
	       if ((U[0]==0)&&(U[1]==1))
		  goto askip;
	       bigresx(0, (l-1)/p, 0, l, U, e);
	       if ((U[0]!=0)||(U[1]!=1))
		  goto askip;
	       if (sumdif!=0)
		  bigresx(0, (l-1)/p, 0, l, U, d-e);
	       else
		  bigresx(0, (l-1)/p, 0, l, U, d+e);
	       if ((U[0]!=0)||(U[1]!=1))
		  goto askip;
	       if (sumdif!=0)
		  bigresx(0, (l-1)/p, 0, l, U, ptemp*(d+e));
	       else
		  bigresx(0, (l-1)/p, 0, l, U, ptemp*(d-e));
	       if ((U[0]!=0)||(U[1]!=1))
		  goto askip;
	       if (flag2!=0) {
		  bigresx(0, (l-1)/p, 0, l, U, 2);
		  if ((U[0]!=0)||(U[1]!=1))
		     goto askip;
		  }
	       }
	    if (p==3) {
	       i=(S[1]-1)/18;
	       if ((i<500)&&(S[0]==0)&&((S[1]&0x80000000)==0))
		  histo[i]=histo[i]+1;
	       for (i=0; i<m; i++) {
		  j=(save[i]-1)/18;
		  if (j<500)
		     histo[j]=histo[j]+1;
		  }
	       }
	    histom[m+1]=histom[m+1]+1;
	    if ((m==1)&&(S[1]==save[0]))
	       histop[2]=histop[2]+1;
	    if ((m==2)&&(S[1]==save[0])&&(S[1]==save[1]))
	       histop[3]=histop[3]+1;
	    if ((m==3)&&(S[1]==save[0])&&(S[1]==save[1])&&(S[1]==save[2]))
	       histop[4]=histop[4]+1;
	    if (((m>0)&&(p!=3))||((m>1)&&(p==3))) {
	       printf("d=%d, e=%d, m=%d, f=%d, %d, %d, %d, %d, %d  %#010x %#010x \n",d,e,m+1,save[0],save[1],save[2],save[3],save[4],save[5],S[0],S[1]);
	       if (iters<2000) {
		  fprintf(Outfp,"d=%d, e=%d, m=%d, f=%d, %d, %d, %d, %d, %d  %#010x %#010x \n",d,e,m+1,save[0],save[1],save[2],save[3],save[4],save[5],S[0],S[1]);
		  iters=iters+1;
		  }
	       }
	    if ((n+1)<outsiz) {
	       output[n]=((int)(d) << 16) | (int)(e);
	       output[n+1]=((m+1)<<16)|flag0;
	       n=n+2;
	       }
	    else
	       error[0]=6;
	    count=count+1;
	    V[0]=T[0];	   // temporary
	    V[1]=T[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;
	       goto bskip;
	       }
	    if ((d/p)*p!=d) {
	       flag=0;
	       rem=d-(d/ps)*ps;
	       for (l=0; l<p-1; l++) {
		  if (rem==residue[l])
		     flag=1;
		  }
	       if (flag==0) {
		  error[1]=1;
		  error[2]=d;
		  error[3]=e;
		  goto bskip;
		  }
	       }
	    if (((e/p)*p!=e)) {
	       flag=0;
	       rem=e-(e/ps)*ps;
	       for (l=0; l<p-1; l++) {
		  if (rem==residue[l])
		     flag=1;
		  }
	       if (flag==0) {
		  error[1]=2;
		  error[2]=d;
		  error[3]=e;
		  goto bskip;
		  }
	       }
	    if ((((d+e)/p)*p!=(d+e))&&(((d-e)/p)*p!=(d-e))) {
	       flag=0;
	       if (sumdif==0)
		  rem=(d+e)-((d+e)/ps)*ps;
	       else
		  rem=(d-e)-((d-e)/ps)*ps;
	       for (l=0; l<p-1; l++) {
		  if (rem==residue[l])
		     flag=1;
		  }
	       if (flag==0) {
		  error[1]=3;
		  error[2]=d;
		  error[3]=e;
		  goto bskip;
		  }
	       }
	    }
	 else {
	    goto askip;
	    }
	 }
      else {
	 for (i=0; i<m; i++) {
	    l=save[i];
	    bigresx(0, (l-1)/p, 0, l, U, p);
	    if ((U[0]==0)&&(U[1]==1))
	       goto askip;
	    bigresx(0, (l-1)/p, 0, l, U, e);
	    if ((U[0]!=0)||(U[1]!=1))
	       goto askip;
	    if (sumdif!=0)
	       bigresx(0, (l-1)/p, 0, l, U, d-e);
	    else
	       bigresx(0, (l-1)/p, 0, l, U, d+e);
	    if ((U[0]!=0)||(U[1]!=1))
	       goto askip;
	    if (sumdif!=0)
	       bigresx(0, (l-1)/p, 0, l, U, ptemp*(d+e));
	    else
	       bigresx(0, (l-1)/p, 0, l, U, ptemp*(d-e));
	    if ((U[0]!=0)||(U[1]!=1))
	       goto askip;
	    if (flag2!=0) {
	       bigresx(0, (l-1)/p, 0, l, U, 2);
	       if ((U[0]!=0)||(U[1]!=1))
		  goto askip;
	       }
	    }
	 if (p==3) {
	    for (i=0; i<m; i++) {
	       j=(save[i]-1)/18;
	       if (j<500)
		  histo[j]=histo[j]+1;
	       }
	    }
	 histom[m]=histom[m]+1;
	 if ((m==2)&&(save[0]==save[1]))
	    histop[2]=histop[2]+1;
	 if ((m==3)&&(save[0]==save[1])&&(save[0]==save[2]))
	    histop[3]=histop[3]+1;
	 if ((m==4)&&(save[0]==save[1])&&(save[0]==save[2])&&(save[0]==save[3]))
	    histop[4]=histop[4]+1;
	 if (((m>1)&&(p!=3))||((m>2)&&(p==3))) {
	    printf("d=%d, e=%d, m=%d, f=%d, %d, %d, %d, %d, %d \n",d,e,m,save[0],save[1],save[2],save[3],save[4],save[5]);
	    if (iters<2000) {
	       fprintf(Outfp,"d=%d, e=%d, m=%d, f=%d, %d, %d, %d, %d, %d \n",d,e,m,save[0],save[1],save[2],save[3],save[4],save[5]);
	       iters=iters+1;
	       }
	    }
	 if ((n+1)<outsiz) {
	    output[n]=((int)(d) << 16) | (int)(e);
	    output[n+1]=(m<<16)|flag0;
	    n=n+2;
	    }
	 else
	    error[0]=6;
	 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;
	    goto bskip;
	    }
	 if ((d/p)*p!=d) {
	    flag=0;
	    rem=d-(d/ps)*ps;
	    for (l=0; l<p-1; l++) {
	       if (rem==residue[l])
		  flag=1;
	       }
	    if (flag==0) {
	       error[1]=1;
	       error[2]=d;
	       error[3]=e;
	       goto bskip;
	       }
	    }
	 if (((e/p)*p!=e)) {
	    flag=0;
	    rem=e-(e/ps)*ps;
	    for (l=0; l<p-1; l++) {
	       if (rem==residue[l])
		  flag=1;
	       }
	    if (flag==0) {
	       error[1]=2;
	       error[2]=d;
	       error[3]=e;
	       goto bskip;
	       }
	    }
	 if ((((d+e)/p)*p!=(d+e))&&(((d-e)/p)*p!=(d-e))) {
	    flag=0;
	    if (sumdif==0)
	       rem=(d+e)-((d+e)/ps)*ps;
	    else
	       rem=(d-e)-((d-e)/ps)*ps;
	    for (l=0; l<p-1; l++) {
	       if (rem==residue[l])
		  flag=1;
	       }
	    if (flag==0) {
	       error[1]=3;
	       error[2]=d;
	       error[3]=e;
	       goto bskip;
	       }
	    }
	 }
askip:dummy(d,e,5);
      }
   }
bskip:
output[n]=-1;
fprintf(Outfp," error0=%d error1=%d asave=%d bsave=%d \n",error[0],error[1],
		error[2],error[3]);
fprintf(Outfp," count=%d \n",(n+1)/2);
for (i=0; i<(n+1)/2; i++)
   fprintf(Outfp," %#10x %#10x \n",output[2*i],output[2*i+1]);
if ((error[1]!=0)||(error[0]!=0))
   printf(" error=%d, %d \n",error[0],error[1]);
printf("\n");
fprintf(Outfp,"\n");
for (i=0; i<500; i++)
   fprintf(Outfp,"i=%d h=%d, %d, %d  \n",i,histo[i],histom[i],histop[i]);
return(0);
}