/*****************************************************************************/
/* */
/* FACTOR (a**p-b**p)/(a-b) (when [(a**p+b**p)/(a+b)] is a pth power) */
/* 02/26/14 (dkc) */
/* */
/* Program determines if p/2, 2p, 2, p, pa, 2pb, 4*p*p(a-b), and 4*p(a+b) */
/* are pth powers modulo the prime factors of (a^p-b^p)/(a-b) when 2 and p */
/* are split. Use "table4a" for a<=2000000. Use "table6a" for 2000000<a<= */
/* 2500000. Use "table7a" for 2500000<a<3000000. Modify "insize" */
/* accordingly. */
/* */
/* The output is "a, b, (code<<16)|number of prime factors, code0||code1, */
/* code2||code3, code4||code5, code6, "and" of code0,code1,...,code6". */
/* "code" values are 0, 1, 2, or 3 if p divides a, b, a-b, or a+b */
/* respectively. code0, code1,...,code6 are codes for the different prime */
/* factors of (a**p-b**p)/(a-b). A corresponding bit is set in the code */
/* if p/2, 2p, 2, p, -, -, -, -, pa, 2pb, -, 4*p(a+b), -, -, 4*p*p(a-b), or */
/* - is a pth power modulo a prime factor of (a^p-b^p)/(a-b). */
/* (Corresponding bits are set if 2 divides b.) */
/* */
/*****************************************************************************/
#include <math.h>
#include <stdio.h>
#include "table7a.h"
void dummy(unsigned int a, unsigned int b, unsigned int c, unsigned int d);
void bigprod(unsigned int a, unsigned int b, unsigned int c, unsigned int *p);
void quotient(unsigned int *a, unsigned int *b, unsigned int);
void bigresx(unsigned int a, unsigned int b, unsigned int c, unsigned int d,
unsigned int *e, unsigned int f);
void bigbigs(unsigned int *a, unsigned int *b);
void bigbigd(unsigned int *a, unsigned int *b);
void bigbigq(unsigned int a0, unsigned int a1, unsigned int a2,
unsigned int a3, unsigned int *quotient, unsigned int d2,
unsigned int d3);
unsigned int lmbd(unsigned int mode, unsigned int a);
void differ(unsigned int *a, unsigned int *b);
int main ()
{
unsigned int p=3; // input prime
unsigned int numfac=5; // number of distinct prime factors of (a**p-b**p)/(a-b)
unsigned int psflag=2; // if set to 1, factors must be of the form p**2*k+1
// if set to 0, factors must not be of the form p**2*k+1
// otherwise, factors can be of mixed types
unsigned int split=1; // if set to 0, don't allow 2 and p to "split"
// if set to 1, only allow "split" 2 and p
unsigned int offset=0; // offset into the input look-up table (must be
// even and less than "insize"). Used to reduce
// execution time.
extern unsigned int input[];
extern unsigned short table[];
extern unsigned int tmptab[];
extern unsigned int output[];
extern unsigned int error[];
extern unsigned int count;
unsigned int insize=1038; // table7a
//unsigned int insize=1068; // table6a
//unsigned int insize=6818; // table4a
unsigned int tsize=1228;
unsigned int maxsiz=200000;
unsigned int tmpsiz;
unsigned int outsiz=11000*7;
unsigned int save[20]; // solutions array
unsigned int savsiz=19; // size of solutions array minus one
unsigned int d,e,c;
unsigned int h,i,j,k,l,m,iters,limit,wrap;
unsigned int flag,temp,sumdif,ps;
unsigned int S[2],T[2],U[2],V[2],W[2],X[3],Y[4],Z[4],Up[2];
int yflag,zflag;
unsigned int rflag,sflag,tflag,uflag,vflag,wflag,xflag;
unsigned int n=0;
FILE *Outfp;
Outfp = fopen("out10.dat","w");
/*********************************/
/* extend prime look-up table */
/*********************************/
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=10001; d<10000000; 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) */
/***********************************/
error[0]=0; // clear error array
error[1]=0;
error[2]=0;
ps=p*p;
zflag=0;
count=0;
wrap=0;
for (h=offset; h<insize; h++) {
if (wrap>8) {
printf("count=%d \n",h+1);
wrap=0;
}
else
wrap=wrap+1;
zloop:
if (zflag<2) {
d=input[3*h];
e=input[3*h+1];
c=input[3*h+2];
sumdif=0;
}
else {
d=input[3*(h-1)+1];
e=input[3*h+1];
c=input[3*h+2];
sumdif=1;
}
if ((d/p)*p==d) {
if (split==0) {
if ((d/2)*2!=d)
goto askip;
}
if (split!=0) {
if ((d/2)*2==d)
goto askip;
}
yflag=0;
}
if ((e/p)*p==e) {
if (split==0) {
if ((e/2)*2!=e)
goto askip;
}
if (split!=0) {
if ((e/2)*2==e)
goto askip;
}
yflag=1;
}
if (((d+e)/p)*p==(d+e)) {
if (split==0) {
if (((d+e)/2)*2!=(d+e))
goto askip;
}
if (split!=0) {
if (((d+e)/2)*2==(d+e))
goto askip;
}
if (sumdif==0)
yflag=3;
else
yflag=2;
}
if (((d-e)/p)*p==(d-e)) {
if (split==0) {
if (((d-e)/2)*2!=(d-e))
goto askip;
}
if (split!=0) {
if (((d-e)/2)*2==(d-e))
goto askip;
}
if (sumdif==0)
yflag=2;
else
yflag=3;
}
/************************************/
/* 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++) {
bigprod(Y[2], Y[3], d, X);
Y[1]=X[0];
Y[2]=X[1];
Y[3]=X[2];
}
Z[0] = 0;
Z[1] = 0;
Z[2] = 0;
Z[3] = e;
for (i=0; i<p-1; i++) {
bigprod(Z[2], Z[3], e, X);
Z[1]=X[0];
Z[2]=X[1];
Z[3]=X[2];
}
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 = (33 - lmbd(1, S[1]))/2;
l = 1 << l;
}
else {
l = (65 - lmbd(1, S[0]))/2;
l = 1 << l;
}
k=0;
if (l>tmptab[tmpsiz-1]) {
flag=1;
k=tmpsiz-1;
}
else {
flag=0;
for (i=0; i<tmpsiz; i++) {
if (tmptab[i] < l) k=i;
else break;
}
}
l=k;
iters=0;
rflag=0xffffffff;
sflag=0xffffffff;
tflag=0xffffffff;
uflag=0xffffffff;
vflag=0xffffffff;
wflag=0xffffffff;
xflag=0xffffffff;
m=0;
for (i=0; i<=l; i++) {
k = tmptab[i];
quotient(S, T, k);
V[0]=T[0];
V[1]=T[1];
bigprod(T[0], T[1], k, X);
if ((S[0]!=X[1]) || (S[1]!=X[2])) continue;
if (psflag==1) {
if (((k-1)/ps)*ps!=(k-1))
goto askip;
}
if (psflag==0) {
if (((k-1)/ps)*ps==(k-1))
goto askip;
}
if (iters<numfac) {
rflag=0;
if ((d&1)==0)
bigresx(0, (k-1)/p, 0, k, U, p*d);
else
bigresx(0, (k-1)/p, 0, k, U, 2*p*d);
if ((U[0]==0)&&(U[1]==1))
rflag=rflag+128;
if ((e&1)==0)
bigresx(0, (k-1)/p, 0, k, U, p*e);
else
bigresx(0, (k-1)/p, 0, k, U, 2*p*e);
if ((U[0]==0)&&(U[1]==1))
rflag=rflag+64;
if (zflag==2)
bigresx(0, (k-1)/p, 0, k, U, 2*2*p*(d-e));
else
bigresx(0, (k-1)/p, 0, k, U, 2*2*p*(d+e));
if ((U[0]==0)&&(U[1]==1))
rflag=rflag+16;
if (zflag==2)
bigresx(0, (k-1)/p, 0, k, U, 2*2*p*p*(d+e));
else
bigresx(0, (k-1)/p, 0, k, U, 2*2*p*p*(d-e));
if ((U[0]==0)&&(U[1]==1))
rflag=rflag+2;
bigresx(0, (k-1)/p, 0, k, U, p);
Up[0]=U[0];
Up[1]=U[1];
if ((U[0]==0)&&(U[1]==1))
rflag=rflag+4096;
bigresx(0, (k-1)/p, 0, k, U, 2);
if ((U[0]==Up[0])&&(U[1]==Up[1]))
rflag=rflag+32768;
if ((U[0]==0)&&(U[1]==1)) {
rflag=rflag+8192;
if (split!=0) {
if (((k-1)/ps)*ps!=(k-1))
error[0]=8;
}
}
bigresx(0, (k-1)/p, 0, k, U, 2*p);
if ((U[0]==0)&&(U[1]==1))
rflag=rflag+16384;
}
aloop: S[0]=V[0];
S[1]=V[1];
save[m]=k;
if (m < savsiz) m=m+1;
else {
error[0]=3;
goto bskip;
}
quotient(S, T, k);
V[0]=T[0];
V[1]=T[1];
bigprod(T[0], T[1], k, X);
if ((S[0]==X[1]) && (S[1]==X[2])) goto aloop;
iters=iters+1;
if (iters<numfac) {
xflag=wflag;
wflag=vflag;
vflag=uflag;
uflag=tflag;
tflag=sflag;
sflag=rflag;
}
else {
if ((S[0]!=0)||(S[1]!=1))
goto askip;
}
}
/***********************************************/
/* output prime factors satisfying criterion */
/***********************************************/
if ((S[0]!=0) || (S[1]!=1)) {
if (flag==1) {
if (S[0]==0) {
j = (33 - lmbd(1, S[1]))/2;
j = 1 << j;
}
else {
j = (65 - lmbd(1, S[0]))/2;
j = 1 << j;
}
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 (psflag==1) {
if (((i-1)/ps)*ps!=(i-1))
goto askip;
}
if (psflag==0) {
if (((i-1)/ps)*ps==(i-1))
goto askip;
}
if (iters<numfac) {
rflag=0;
if ((d&1)==0)
bigresx(0, (i-1)/p, 0, i, U, p*d);
else
bigresx(0, (i-1)/p, 0, i, U, 2*p*d);
if ((U[0]==0)&&(U[1]==1))
rflag=rflag+128;
if ((e&1)==0)
bigresx(0, (i-1)/p, 0, i, U, p*e);
else
bigresx(0, (i-1)/p, 0, i, U, 2*p*e);
if ((U[0]==0)&&(U[1]==1))
rflag=rflag+64;
if (zflag==2)
bigresx(0, (i-1)/p, 0, i, U, 2*2*p*(d-e));
else
bigresx(0, (i-1)/p, 0, i, U, 2*2*p*(d+e));
if ((U[0]==0)&&(U[1]==1))
rflag=rflag+16;
if (zflag==2)
bigresx(0, (i-1)/p, 0, i, U, 2*2*p*p*(d+e));
else
bigresx(0, (i-1)/p, 0, i, U, 2*2*p*p*(d-e));
if ((U[0]==0)&&(U[1]==1))
rflag=rflag+2;
bigresx(0, (i-1)/p, 0, i, U, p);
Up[0]=U[0];
Up[1]=U[1];
if ((U[0]==0)&&(U[1]==1))
rflag=rflag+4096;
bigresx(0, (i-1)/p, 0, i, U, 2);
if ((U[0]==Up[0])&&(U[1]==Up[1]))
rflag=rflag+32768;
if ((U[0]==0)&&(U[1]==1)) {
rflag=rflag+8192;
if (split!=0) {
if (((i-1)/ps)*ps!=(i-1))
error[0]=8;
}
}
bigresx(0, (i-1)/p, 0, i, U, 2*p);
if ((U[0]==0)&&(U[1]==1))
rflag=rflag+16384;
}
iters=iters+1;
if (iters<numfac) {
xflag=wflag;
wflag=vflag;
vflag=uflag;
uflag=tflag;
tflag=sflag;
sflag=rflag;
}
if (T[0]<=limit) { // largest prime in table is 5801977
S[0]=T[0];
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: if ((S[0]==0)&&(S[1]==1)) {
if (iters!=numfac)
goto askip;
else
goto dskip;
}
if ((S[0]==0)&&(S[1]==save[m])) {
if (iters==numfac)
goto dskip;
else
goto askip;
}
if (iters!=(numfac-1))
goto askip;
if (psflag==1) {
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 (psflag==0) {
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;
}
T[0]=0;
T[1]=1;
differ(S, T);
quotient(T, T, p);
rflag=0;
if ((d&1)==0)
bigresx(T[0], T[1], S[0], S[1], U, p*d);
else
bigresx(T[0], T[1], S[0], S[1], U, 2*p*d);
if ((U[0]==0)&&(U[1]==1))
rflag=rflag+128;
if ((e&1)==0)
bigresx(T[0], T[1], S[0], S[1], U, p*e);
else
bigresx(T[0], T[1], S[0], S[1], U, 2*p*e);
if ((U[0]==0)&&(U[1]==1))
rflag=rflag+64;
if (zflag==2)
bigresx(T[0], T[1], S[0], S[1], U, 2*2*p*(d-e));
else
bigresx(T[0], T[1], S[0], S[1], U, 2*2*p*(d+e));
if ((U[0]==0)&&(U[1]==1))
rflag=rflag+16;
if (zflag==2)
bigresx(T[0], T[1], S[0], S[1], U, 2*2*p*p*(d+e));
else
bigresx(T[0], T[1], S[0], S[1], U, 2*2*p*p*(d-e));
if ((U[0]==0)&&(U[1]==1))
rflag=rflag+2;
bigresx(T[0], T[1], S[0], S[1], U, p);
Up[0]=U[0];
Up[1]=U[1];
if ((U[0]==0)&&(U[1]==1))
rflag=rflag+4096;
bigresx(T[0], T[1], S[0], S[1], U, 2);
if ((U[0]==Up[0])&&(U[1]==Up[1]))
rflag=rflag+32768;
if ((U[0]==0)&&(U[1]==1)) {
rflag=rflag+8192;
if (split!=0) {
V[0]=0;
V[1]=1;
differ(S,V);
quotient(V,V,ps);
bigprod(V[0],V[1],ps,X);
V[0]=0;
V[1]=1;
differ(S,V);
if ((X[1]!=V[0])||(X[2]!=V[1]))
error[0]=8;
}
}
bigresx(T[0], T[1], S[0], S[1], U, 2*p);
if ((U[0]==0)&&(U[1]==1))
rflag=rflag+16384;
dskip: if (n+6>outsiz) {
error[0]=6;
goto bskip;
}
output[n]=d;
output[n+1]=e;
output[n+2]=(yflag<<16)|(m+1);
output[n+3]=(xflag<<16)|wflag;
output[n+4]=(vflag<<16)|uflag;
output[n+5]=(tflag<<16)|sflag;
xflag=rflag&sflag&tflag&uflag&vflag&wflag&wflag;
output[n+6]=(rflag<<16)|xflag;
if (split!=0) {
if ((output[n+6]&0xfff)!=0x0d2)
printf("error: d=%d, e=%d, %#010x, z=%d \n",d,e,output[n+6],zflag);
}
else {
if (((d&1)==0)||((e&1)==0)) {
if (((output[n+6]&0x1fff)!=0x10d2)&&((output[n+6]&0x1000)!=0))
printf("error: d=%d, e=%d, %#010x, z=%d \n",d,e,output[n+6],zflag);
}
}
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;
}
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]))
error[1]+=1;
n=n+7;
count=count+1;
if ((numfac==2)&&(split==0)&&(psflag==0)) {
if ((yflag==0)||(yflag==1)) {
if (rflag!=sflag)
error[0]=8;
T[0]=0;
T[1]=1;
differ(W,T);
quotient(T,T,ps);
bigprod(T[0],T[1],ps,X);
T[0]=0;
T[1]=1;
differ(W,T);
if ((X[1]!=T[0])||(X[2]!=T[1]))
error[0]=8;
}
}
}
//
// S[0]=0, S[1]=1
//
else {
if (iters!=numfac)
goto askip;
if (n+6>outsiz) {
error[0]=6;
goto bskip;
}
output[n]=d;
output[n+1]=e;
output[n+2]=(yflag<<16)|m;
output[n+3]=(xflag<<16)|wflag;
output[n+4]=(vflag<<16)|uflag;
output[n+5]=(tflag<<16)|sflag;
xflag=rflag&sflag&tflag&uflag&vflag&wflag&wflag;
output[n+6]=(rflag<<16)|xflag;
if (split!=0) {
if ((output[n+6]&0xfff)!=0x0d2)
printf("error: d=%d, e=%d, %#010x, z=%d \n",d,e,output[n+6],zflag);
}
else {
if (((d&1)==0)||((e&1)==0)) {
if (((output[n+6]&0x1fff)!=0x10d2)&&((output[n+6]&0x1000)!=0))
printf("error: d=%d, e=%d, %#010x, z=%d \n",d,e,output[n+6],zflag);
}
}
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;
}
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]))
error[1]+=1;
n=n+7;
count=count+1;
if ((numfac==2)&&(split==0)&&(psflag==0)) {
if ((yflag==0)||(yflag==1)) {
if (rflag!=sflag)
error[0]=8;
T[0]=0;
T[1]=1;
differ(W,T);
quotient(T,T,ps);
bigprod(T[0],T[1],ps,X);
T[0]=0;
T[1]=1;
differ(W,T);
if ((X[1]!=T[0])||(X[2]!=T[1]))
error[0]=8;
}
}
}
askip: if (zflag==2)
zflag=-1;
zflag+=1;
if (zflag==2)
goto zloop;
}
bskip:
output[n]=0xffffffff;
fprintf(Outfp," count=%d \n",(n+1)/7);
for (i=0; i<(n+1)/7; i++)
fprintf(Outfp," %#9x %#9x %#10x %#10x %#10x %#10x %#6x %#6x \n",
output[7*i],output[7*i+1],output[7*i+2],output[7*i+3],output[7*i+4],
output[7*i+5],(unsigned short)(output[7*i+6]>>16),
(unsigned short)(output[7*i+6]&0xffff));
fclose(Outfp);
return(0);
}