- Notifications
You must be signed in to change notification settings - Fork 0
Files
/
kGMP.cc
628 lines (597 loc) · 24.6 KB
/
kGMP.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#include <list>
#include <cmath>
#include <string>
#include <random>
#include <algorithm>
#include <vector>
#include <iostream>
#include <fstream>
#include <time.h>
#include <stdlib.h>
#include <gmpxx.h>
#include <gmp.h>
#define cyclemax 12
using namespace std;
// code base mxlg [d1[@d2] [d3[@d4]]]
// tries to extend the current kernel for base with primes up to length mxlg
// if d1 is specified, we only consider families starting with digit d1 (up to d2 if specified)
// if d3 is specified, we only consider families ending with digit d3 (up to d4 if specified)
// new found primes are gathered in a file newprimes{base}-suffix
// and families to be still analyzed are gathered in a file left{base}-suffix
// details of the exploration are send to the standard output
char tr[128]; int w[128];
using family=vector <string>;
using sint=long unsigned int; // std::vector<std::__cxx11::basic_string<char> >::size_type;
vector <string> found;
ifstream kern; ofstream result,reject,court;
sint b;
vector <sint> fact; string covering;
clock_t cpustarttime,cputime;
mpz_class nt,nprime,nlong,n1,y,z,z1,z2,bx,zz1,zz2,zzz;
sint k,lgmax,nbsmall;
mpz_t rtt,rt1,rt2;
random_device rd;
mt19937 gen(rd()); // set random gen
string s10(mpz_class x)
{mpz_class y=x; int r; string s="";
while (y!=0){r=mpz_class{y%10}.get_si();y=y/10;s=tr[r]+s;}
return s;
}
string sb(mpz_class x)
{mpz_class y=x; int r; string s="";if(x==0)return "0";
while (y!=0){r=mpz_class{y%b}.get_si();y=y/b;s=tr[r]+s;}
return s;
}
string familystrip(family f)
{string ss; sint i;
ss="";
for(i=0;i<f.size();i=i+2)ss=ss+f[i];
return(ss);
}
string stringf(family f)
{sint i; string s;if(f.size()==1)return f[0];
if(f.size()==1){s=f[0];return s;}
for(i=0;i<f.size()-2;i=i+2)s=s+f[i]+"{"+f[i+1]+"}";
s=s+f[i];
return s;
}
long long gcd(long long i,long long j)
{long long ii,jj,kk; if(i==0)return j;if(j==0)return i;
if(j>i){ii=j;jj=i;} else {ii=i;jj=j;}
while(jj!=0){kk=ii%jj;ii=jj;jj=kk;}
return ii;}
mpz_class gcd(mpz_class i,mpz_class j)
{mpz_class ii,jj,kk; if(i==0)return j;if(j==0)return i;
if(j>i){ii=j;jj=i;} else {ii=i;jj=j;}
while(jj!=0){kk=ii%jj;ii=jj;jj=kk;}
return ii;}
int min( int i, int j)
{if(i<=j)return i;else return j;}
void buildfact()
{sint kk,n; bool ok;
fact.push_back(2);fact.push_back(3);fact.push_back(5);
for(n=7;n<b*b*b;n++)
{ok=true;for(kk=0;fact[kk]*fact[kk]<=n;kk++)
if(n%fact[kk]==0){ok=false;break;}
if(ok){fact.push_back(n);}
}
for(nbsmall=0;fact[nbsmall]<b*b;nbsmall++)continue;
}
bool cover(string el)
// checks if some member of found is a substring of el
{sint i,j,it;
for(it=0;it!=found.size();it++)
{if(el==found[it]){covering=found[it];return true;}
if(el.length()<found[it].length())continue;
i=0;j=0;
while(i<el.length())
{if(el[i]==(found[it])[j])
{i++;j++;if(j>=(found[it]).length()){covering=found[it];return true;}
if(i>=el.length())break;
}
else {i++;if(i>=el.length())break;}
}
}
return false;
}
bool newprime(string s)
{sint i; mpz_class n1=0;
for(i=0;i<s.length();i++)n1=n1*b+w[s[i]];
if(mpz_probab_prime_p(n1.get_mpz_t(),50)>0)
{nprime++;court<<s<<"\n";found.push_back(s);court.flush();return true;}
else {return false;}
}
bool difsq(string s1,int y,string s2)
{sint i; mpz_class bb,bx,xb,yb,zy,gg,xx,yy,zz,z1,z2; bool test;
xb=0;yb=0;bx=1;bb=b;zy=y;
if(!mpz_root(rtt,bb.get_mpz_t(),2))return false;
for(i=0;i<s1.size();i++)xb=xb*b+w[s1[i]];
for(i=0;i<s2.size();i++)yb=yb*b+w[s2[i]];
for(i=0;i<s2.size();i++)bx=bx*b;
gg=gcd(zy,bb-1);
xx=(y+(b-1)*xb)/gg;
if(!mpz_root(rtt,xx.get_mpz_t(),2))return false;
yy=(bx*y-(b-1)*yb)/gg;
if(!mpz_root(rtt,yy.get_mpz_t(),2))return false;
z2=mpz_class(rtt);
zz=bx*xx;
mpz_root(rtt,zz.get_mpz_t(),2);
z1=mpz_class(rtt);
test=(z1-z2)>((b-1)/gg);
if(test)
{cout<<s1<<"{"<<tr[y]<<"}"<<s2<<" is a difference of squares "
<<b<<", "<<xx<<" and "<<yy<<"\n"; return true;}
return false;
}
bool p4p4p4b(string s1,int y,string s2)
// checks if we have a factorisation of x^4+4.y^4 for {d}
{sint i; mpz_class bb,bx,xb,yb,zy,gg,xx,yy,zz,z1,z2; bool t1,t2;
xb=0;yb=0;bx=1;bb=b;zy=y;
if(!mpz_root(rtt,bb.get_mpz_t(),4))return false;
for(i=0;i<s1.size();i++)xb=xb*b+w[s1[i]];
for(i=0;i<s2.size();i++)yb=yb*b+w[s2[i]];
for(i=0;i<s2.size();i++)bx=bx*b;
gg=gcd(zy,bb-1);
xx=(y+(b-1)*xb)/gg;
yy=((b-1)*yb-bx*y)/gg;if(yy<=0)return false;
if(xx%4==0){z1=xx/4; t1=mpz_root(rt1,z1.get_mpz_t(),4);
t2=mpz_root(rt2,yy.get_mpz_t(),4);
if(t1&&t2)
{cout<<s1<<"{"<<sb(y)<<"}"<<s2<<" has a Germain's factorisation ("
<<z1<<","<<yy<<")\n";return true;}
}
if(yy%4==0){z2=yy/4; t1=mpz_root(rt2,z2.get_mpz_t(),4);
t2=mpz_root(rt2,xx.get_mpz_t(),4);
if(t1&&t2)
{cout<<s1<<"{"<<sb(y)<<"}"<<s2<<" has a Germain's factorisation ("
<<xx<<","<<z2<<")\n";return true;}
}
return false;
}
bool p4p4p4bb(string s1,string sddk,string s2)
// checks if we have a factorisation of x^4+4.y^4 for {dd} or {dddd}
{sint i,k; mpz_class bb,bx,xb,yb,y,gg,xx,yy,zz,z1,z2; bool t1,t2;
xb=0;yb=0;bx=1;y=0;k=sddk.size();for(i=0;i<k;i++)y=y*b+w[sddk[i]];
bb=b;if(k==2)if(!mpz_root(rtt,bb.get_mpz_t(),2))return false;
if(s2.size()%k!=0)
{for(i=0;i<s2.size()%k;i++){s2=sddk[0]+s2;
if(cover(s1+s2))return false;
if(newprime(s1+s2))return false;}
for(i=i;i<k;i++){s1=s1+sddk[0];
if(cover(s1+s2))return false;
if(newprime(s1+s2))return false;}
}
for(i=0;i<s1.size();i++)xb=xb*b+w[s1[i]];
for(i=0;i<s2.size();i++)yb=yb*b+w[s2[i]];
for(i=0;i<s2.size();i++)bx=bx*b;
gg=gcd(y,bx-1);
xx=(y+(bx-1)*xb)/gg;
yy=((bx-1)*yb-bx*y)/gg;if(yy<=0)return false;
if(xx%4==0){z1=xx/4; t1=mpz_root(rt1,z1.get_mpz_t(),4);
t2=mpz_root(rt2,yy.get_mpz_t(),4);
if(t1&&t2)
{cout<<s1<<"{"<<sb(y)<<"}"<<s2<<" has a Germain's factorisation ("
<<z1<<","<<yy<<")\n";return true;}
}
if(yy%4==0){z2=yy/4; t1=mpz_root(rt2,z2.get_mpz_t(),4);
t2=mpz_root(rt2,xx.get_mpz_t(),4);
if(t1&&t2)
{cout<<s1<<"{"<<sb(y)<<"}"<<s2<<" has a Germain's factorisation ("
<<xx<<","<<z2<<")\n";return true;}
}
return false;
}
bool difpowb(string s1,int y,string s2,int k)
{sint i; mpz_class bb,bx,xb,yb,zy,gg,xx,yy,zz,rt,rtk,z1,z2; bool test;
xb=0;yb=0;bx=1;bb=b;zy=y;
if(!mpz_root(rtt,bb.get_mpz_t(),k))return false;
for(i=0;i<s1.size();i++)xb=xb*b+w[s1[i]];
for(i=0;i<s2.size();i++)yb=yb*b+w[s2[i]];
for(i=0;i<s2.size();i++)bx=bx*b;
gg=gcd(zy,bb-1);
xx=(y+(b-1)*xb)/gg;
if(!mpz_root(rtt,xx.get_mpz_t(),k))return false;
yy=(bx*y-(b-1)*yb)/gg;
if(yy<0&&k%2==0)return false;
if(!mpz_root(rtt,yy.get_mpz_t(),k))return false;
zz=bx*xx;
mpz_root(rt1,zz.get_mpz_t(),k);z1=mpz_class(rt1);
mpz_root(rt2,yy.get_mpz_t(),k);z2=mpz_class(rt2);
test=(z1-z2)>((b-1)/gg);
if(test)
{cout<<s1<<"{"<<sb(y)<<"}"<<s2<<" is a difference of "
<<k<<"th powers: "<<b<<"^*."<<xx<<" and "<<yy<<"\n";
return true;}
return false;
}
bool difpowbb(string s1,string sddk,string s2)
{sint i,k; mpz_class bb,bx,xb,yb,y,gg,xx,yy,zz,rt,rtk,z1,z2; bool test;
xb=0;yb=0;bx=1;y=0;k=sddk.size();for(i=0;i<k;i++)y=y*b+w[sddk[i]];
bb=1;for(i=0;i<k;i++){bb=bb*b;}
if(s2.size()%k!=0)
{for(i=0;i<s2.size()%k;i++){s2=sddk[0]+s2;
if(cover(s1+s2))return false;
if(newprime(s1+s2))return false;}
for(i=i;i<k;i++){s1=s1+sddk[0];
if(cover(s1+s2))return false;
if(newprime(s1+s2))return false;}
}
for(i=0;i<s1.size();i++)xb=xb*b+w[s1[i]];
for(i=0;i<s2.size();i++)yb=yb*b+w[s2[i]];
for(i=0;i<s2.size();i++)bx=bx*b;
gg=gcd(y,bb-1);
xx=(y+(bb-1)*xb)/gg;
if(!mpz_root(rtt,xx.get_mpz_t(),k)){return false;}
yy=(bx*y-(bb-1)*yb)/gg;
if(yy<0&&k%2==0)return false;
if(!mpz_root(rtt,yy.get_mpz_t(),k))return false;
zz=bx*xx;
mpz_root(rt1,zz.get_mpz_t(),k);z1=mpz_class(rt1);
mpz_root(rt2,yy.get_mpz_t(),k);z2=mpz_class(rt2);
test=(z1-z2)>((bb-1)/gg);
if(test)
{cout<<s1<<"{"<<sb(y)<<"}"<<s2<<" is a difference of "
<<k<<"th powers: "<<bb<<", "<<xx<<" and "<<yy<<"\n";
return true;}
return false;
}
bool smallfactor(family f)
{sint i,ii,i1,i2;mpz_class n1,z1,z2,bx,zz1,zz2,zzz;
for(ii=0;ii<nbsmall;ii++)
{i=fact[ii];n1=0;
for(i1=0;i1<f.size();i1++)
{if(i1%2==0)
{for(i2=0;i2<f[i1].size();i2++)n1=(n1*b+w[f[i1][i2]])%i;
}
else
{for(i2=0;i2<f[i1].size();i2++){if(n1!=((n1*b+w[f[i1][i2]])%i))break;}
if(i2!=f[i1].size())break;
}
}
if(i1!=f.size())continue;
if(n1==0){cout<<"small factor "<<i<<" for "<<stringf(f)<<"\n";return true;}
}
if(f.size()==3)if(f[1].size()==1)
{z1=0;z2=0;bx=1;y=w[f[1][0]];
for(i=0;i<f[0].size();i++)z1=z1*b+w[f[0][i]];
cout<<"extend212 "<<stringf(f)<<" with ("<<ic1<<","<<f[ic1][jc1]<<
") and ("<<ic2<<","<<f[ic2][jc2]<<")\n";
handelf(ff);
} else
{ff[ic1]=ff[ic1]+p2;ff.insert(ff.begin()+ic1+1,p1);ff.insert(ff.begin()+ic1+2,fff[ic1]+p1);
ff.insert(ff.begin()+ic2+3,p2);ff.insert(ff.begin()+ic2+4,f[ic2]);
cout<<"extend212 "<<stringf(f)<<" with ("<<ic1<<","<<f[ic1][jc1]<<
") and ("<<ic2<<","<<f[ic2][jc2]<<")\n";
handelf(ff);ff=fff;
ff[ic1]=ff[ic1]+p1;ff.insert(ff.begin()+ic1+1,p2);ff.insert(ff.begin()+ic1+2,fff[ic2]+p2);
ff.insert(ff.begin()+ic2+3,p1);ff.insert(ff.begin()+ic2+4,f[ic1]);
cout<<"extend221 "<<stringf(f)<<" with ("<<ic1<<","<<f[ic1][jc2]<<
") and ("<<ic2<<","<<f[ic2][jc1]<<")\n";
handelf(ff);
}
}
} // fin du traitement de la base
int main(int argc, char *argv[])
{string ms,pref,prefms,p,ss,chunkl,chunkr; family f;
sint i,ilb,ilh,irb,irh,il,ir,j;
mpz_init(rtt); mpz_init(rt1); mpz_init(rt2);
ms=argv[1];b=atoi(argv[1]); lgmax=atoi(argv[2]);
pref="kernel"; prefms=pref+ms;
kern.open(prefms.c_str());
if(argc>3){chunkl=argv[3];ms=ms+'-'+argv[3];}else{chunkl="";}
if(argc>4){chunkr=argv[4];ms=ms+'-'+argv[4];}else{chunkr="";}
pref="result"; prefms=pref+ms;
result.open(prefms.c_str(),ios::out);
pref="newprimes"; prefms=pref+ms;
court.open(prefms.c_str(),ios::out);
pref="left"; prefms=pref+ms;
reject.open(prefms.c_str(),ios::out);
for(char c='0';c<='9';c++){w[c]=0+c-'0';tr[0+c-'0']=c;}
for(char c='A';c<='Z';c++){w[c]=10+c-'A';tr[10+c-'A']=c;}
for(char c='a';c<='z';c++){w[c]=36+c-'a';tr[36+c-'a']=c;}
buildfact();
if(chunkl!="")cout<<" for initial digits "<<chunkl;
if(chunkr!="")cout<<" and for terminal digits "<<chunkr;
cout<<"\n";
cout<<"lgmax="<<lgmax<<"\n";cout.flush();
found.clear();nt=0;nprime=0;nlong=0;
while(kern>>p)
{found.push_back(p);
}
// initialisation
if(chunkl==""){ilb=1;ilh=b-1;}
else {for(i=0;i<chunkl.length();i++)if(chunkl[i]=='@')break;
if(i==chunkl.length()){ilb=stoi(chunkl);ilh=ilb;}
else {ilb=stoi(chunkl.substr(0,i));ilh=stoi(chunkl.substr(i+1));}
}
if(chunkr==""){irb=1;irh=b-1;}
else {for(i=0;i<chunkr.length();i++)if(chunkr[i]=='@')break;
if(i==chunkr.length()){irb=stoi(chunkr);irh=irb;}
else {irb=stoi(chunkr.substr(0,i));irh=stoi(chunkr.substr(i+1));}
}
cpustarttime=clock();
ss="";for(j=0;j<b;j++)ss=ss+tr[j];
f.clear();f.push_back("0");f.push_back(ss);f.push_back("0");
for(il=ilb;il<=ilh;il++)for(ir=irb;ir<=irh;ir++)if(gcd(b,ir)==1)
{f[0][0]=tr[il];f[2][0]=tr[ir];
//construction of the familles to be explored from the first and last digits
handelf(f);
}
cputime = clock()-cpustarttime; cputime=cputime/1000000;
result<<"\nFinally, in "<<cputime<<" time units, we explored "
<<nt<<" families of numbers for base "<<b;
if(chunkl!="")result<<" and the initial digit "<<chunkl;
if(chunkr!="")result<<" and the terminal digit "<<chunkr;
result<<"\nthere remains "<<nprime<<" primes to incorporate in the kernel, and "
<<nlong<<" families to analyse\n";
} // end main