/*****************************************************************************/
/*									     */
/*  FACTOR (d**p + e**p)/(d + e)					     */
/*  11/13/06 (dkc)							     */
/*									     */
/*  This C program finds d and e such that (d**p + e**p)/(d + e) is a cube   */
/*  or p times a cube.	p is set to 3.					     */
/*									     */
/*****************************************************************************/
#include <stdio.h>
#include <math.h>
#include "table0c.h"
unsigned int lmbd(unsigned int mode, unsigned int a);
void sum(unsigned int *addend, unsigned int *augend);
void differ(unsigned int *minuend, unsigned int *subtrahend);
void bigprod(unsigned int a, unsigned int b, unsigned int c, unsigned int *p);
void quotient(unsigned int *a, unsigned int *b, unsigned int);
int main ()
{
int p=3;	    // input prime
int dbeg=10000;     // starting "a" value
int dend=1;	    // ending "a" value
//int stop=0x2e831;
int sumdif=1;	    // select [(a**p+b**p)/(a+b)] if "sumdif" is non-zero,
		    // or [(a**p-b**p)/(a-b)] otherwise

extern unsigned short table3[];
extern unsigned int output[];
extern unsigned int error[];
int t3size=2556;
int outsiz=1999;
int n=0;
int d,e,a,b,temp;
int i,j,k,l,lp,m;
unsigned int S[2],T[2],V[2],X[3];
double croot2,croot4,halfcr4;
FILE *Outfp;
Outfp = fopen("output.dat","w");
croot2=1.259921;
croot4=1.587401;
halfcr4=croot4*((double)(0.5));
/***********************************/
/*  factor (d**p + e**p)/(d + e)   */
/***********************************/
error[0]=0;	// clear error array
for (d=dbeg; d>=dend; d--) {
   for (e=d-1; e>0; e--) {
//    if (e!=stop) continue;
/*******************************/
/*  check for common factors   */
/*******************************/
      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;
/************************************/
/*  compute (d**p + e**p)/(d + e)   */
/************************************/
      S[0]=0;
      S[1]=d;
      for (i=0; i<p-1; i++) {
	 bigprod(S[0], S[1], d, X);
	 S[0]=X[1];
	 S[1]=X[2];
	 }
      T[0]=0;
      T[1]=e;
      for (i=0; i<p-1; i++) {
	 bigprod(T[0], T[1], e, X);
	 T[0]=X[1];
	 T[1]=X[2];
	 }
      if (sumdif==1) {
	 sum(S, T);
	 temp=d+e;
	 if (((d+e)/p)*p==(d+e))
	    temp=temp*p;
	 quotient(T, S, temp);
	 }
      else {
	 differ(S, T);
	 temp=d-e;
	 if (((d-e)/p)*p==(d-e))
	    temp=temp*p;
	 quotient(T, S, temp);
	 }
/************************************************/
/*  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/3)*3;
      l=l/3;
      l = 1 << l;
      if (j==0)
	 lp=(int)(((double)(l))*halfcr4);
      if (j==1) {
	 lp=l;
	 l=(int)(((double)(l))*croot2);
	 }
      if (j==2){
	 lp=(int)(((double)(l))*croot2);
	 l=(int)(((double)(l))*croot4);
	 }
      lp=lp-1;
      l=l+1;
      if (l>table3[t3size-1]) {
	 error[0]=5;
	 goto bskip;
	 }
      else {
	 j=0;
	 for (i=0; i<t3size; i++) {
	    if (table3[i] < lp) j=i;
	    else break;
	    }
	 k=j;
	 for (i=j; i<t3size; i++) {
	    if (table3[i] < l) k=i;
	    else break;
	    }
	 }
      for (i=j; i<=k; i++) {
	 m=0;
	 l = table3[i];
	 quotient(S, V, l);
	 bigprod(V[0], V[1], l, X);
	 if ((S[0]!=X[1]) || (S[1]!=X[2])) continue;
aloop:	 S[0]=V[0];
	 S[1]=V[1];
	 m=m+1;
	 quotient(S, V, l);
	 bigprod(V[0], V[1], l, X);
	 if ((S[0]==X[1]) && (S[1]==X[2])) goto aloop;
	 if ((m/3)*3!=m)
	    goto askip;
	 }
      if ((S[0]!=0) || (S[1]!=1)) continue;
      if (n+1>outsiz) {
	 error[0]=6;
	 goto bskip;
	 }
      output[n]=d;
      output[n+1]=e;
      n=n+2;
askip:temp=0;
      }
   }
bskip:
output[n]=-1;
fprintf(Outfp," error=%d \n",error[0]);
fprintf(Outfp," count=%d \n",(n+1)/3);
for (i=0; i<(n+1)/2; i++)
   fprintf(Outfp," %#10x %#10x \n",output[2*i],output[2*i+1]);
fclose(Outfp);
return(0);
}