/*****************************************************************************/
/*									     */
/*  FACTOR (a**p+b**p)/(a+b)						     */
/*  11/27/06 (dkc)							     */
/*									     */
/*  This C program finds a and b such that every prime factor of	     */
/*  (a**p+b**p)/(a+b) other than p is of the form p**2*k+1.  p**2 must	     */
/*  divide a, b, a+b or a-b.  Whether a (or b) is a pth power modulo p**2    */
/*  when p does not divide a (or b) is determined.  Whether a+b and a-b      */
/*  are pth powers modulo p**2 when p does not divide a+b or a-b is	     */
/*  determined.  (These are the "weak" Furtwangler conditions.)              */
/*									     */
/*  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 the "weak" Furtwangler conditions aren't  */
/*  satisfied, then an error is indicated ("error[1]" is set to a non-zero   */
/*  value).								     */
/*									     */
/*****************************************************************************/
#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 50;
//
unsigned int p=3;	     // input prime
unsigned int dbeg=1000;	     // starting "a" value
unsigned int dend=1;	     // ending "a" value
//unsigned int stop=0x44;
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 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,limit,fflag;
unsigned short rem;
unsigned int S[2],T[2],V[2],W[2],X[3],Y[4],Z[4];
unsigned int ps;
unsigned int n=0;
double sqrt2=1.4142135;
FILE *Outfp;
Outfp = fopen("out37.dat","w");
ps=p*p;
/*********************************/
/*  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)   */
/***********************************/
error[1]=0;
error[2]=0;
count=0;
for (d=dbeg; d>=dend; d--) {
   for (e=d-1; e>0; e--) {
//    if (e!=stop) continue;
/*********************************************/
/*  check if p**2 divides d, e, d+e or d-e   */
/*********************************************/
      if ((d/ps)*ps==d)
	 goto zskip;
      if ((e/ps)*ps==e)
	 goto zskip;
      if (((d+e)/ps)*ps==(d+e))
	 goto zskip;
      if (((d-e)/ps)*ps!=(d-e))
	 continue;
/******************************************/
/*  check for common factors of d and e   */
/******************************************/
zskip: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 for Furtwangler conditions   */
/***************************************/
     fflag=1;
     if ((d/ps)*ps!=d) {
	flag=0;
	rem=d-(d/ps)*ps;
	for (l=0; l<p-1; l++) {
	   if (rem==residue[l])
	      flag=1;
	   }
	if (flag==0)
	   fflag=0;
	}
     if ((e/ps)*ps!=e) {
	flag=0;
	rem=e-(e/ps)*ps;
	for (l=0; l<p-1; l++) {
	   if (rem==residue[l])
	      flag=1;
	   }
	if (flag==0)
	   fflag=0;
	}
     if ((((d+e)/ps)*ps!=(d+e))&&(((d-e)/ps)*ps!=(d-e))) {
	flag=0;
	rem=(d+e)-((d+e)/ps)*ps;
	for (l=0; l<p-1; l++) {
	   if (rem==residue[l])
	      flag=1;
	   }
	if (flag==0)
	   fflag=0;
	flag=0;
	rem=(d-e)-((d-e)/ps)*ps;
	for (l=0; l<p-1; l++) {
	   if (rem==residue[l])
	      flag=1;
	   }
	if (flag==0)
	   fflag=0;
	}
/************************************/
/*  compute (d**p + e**p)/(d + e)   */
/************************************/
      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;
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;
		  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, 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;
	 if (n>outsiz) {
	    error[0]=6;
	    goto bskip;
	    }
	 output[n]=((int)(d) << 16) | (int)(e);
	 if (m>0)
	    printf("d=%d, e=%d, m=%d \n",d,e,m+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 (m>0) {
	    n=n+1;
	    count=count+1;
	    }
	 if (fflag==0) {
	    error[1]=8;
	    goto bskip;
	    }
	 }
      else {
	 if (n>outsiz) {
	    error[0]=6;
	    goto bskip;
	    }
	 output[n]=((int)(d) << 16) | (int)(e);
	 if (m>1)
	    printf("d=%d, e=%d, m=%d \n",d,e,m);
	 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 (m>1) {
	    n=n+1;
	    count=count+1;
	    }
	 if (fflag==0) {
	    error[1]=8;
	    goto bskip;
	    }
	 }
askip:dummy(d,e,6);
      }
   }
bskip:
output[n]=-1;
fprintf(Outfp," error0=%d error1=%d \n",error[0],error[1]);
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);
}